go-zero/core/queue/multipusher.go

36 lines
770 B
Go
Raw Normal View History

2020-08-13 17:00:53 +08:00
package queue
import "github.com/zeromicro/go-zero/core/errorx"
2020-08-13 17:00:53 +08:00
2021-02-22 16:38:42 +08:00
// A MultiPusher is a pusher that can push messages to multiple underlying pushers.
2020-08-16 23:07:21 +08:00
type MultiPusher struct {
2020-08-13 17:00:53 +08:00
name string
pushers []Pusher
}
2021-02-22 16:38:42 +08:00
// NewMultiPusher returns a MultiPusher.
2020-08-16 23:07:21 +08:00
func NewMultiPusher(pushers []Pusher) Pusher {
return &MultiPusher{
2020-08-13 17:00:53 +08:00
name: generateName(pushers),
pushers: pushers,
}
}
2021-02-22 16:38:42 +08:00
// Name returns the name of pusher.
2020-08-16 23:07:21 +08:00
func (pusher *MultiPusher) Name() string {
2020-08-13 17:00:53 +08:00
return pusher.name
}
2021-02-22 16:38:42 +08:00
// Push pushes a message into the underlying queue.
2020-08-16 23:07:21 +08:00
func (pusher *MultiPusher) Push(message string) error {
2020-08-13 17:00:53 +08:00
var batchError errorx.BatchError
for _, each := range pusher.pushers {
if err := each.Push(message); err != nil {
batchError.Add(err)
}
}
return batchError.Err()
}