diff options
-rw-r--r-- | buffer.go | 39 | ||||
-rw-r--r-- | worker.go | 4 |
2 files changed, 41 insertions, 2 deletions
diff --git a/buffer.go b/buffer.go new file mode 100644 index 00000000..c1265ccd --- /dev/null +++ b/buffer.go @@ -0,0 +1,39 @@ +package roadrunner + +import ( + "bytes" + "sync" +) + +// thread safe buffer +type buffer struct { + mu sync.Mutex + buffer *bytes.Buffer +} + +// Len returns the number of bytes of the unread portion of the buffer; +// b.Len() == len(b.Bytes()). +func (b *buffer) Len() int { + b.mu.Lock() + defer b.mu.Unlock() + + return b.buffer.Len() +} + +// Write appends the contents of p to the buffer, growing the buffer as +// needed. The return value n is the length of p; err is always nil. If the +// buffer becomes too large, Write will panic with ErrTooLarge. +func (b *buffer) Write(p []byte) (n int, err error) { + b.mu.Lock() + defer b.mu.Unlock() + + return b.buffer.Write(p) +} + +// Strings fetches all buffer data into string. +func (b *buffer) String() string { + b.mu.Lock() + defer b.mu.Unlock() + + return b.buffer.String() +} @@ -36,7 +36,7 @@ type Worker struct { // err aggregates stderr output from underlying process. Value can be // receive only once command is completed and all pipes are closed. - err *bytes.Buffer + err *buffer // channel is being closed once command is complete. waitDone chan interface{} @@ -60,7 +60,7 @@ func newWorker(cmd *exec.Cmd) (*Worker, error) { w := &Worker{ Created: time.Now(), cmd: cmd, - err: new(bytes.Buffer), + err: &buffer{buffer: new(bytes.Buffer)}, waitDone: make(chan interface{}), state: newState(StateInactive), } |