You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Useful for stuff like storing logs without memory going overboard, or in cases
when you have multiple readers reading a single stream of data from a single
writer.
This code is thread safe and allows either standard readers, or blocking
readers that will wait when no more data is available (this uses
sync.Cond with RLocker
meaning multiple readers will resume reading in parallel, allowing
the most out of goroutines).
Originally this was written as a buffer for log
allowing to redirect output from logs to multiple targets at the same time
(write to file and stdout at the same time) while also allowing a web API
to fetch the last X MB of entries (as large as the buffer is).
l, err:=ringbuf.New(1024*1024) // 1MB ring buffer for logsiferr!=nil {
...
}
log.SetOutput(l)
// output log to stdout// (duplicate this to also output to files/etc)gofunc() {
r:=l.BlockingReader()
deferr.Close()
io.Copy(os.Stdout, r)
}()
funcdmesg() ([]byte, error) {
// return up to last 1MB of log entriesr:=l.Reader()
deferr.Close()
returnioutil.ReadAll(r)
}
About
Yet another Go ringbuffer implementation (one with readers)