Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions packages/envd/internal/services/process/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"os/exec"
"os/user"
"slices"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -242,21 +243,25 @@ func New(

go logs.LogBufferedDataEvents(stdoutLogs, &stdoutLogger, "data")

// Reusable read buffer to avoid allocation per Read cycle when no
// subscribers are connected.
readBuf := make([]byte, stdChunkSize)

for {
buf := make([]byte, stdChunkSize)
n, readErr := stdout.Read(readBuf)

n, readErr := stdout.Read(buf)
if n > 0 && outMultiplex.HasSubscribers() {
data := slices.Clone(readBuf[:n])

if n > 0 {
outMultiplex.Source <- rpc.ProcessEvent_Data{
Data: &rpc.ProcessEvent_DataEvent{
Output: &rpc.ProcessEvent_DataEvent_Stdout{
Stdout: buf[:n],
Stdout: data,
},
},
}

stdoutLogs <- buf[:n]
stdoutLogs <- data
Comment thread
arkamar marked this conversation as resolved.
Comment thread
arkamar marked this conversation as resolved.
}
Comment thread
arkamar marked this conversation as resolved.

if errors.Is(readErr, io.EOF) {
Expand Down Expand Up @@ -284,21 +289,23 @@ func New(

go logs.LogBufferedDataEvents(stderrLogs, &stderrLogger, "data")

readBuf := make([]byte, stdChunkSize)

for {
buf := make([]byte, stdChunkSize)
n, readErr := stderr.Read(readBuf)

n, readErr := stderr.Read(buf)
if n > 0 && outMultiplex.HasSubscribers() {
data := slices.Clone(readBuf[:n])

if n > 0 {
outMultiplex.Source <- rpc.ProcessEvent_Data{
Data: &rpc.ProcessEvent_DataEvent{
Output: &rpc.ProcessEvent_DataEvent_Stderr{
Stderr: buf[:n],
Stderr: data,
},
},
}

stderrLogs <- buf[:n]
stderrLogs <- data
}
Comment thread
arkamar marked this conversation as resolved.

if errors.Is(readErr, io.EOF) {
Expand Down
14 changes: 14 additions & 0 deletions packages/envd/internal/services/process/handler/multiplex.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ func (m *MultiplexedChannel[T]) run() {
m.channels = nil
}

// HasSubscribers reports whether any non-cancelled subscriber exists.
func (m *MultiplexedChannel[T]) HasSubscribers() bool {
m.mu.RLock()
defer m.mu.RUnlock()

for _, s := range m.channels {
if !s.isCancelled() {
return true
}
}

return false
}

// Fork registers a new subscriber and returns its channel plus a cancel func.
// If Source is already closed it returns a pre-closed channel and a no-op cancel.
// The channel is bidirectional for backwards compat with start.go which writes
Expand Down
2 changes: 1 addition & 1 deletion packages/envd/pkg/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package pkg

const Version = "0.5.18"
const Version = "0.5.19"
Loading