2016-03-23 08:21:16 +03:00
|
|
|
package events
|
|
|
|
|
2016-08-20 00:52:11 +03:00
|
|
|
import (
|
2016-08-26 00:05:09 +03:00
|
|
|
"fmt"
|
2016-08-20 00:52:11 +03:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
)
|
2016-03-23 08:21:16 +03:00
|
|
|
|
|
|
|
// Broadcaster sends events to multiple, reliable Sinks. The goal of this
|
|
|
|
// component is to dispatch events to configured endpoints. Reliability can be
|
|
|
|
// provided by wrapping incoming sinks.
|
|
|
|
type Broadcaster struct {
|
|
|
|
sinks []Sink
|
|
|
|
events chan Event
|
|
|
|
adds chan configureRequest
|
|
|
|
removes chan configureRequest
|
2016-08-20 00:52:11 +03:00
|
|
|
|
|
|
|
shutdown chan struct{}
|
|
|
|
closed chan struct{}
|
|
|
|
once sync.Once
|
2016-03-23 08:21:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBroadcaster appends one or more sinks to the list of sinks. The
|
|
|
|
// broadcaster behavior will be affected by the properties of the sink.
|
|
|
|
// Generally, the sink should accept all messages and deal with reliability on
|
|
|
|
// its own. Use of EventQueue and RetryingSink should be used here.
|
|
|
|
func NewBroadcaster(sinks ...Sink) *Broadcaster {
|
|
|
|
b := Broadcaster{
|
2016-08-20 00:52:11 +03:00
|
|
|
sinks: sinks,
|
|
|
|
events: make(chan Event),
|
|
|
|
adds: make(chan configureRequest),
|
|
|
|
removes: make(chan configureRequest),
|
|
|
|
shutdown: make(chan struct{}),
|
|
|
|
closed: make(chan struct{}),
|
2016-03-23 08:21:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start the broadcaster
|
|
|
|
go b.run()
|
|
|
|
|
|
|
|
return &b
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write accepts an event to be dispatched to all sinks. This method will never
|
|
|
|
// fail and should never block (hopefully!). The caller cedes the memory to the
|
|
|
|
// broadcaster and should not modify it after calling write.
|
|
|
|
func (b *Broadcaster) Write(event Event) error {
|
|
|
|
select {
|
|
|
|
case b.events <- event:
|
|
|
|
case <-b.closed:
|
|
|
|
return ErrSinkClosed
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the sink to the broadcaster.
|
|
|
|
//
|
|
|
|
// The provided sink must be comparable with equality. Typically, this just
|
|
|
|
// works with a regular pointer type.
|
|
|
|
func (b *Broadcaster) Add(sink Sink) error {
|
|
|
|
return b.configure(b.adds, sink)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the provided sink.
|
|
|
|
func (b *Broadcaster) Remove(sink Sink) error {
|
|
|
|
return b.configure(b.removes, sink)
|
|
|
|
}
|
|
|
|
|
|
|
|
type configureRequest struct {
|
|
|
|
sink Sink
|
|
|
|
response chan error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Broadcaster) configure(ch chan configureRequest, sink Sink) error {
|
|
|
|
response := make(chan error, 1)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case ch <- configureRequest{
|
|
|
|
sink: sink,
|
|
|
|
response: response}:
|
|
|
|
ch = nil
|
|
|
|
case err := <-response:
|
|
|
|
return err
|
|
|
|
case <-b.closed:
|
|
|
|
return ErrSinkClosed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the broadcaster, ensuring that all messages are flushed to the
|
|
|
|
// underlying sink before returning.
|
|
|
|
func (b *Broadcaster) Close() error {
|
2016-08-20 00:52:11 +03:00
|
|
|
b.once.Do(func() {
|
|
|
|
close(b.shutdown)
|
|
|
|
})
|
|
|
|
|
|
|
|
<-b.closed
|
|
|
|
return nil
|
2016-03-23 08:21:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// run is the main broadcast loop, started when the broadcaster is created.
|
|
|
|
// Under normal conditions, it waits for events on the event channel. After
|
|
|
|
// Close is called, this goroutine will exit.
|
|
|
|
func (b *Broadcaster) run() {
|
2016-08-20 00:52:11 +03:00
|
|
|
defer close(b.closed)
|
2016-03-23 08:21:16 +03:00
|
|
|
remove := func(target Sink) {
|
|
|
|
for i, sink := range b.sinks {
|
|
|
|
if sink == target {
|
|
|
|
b.sinks = append(b.sinks[:i], b.sinks[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-b.events:
|
|
|
|
for _, sink := range b.sinks {
|
|
|
|
if err := sink.Write(event); err != nil {
|
|
|
|
if err == ErrSinkClosed {
|
|
|
|
// remove closed sinks
|
|
|
|
remove(sink)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
logrus.WithField("event", event).WithField("events.sink", sink).WithError(err).
|
|
|
|
Errorf("broadcaster: dropping event")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case request := <-b.adds:
|
|
|
|
// while we have to iterate for add/remove, common iteration for
|
|
|
|
// send is faster against slice.
|
|
|
|
|
|
|
|
var found bool
|
|
|
|
for _, sink := range b.sinks {
|
|
|
|
if request.sink == sink {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
b.sinks = append(b.sinks, request.sink)
|
|
|
|
}
|
|
|
|
// b.sinks[request.sink] = struct{}{}
|
|
|
|
request.response <- nil
|
|
|
|
case request := <-b.removes:
|
|
|
|
remove(request.sink)
|
|
|
|
request.response <- nil
|
2016-08-20 00:52:11 +03:00
|
|
|
case <-b.shutdown:
|
2016-03-23 08:21:16 +03:00
|
|
|
// close all the underlying sinks
|
|
|
|
for _, sink := range b.sinks {
|
|
|
|
if err := sink.Close(); err != nil && err != ErrSinkClosed {
|
|
|
|
logrus.WithField("events.sink", sink).WithError(err).
|
|
|
|
Errorf("broadcaster: closing sink failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-26 00:05:09 +03:00
|
|
|
|
|
|
|
func (b Broadcaster) String() string {
|
|
|
|
// Serialize copy of this broadcaster without the sync.once, to avoid
|
|
|
|
// a data race.
|
|
|
|
|
|
|
|
b2 := Broadcaster{
|
|
|
|
sinks: b.sinks,
|
|
|
|
events: b.events,
|
|
|
|
adds: b.adds,
|
|
|
|
removes: b.removes,
|
|
|
|
|
|
|
|
shutdown: b.shutdown,
|
|
|
|
closed: b.closed,
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprint(b2)
|
|
|
|
}
|