go-log/fanout_router.go

41 lines
753 B
Go
Raw Normal View History

2015-10-10 21:45:21 +00:00
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
}