agent/util/process/safebuffer.go (24 lines of code) (raw):
package process
import (
"bytes"
"sync"
)
// Buffer is a goroutine safe bytes.Buffer
type SafeBuffer struct {
buffer bytes.Buffer
mutex sync.Mutex
}
// Write appends the contents of p to the buffer, growing the buffer as needed. It returns
// the number of bytes written.
func (s *SafeBuffer) Write(p []byte) (n int, err error) {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.buffer.Write(p)
}
func (s *SafeBuffer) Read(p []byte) (n int, err error) {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.buffer.Read(p)
}
// String returns the contents of the unread portion of the buffer
// as a string. If the Buffer is a nil pointer, it returns "<nil>".
func (s *SafeBuffer) String() string {
s.mutex.Lock()
defer s.mutex.Unlock()
return s.buffer.String()
}