github.com/argoproj/argo-events@v1.9.1/eventbus/jetstream/eventsource/source_conn.go (about) 1 package eventsource 2 3 import ( 4 "context" 5 "fmt" 6 7 eventbuscommon "github.com/argoproj/argo-events/eventbus/common" 8 jetstreambase "github.com/argoproj/argo-events/eventbus/jetstream/base" 9 nats "github.com/nats-io/nats.go" 10 ) 11 12 type JetstreamSourceConn struct { 13 *jetstreambase.JetstreamConnection 14 eventSourceName string 15 } 16 17 func CreateJetstreamSourceConn(conn *jetstreambase.JetstreamConnection, eventSourceName string) *JetstreamSourceConn { 18 return &JetstreamSourceConn{ 19 conn, eventSourceName, 20 } 21 } 22 23 func (jsc *JetstreamSourceConn) Publish(ctx context.Context, 24 msg eventbuscommon.Message) error { 25 if jsc == nil { 26 return fmt.Errorf("Publish() failed; JetstreamSourceConn is nil") 27 } 28 29 // exactly once on the publishing side is done by assigning a "deduplication key" to the message 30 dedupKey := nats.MsgId(msg.ID) 31 32 // derive subject from event source name and event name 33 subject := fmt.Sprintf("default.%s.%s", msg.EventSourceName, msg.EventName) 34 _, err := jsc.JSContext.Publish(subject, msg.Body, dedupKey) 35 jsc.Logger.Debugf("published message to subject %s", subject) 36 return err 37 } 38 39 func (conn *JetstreamSourceConn) IsClosed() bool { 40 return conn == nil || conn.JetstreamConnection.IsClosed() 41 } 42 43 func (conn *JetstreamSourceConn) Close() error { 44 if conn == nil { 45 return fmt.Errorf("can't close Jetstream source connection, JetstreamSourceConn is nil") 46 } 47 return conn.JetstreamConnection.Close() 48 }