/
publisher.go
51 lines (41 loc) · 1.17 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
package nats
import (
"fmt"
"github.com/nats-io/stan.go"
"github.com/sirupsen/logrus"
"github.com/teamlint/pg-flow/config"
"github.com/teamlint/pg-flow/event"
)
const (
ErrNatsConnection = "nats connection error"
)
//go:generate easyjson nats_publisher.go
// NatsPublisher represent event publisher.
type NatsPublisher struct {
conn stan.Conn
}
// Close NATS connection.
func (n NatsPublisher) Close() error {
return n.conn.Close()
}
// Publish serializes the event and publishes it on the bus.
func (n NatsPublisher) Publish(subject string, evt *event.Event) error {
msg, err := evt.MarshalJSON()
if err != nil {
return fmt.Errorf("marshal err: %w", err)
}
return n.conn.Publish(subject, msg)
}
// New return new NatsPublisher instance.
func New(conn stan.Conn) *NatsPublisher {
return &NatsPublisher{conn: conn}
}
// Register 注册 NATS 事件发布器
func Register(cfg *config.Config) {
// nats publisher
sc, err := stan.Connect(cfg.Publisher.ClusterID, cfg.Publisher.ClientID, stan.NatsURL(cfg.Publisher.Address))
if err != nil {
logrus.WithError(err).WithField("publisher.Register", "nats").Fatalln(ErrNatsConnection)
}
event.RegisterPublisher("nats", New(sc))
}