refactor(process): use map for subscribers and drop dead ring buffer
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -13,7 +13,6 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
outputBufferSize = 4 * 1024
|
outputBufferSize = 4 * 1024
|
||||||
ringBufferSize = 64 * 1024
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Manager manages OpenCode processes per workspace.
|
// Manager manages OpenCode processes per workspace.
|
||||||
@@ -93,6 +92,7 @@ func (m *LocalManager) Start(workspaceID string, workspaceRoot string) error {
|
|||||||
Root: workspaceRoot,
|
Root: workspaceRoot,
|
||||||
Cmd: cmd,
|
Cmd: cmd,
|
||||||
Stdin: stdinW,
|
Stdin: stdinW,
|
||||||
|
Subscribers: make(map[Subscription]struct{}),
|
||||||
}
|
}
|
||||||
m.sessions[workspaceID] = sess
|
m.sessions[workspaceID] = sess
|
||||||
|
|
||||||
@@ -103,31 +103,25 @@ func (m *LocalManager) Start(workspaceID string, workspaceRoot string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// captureOutput reads from the merged stdout/stderr pipe, keeps the most
|
// captureOutput reads from the merged stdout/stderr pipe and fans out each
|
||||||
// recent ringBufferSize bytes in an in-memory ring buffer, and fans out each
|
|
||||||
// chunk to all subscribers using non-blocking sends.
|
// chunk to all subscribers using non-blocking sends.
|
||||||
func (m *LocalManager) captureOutput(sess *Session, stdoutR *os.File, done chan<- struct{}) {
|
func (m *LocalManager) captureOutput(sess *Session, stdoutR *os.File, done chan<- struct{}) {
|
||||||
defer close(done)
|
defer close(done)
|
||||||
defer stdoutR.Close()
|
defer stdoutR.Close()
|
||||||
|
|
||||||
buf := make([]byte, outputBufferSize)
|
buf := make([]byte, outputBufferSize)
|
||||||
ring := newRingBuffer(ringBufferSize)
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
n, err := stdoutR.Read(buf)
|
n, err := stdoutR.Read(buf)
|
||||||
if n > 0 {
|
if n > 0 {
|
||||||
chunk := make([]byte, n)
|
chunk := make([]byte, n)
|
||||||
copy(chunk, buf[:n])
|
copy(chunk, buf[:n])
|
||||||
ring.Write(chunk)
|
|
||||||
|
|
||||||
sess.mu.Lock()
|
sess.mu.Lock()
|
||||||
subs := make([]Subscription, len(sess.Subscribers))
|
for sub := range sess.Subscribers {
|
||||||
copy(subs, sess.Subscribers)
|
|
||||||
sess.mu.Unlock()
|
|
||||||
|
|
||||||
for _, sub := range subs {
|
|
||||||
sub.(*subscription).send(chunk)
|
sub.(*subscription).send(chunk)
|
||||||
}
|
}
|
||||||
|
sess.mu.Unlock()
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err != io.EOF {
|
if err != io.EOF {
|
||||||
@@ -158,7 +152,7 @@ func (m *LocalManager) waitExit(sess *Session, outputDone <-chan struct{}) {
|
|||||||
_ = sess.Stdin.Close()
|
_ = sess.Stdin.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, sub := range sess.Subscribers {
|
for sub := range sess.Subscribers {
|
||||||
sub.(*subscription).closeChan()
|
sub.(*subscription).closeChan()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -230,7 +224,7 @@ func (m *LocalManager) Subscribe(workspaceID string) (Subscription, error) {
|
|||||||
|
|
||||||
sess.mu.Lock()
|
sess.mu.Lock()
|
||||||
sub := newSubscription(sess)
|
sub := newSubscription(sess)
|
||||||
sess.Subscribers = append(sess.Subscribers, sub)
|
sess.Subscribers[sub] = struct{}{}
|
||||||
if sess.Cmd.ProcessState != nil {
|
if sess.Cmd.ProcessState != nil {
|
||||||
sub.closeChan()
|
sub.closeChan()
|
||||||
}
|
}
|
||||||
@@ -274,28 +268,3 @@ func (m *LocalManager) exitOf(workspaceID string) (ExitInfo, error) {
|
|||||||
return sess.Exit, nil
|
return sess.Exit, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ringBuffer is a fixed-size byte buffer that drops the oldest bytes when full.
|
|
||||||
type ringBuffer struct {
|
|
||||||
mu sync.Mutex
|
|
||||||
buf []byte
|
|
||||||
maxN int
|
|
||||||
}
|
|
||||||
|
|
||||||
func newRingBuffer(maxN int) *ringBuffer {
|
|
||||||
return &ringBuffer{maxN: maxN}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *ringBuffer) Write(p []byte) {
|
|
||||||
r.mu.Lock()
|
|
||||||
defer r.mu.Unlock()
|
|
||||||
|
|
||||||
if len(p) >= r.maxN {
|
|
||||||
r.buf = append(r.buf[:0], p[len(p)-r.maxN:]...)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
r.buf = append(r.buf, p...)
|
|
||||||
if len(r.buf) > r.maxN {
|
|
||||||
r.buf = r.buf[len(r.buf)-r.maxN:]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ type Session struct {
|
|||||||
Root string
|
Root string
|
||||||
Cmd *exec.Cmd
|
Cmd *exec.Cmd
|
||||||
Stdin io.WriteCloser
|
Stdin io.WriteCloser
|
||||||
Subscribers []Subscription
|
Subscribers map[Subscription]struct{}
|
||||||
Exit ExitInfo
|
Exit ExitInfo
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
}
|
}
|
||||||
@@ -35,10 +35,5 @@ type Session struct {
|
|||||||
func (s *Session) removeSubscription(sub *subscription) {
|
func (s *Session) removeSubscription(sub *subscription) {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
for i, cur := range s.Subscribers {
|
delete(s.Subscribers, sub)
|
||||||
if cur == sub {
|
|
||||||
s.Subscribers = append(s.Subscribers[:i], s.Subscribers[i+1:]...)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user