-
Notifications
You must be signed in to change notification settings - Fork 219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement nats_jetstream using new jetstream package #1083
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import ( | |
"github.com/cloudevents/sdk-go/v2/protocol" | ||
|
||
"github.com/nats-io/nats.go" | ||
"github.com/nats-io/nats.go/jetstream" | ||
) | ||
|
||
// Protocol is a reference implementation for using the CloudEvents binding | ||
|
@@ -46,6 +47,24 @@ func NewProtocol(url, stream, sendSubject, receiveSubject string, natsOpts []nat | |
return p, nil | ||
} | ||
|
||
// NewProtocolV2 creates a new NATS protocol. | ||
func NewProtocolV2(ctx context.Context, url, stream, sendSubject string, natsOpts []nats.Option, jsOpts []jetstream.JetStreamOpt, opts ...ProtocolOption) (*Protocol, error) { | ||
conn, err := nats.Connect(url, natsOpts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
p, err := NewProtocolFromConnV2(ctx, conn, stream, sendSubject, jsOpts, opts...) | ||
if err != nil { | ||
conn.Close() | ||
return nil, err | ||
} | ||
|
||
p.connOwned = true | ||
|
||
return p, nil | ||
} | ||
|
||
func NewProtocolFromConn(conn *nats.Conn, stream, sendSubject, receiveSubject string, jsOpts []nats.JSOpt, subOpts []nats.SubOpt, opts ...ProtocolOption) (*Protocol, error) { | ||
var err error | ||
p := &Protocol{ | ||
|
@@ -67,6 +86,36 @@ func NewProtocolFromConn(conn *nats.Conn, stream, sendSubject, receiveSubject st | |
return p, nil | ||
} | ||
|
||
func NewProtocolFromConnV2(ctx context.Context, conn *nats.Conn, stream, sendSubject string, jsOpts []jetstream.JetStreamOpt, opts ...ProtocolOption) (*Protocol, error) { | ||
var err error | ||
var js jetstream.JetStream | ||
Comment on lines
+90
to
+91
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to define those? Is it because of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is because of the combined "if"+"assignment" on line 100. The |
||
p := &Protocol{ | ||
Conn: conn, | ||
} | ||
|
||
if err := p.applyOptions(opts...); err != nil { | ||
return nil, err | ||
} | ||
|
||
if js, err = jetstream.New(conn, jsOpts...); err != nil { | ||
return nil, err | ||
} | ||
streamConfig := jetstream.StreamConfig{Name: stream, Subjects: []string{sendSubject}} | ||
if _, err := js.CreateOrUpdateStream(ctx, streamConfig); err != nil { | ||
return nil, err | ||
} | ||
|
||
if p.Consumer, err = NewConsumerFromConnV2(ctx, conn, jsOpts, p.consumerOptions...); err != nil { | ||
return nil, err | ||
} | ||
|
||
if p.Sender, err = NewSenderFromConnV2(ctx, conn, sendSubject, jsOpts, p.senderOptions...); err != nil { | ||
return nil, err | ||
} | ||
|
||
return p, nil | ||
} | ||
|
||
// Send implements Sender.Send | ||
func (p *Protocol) Send(ctx context.Context, in binding.Message, transformers ...binding.Transformer) error { | ||
return p.Sender.Send(ctx, in, transformers...) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: do we need all those typed errors? They're now part of the API. Also, in those cases a more simple pattern is to use an error struct, for example https://github.com/aws-controllers-k8s/eventbridge-controller/blob/67a9375a6ac7f95f1a3eb8b1b49f329755305c24/pkg/resource/rule/hooks.go#L29-L43
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, with
errors.As
, using simple exported errors works rather than needing to do something more complicated unless you want additional methods.