2014-07-30 15:16:10 +00:00
|
|
|
package broadcastwriter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// BroadcastWriter accumulate multiple io.WriteCloser by stream.
|
|
|
|
type BroadcastWriter struct {
|
|
|
|
sync.Mutex
|
2015-07-09 16:00:03 +00:00
|
|
|
writers map[io.WriteCloser]struct{}
|
2014-07-30 15:16:10 +00:00
|
|
|
}
|
|
|
|
|
2015-07-09 16:00:03 +00:00
|
|
|
// AddWriter adds new io.WriteCloser.
|
|
|
|
func (w *BroadcastWriter) AddWriter(writer io.WriteCloser) {
|
2014-07-30 15:16:10 +00:00
|
|
|
w.Lock()
|
2015-07-09 16:00:03 +00:00
|
|
|
w.writers[writer] = struct{}{}
|
2014-07-30 15:16:10 +00:00
|
|
|
w.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write writes bytes to all writers. Failed writers will be evicted during
|
|
|
|
// this call.
|
|
|
|
func (w *BroadcastWriter) Write(p []byte) (n int, err error) {
|
|
|
|
w.Lock()
|
2015-07-09 16:00:03 +00:00
|
|
|
for sw := range w.writers {
|
|
|
|
if n, err := sw.Write(p); err != nil || n != len(p) {
|
|
|
|
// On error, evict the writer
|
|
|
|
delete(w.writers, sw)
|
2015-03-30 21:30:01 +00:00
|
|
|
}
|
2014-07-30 15:16:10 +00:00
|
|
|
}
|
|
|
|
w.Unlock()
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean closes and removes all writers. Last non-eol-terminated part of data
|
|
|
|
// will be saved.
|
|
|
|
func (w *BroadcastWriter) Clean() error {
|
|
|
|
w.Lock()
|
2015-07-09 16:00:03 +00:00
|
|
|
for w := range w.writers {
|
|
|
|
w.Close()
|
2014-07-30 15:16:10 +00:00
|
|
|
}
|
2015-07-09 16:00:03 +00:00
|
|
|
w.writers = make(map[io.WriteCloser]struct{})
|
2014-07-30 15:16:10 +00:00
|
|
|
w.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-03 01:45:05 +00:00
|
|
|
// New creates a new BroadcastWriter.
|
2014-07-30 15:16:10 +00:00
|
|
|
func New() *BroadcastWriter {
|
|
|
|
return &BroadcastWriter{
|
2015-07-09 16:00:03 +00:00
|
|
|
writers: make(map[io.WriteCloser]struct{}),
|
2014-07-30 15:16:10 +00:00
|
|
|
}
|
|
|
|
}
|