init project
This commit is contained in:
commit
d63cc8d9f5
33 files changed
+1529
No files matched your search
@@ -0,0 +1,58 @@
|
||||
package jetstream
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
nats "github.com/nats-io/nats.go"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Addr string `json:"addr"`
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
type JetStream struct {
|
||||
nats.JetStreamContext
|
||||
nc *nats.Conn
|
||||
}
|
||||
|
||||
func NewJetStreamBroker(conf Config) (*JetStream, error) {
|
||||
nc, err := nats.Connect(conf.Addr, nats.Token(conf.Token),
|
||||
nats.DisconnectErrHandler(func(conn *nats.Conn, err error) {
|
||||
log.Errorf("jetStream disconnect: %v", err)
|
||||
}),
|
||||
nats.ReconnectHandler(func(conn *nats.Conn) {
|
||||
log.Infof("jetStream reconnect")
|
||||
}),
|
||||
nats.ClosedHandler(func(conn *nats.Conn) {
|
||||
log.Infof("jetStream closed")
|
||||
}),
|
||||
nats.DiscoveredServersHandler(func(conn *nats.Conn) {
|
||||
log.Infof("jetStream discovered")
|
||||
}),
|
||||
nats.ErrorHandler(func(conn *nats.Conn, subscription *nats.Subscription, e error) {
|
||||
subDesc := ""
|
||||
if subscription != nil {
|
||||
subDesc = fmt.Sprintf(" subject:%s queue:%s", subscription.Subject, subscription.Queue)
|
||||
}
|
||||
log.Errorf("jetStream%s err:%v", subDesc, e)
|
||||
}),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
js, err := nc.JetStream()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Infof("jetStream Addr: %s", conf.Addr)
|
||||
return &JetStream{
|
||||
nc: nc,
|
||||
JetStreamContext: js,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *JetStream) Close() {
|
||||
c.nc.Close()
|
||||
}
|
||||
Reference in new issue
Block a user