-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add jetstream publisher, reuse nats connection
* closes #31
- Loading branch information
1 parent
b1fd228
commit 7b159bb
Showing
14 changed files
with
321 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package jetstream | ||
|
||
import ( | ||
"errors" | ||
"log/slog" | ||
"time" | ||
|
||
"github.com/nats-io/nats.go" | ||
) | ||
|
||
type ( | ||
JetStreamOpts struct { | ||
Logg *slog.Logger | ||
JetStreamID string | ||
Endpoint string | ||
PersistDuration time.Duration | ||
} | ||
|
||
JetStream struct { | ||
JSCtx nats.JetStreamContext | ||
JSSub *nats.Subscription | ||
|
||
natsConn *nats.Conn | ||
logg *slog.Logger | ||
} | ||
) | ||
|
||
const ( | ||
PushStream = "CUSTODIAL" | ||
|
||
pullStream = "TRACKER" | ||
pullSubject = "TRACKER.*" | ||
) | ||
|
||
var pushStreamSubjects = []string{ | ||
"CUSTODIAL.*", | ||
} | ||
|
||
func NewJetStream(o JetStreamOpts) (*JetStream, error) { | ||
natsConn, err := nats.Connect(o.Endpoint) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
js, err := natsConn.JetStream() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
stream, err := js.StreamInfo(PushStream) | ||
if err != nil && !errors.Is(err, nats.ErrStreamNotFound) { | ||
return nil, err | ||
} | ||
if stream == nil { | ||
_, err := js.AddStream(&nats.StreamConfig{ | ||
Name: PushStream, | ||
MaxAge: o.PersistDuration, | ||
Storage: nats.FileStorage, | ||
Subjects: pushStreamSubjects, | ||
Duplicates: time.Minute, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
o.Logg.Info("successfully created NATS JetStream stream", "stream_name", PushStream) | ||
} | ||
|
||
sub, err := js.PullSubscribe(pullSubject, o.JetStreamID, nats.AckExplicit()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &JetStream{ | ||
JSCtx: js, | ||
JSSub: sub, | ||
natsConn: natsConn, | ||
logg: o.Logg, | ||
}, nil | ||
} | ||
|
||
func (s *JetStream) Close() { | ||
if s.natsConn != nil { | ||
s.natsConn.Close() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package pub | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/grassrootseconomics/eth-custodial/internal/jetstream" | ||
"github.com/grassrootseconomics/eth-custodial/pkg/event" | ||
"github.com/nats-io/nats.go" | ||
) | ||
|
||
type ( | ||
PubOpts struct { | ||
JSCtx nats.JetStreamContext | ||
} | ||
|
||
Pub struct { | ||
jsCtx nats.JetStreamContext | ||
} | ||
) | ||
|
||
func NewPub(o PubOpts) *Pub { | ||
return &Pub{ | ||
jsCtx: o.JSCtx, | ||
} | ||
} | ||
|
||
func (p *Pub) Send(_ context.Context, payload event.Event) error { | ||
data, err := payload.Serialize() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = p.jsCtx.Publish( | ||
fmt.Sprintf("%s.%s", jetstream.PushStream, payload.TrackingID), | ||
data, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.