diff --git a/fanout_router.go b/fanout_router.go new file mode 100644 index 0000000..dae08c2 --- /dev/null +++ b/fanout_router.go @@ -0,0 +1,40 @@ +package flog + + +import ( + "github.com/reiver/go-manyerrors" +) + + +// NewFanoutRouter returns an initialized FanoutRouter. +func NewFanoutRouter(subrouters ...Router) *FanoutRouter { + router := FanoutRouter{ + subrouters:subrouters, + } + + return &router +} + + +// FanoutRouter is a Router that re-routes any message (and its context) it +// receives to all of its sub-routers. +type FanoutRouter struct { + subrouters []Router +} + + +func (router *FanoutRouter) Route(message string, context map[string]interface{}) error { + errors := []error{} + + for _, subrouter := range router.subrouters { + if err := subrouter.Route(message, context); nil != err { + errors = append(errors, err) + } + } + + if 0 < len(errors) { + return manyerrors.New(errors...) + } + + return nil +} diff --git a/fanout_router_test.go b/fanout_router_test.go new file mode 100644 index 0000000..432ddfc --- /dev/null +++ b/fanout_router_test.go @@ -0,0 +1,350 @@ +package flog + + +import ( + "testing" + + "github.com/reiver/go-manyerrors" + + "fmt" + "math/rand" + "time" +) + + +func TestNewFanoutRouter(t *testing.T) { + + randomness := rand.New(rand.NewSource( time.Now().UTC().UnixNano() )) + + + router := NewFanoutRouter() + if nil == router { + t.Errorf("After trying to create a discard router, expected it to be not nil, but was: %v", router) + } + + + message := fmt.Sprint("%x", randomness.Int63n(9999999999)) + + context := make(map[string]interface{}) + limit := randomness.Int63n(30) + for i:=int64(0); i