github.com/cloudfoundry-attic/garden-linux@v0.333.2-candidate/process_tracker/writer/fan_out.go (about)

     1  package writer
     2  
     3  import (
     4  	"io"
     5  	"sync"
     6  )
     7  
     8  type FanOut interface {
     9  	Write(data []byte) (int, error)
    10  	AddSink(sink io.Writer)
    11  }
    12  
    13  func NewFanOut() FanOut {
    14  	return &fanOut{}
    15  }
    16  
    17  type fanOut struct {
    18  	sinks  []io.Writer
    19  	sinksL sync.Mutex
    20  }
    21  
    22  func (w *fanOut) Write(data []byte) (int, error) {
    23  	w.sinksL.Lock()
    24  	defer w.sinksL.Unlock()
    25  
    26  	// the sinks should be nonblocking and never actually error;
    27  	// we can assume lossiness here, and do this all within the lock
    28  	for _, s := range w.sinks {
    29  		s.Write(data)
    30  	}
    31  
    32  	return len(data), nil
    33  }
    34  
    35  func (w *fanOut) AddSink(sink io.Writer) {
    36  	w.sinksL.Lock()
    37  	defer w.sinksL.Unlock()
    38  
    39  	w.sinks = append(w.sinks, sink)
    40  }