diff options
author | Wolfy-J <[email protected]> | 2018-06-05 23:28:18 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2018-06-05 23:28:18 +0300 |
commit | 28a5d8c3b6b7763e510617eeb8d483bfc6698b04 (patch) | |
tree | 50c897d4f2afef7fe96637ce1763084c588002f5 /error_buffer.go | |
parent | 5c45a050ae211ae565cbc8903adda7900ab06577 (diff) |
thread safe buffer for worker errors
Diffstat (limited to 'error_buffer.go')
-rw-r--r-- | error_buffer.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/error_buffer.go b/error_buffer.go new file mode 100644 index 00000000..fcf566c8 --- /dev/null +++ b/error_buffer.go @@ -0,0 +1,39 @@ +package roadrunner + +import ( + "bytes" + "sync" +) + +// thread safe errBuffer +type errBuffer struct { + mu sync.Mutex + buffer *bytes.Buffer +} + +// Len returns the number of bytes of the unread portion of the errBuffer; +// b.Len() == len(b.Bytes()). +func (b *errBuffer) Len() int { + b.mu.Lock() + defer b.mu.Unlock() + + return b.buffer.Len() +} + +// Write appends the contents of p to the errBuffer, growing the errBuffer as +// needed. The return value n is the length of p; err is always nil. If the +// errBuffer becomes too large, Write will panic with ErrTooLarge. +func (b *errBuffer) Write(p []byte) (n int, err error) { + b.mu.Lock() + defer b.mu.Unlock() + + return b.buffer.Write(p) +} + +// Strings fetches all errBuffer data into string. +func (b *errBuffer) String() string { + b.mu.Lock() + defer b.mu.Unlock() + + return b.buffer.String() +} |