-
Notifications
You must be signed in to change notification settings - Fork 0
/
publisher.go
52 lines (46 loc) 路 1.07 KB
/
publisher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package once
import (
"context"
"io"
nc "github.com/geniusrabbit/notificationcenter"
)
// PublisherWrapper provides additional check before send message to the stream
type PublisherWrapper struct {
checker Checker
pub nc.Publisher
}
// MewPublisherWrapper with checker
func MewPublisherWrapper(pub nc.Publisher, checker Checker) *PublisherWrapper {
if pub == nil {
panic("publisher is required")
}
if checker == nil {
panic("check is required")
}
return &PublisherWrapper{
pub: pub,
checker: checker,
}
}
// Publish one or more messages to the pub-service if will pass conditions
func (wr *PublisherWrapper) Publish(ctx context.Context, messages ...interface{}) error {
for _, msg := range messages {
if wr.checker.IsSkip(msg) {
continue
}
if err := wr.pub.Publish(ctx, msg); err != nil {
return err
}
if err := wr.checker.MarkAsSent(msg); err != nil {
return err
}
}
return nil
}
// Close publisher if supported
func (wr *PublisherWrapper) Close() error {
if closer, ok := wr.pub.(io.Closer); ok {
return closer.Close()
}
return nil
}