Skip to content
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

feat(go)!: allow serving with queue group #259

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions go/nats/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,17 @@ func subscribe(conn *nats.Conn, prefix string, f func(context.Context, []byte),
}

type Client struct {
conn *nats.Conn
prefix string
conn *nats.Conn
prefix string
queueGroup *string
}

func NewClient(conn *nats.Conn, prefix string) *Client {
return &Client{conn, prefix}
return &Client{conn: conn, prefix: prefix, queueGroup: nil}
}

func NewClientWithQueueGroup(conn *nats.Conn, prefix string, queueGroup string) *Client {
return &Client{conn, prefix, &queueGroup}
}

type paramWriter struct {
Expand Down Expand Up @@ -350,7 +355,7 @@ func (c *Client) Invoke(ctx context.Context, instance string, name string, f fun
}

func (c *Client) Serve(instance string, name string, f func(context.Context, wrpc.IndexWriter, wrpc.IndexReadCloser) error, subs ...wrpc.SubscribePath) (stop func() error, err error) {
sub, err := c.conn.Subscribe(invocationSubject(c.prefix, instance, name), func(m *nats.Msg) {
serveFunction := func(m *nats.Msg) {
ctx := context.Background()
ctx = ContextWithHeader(ctx, m.Header)

Expand Down Expand Up @@ -413,7 +418,16 @@ func (c *Client) Serve(instance string, name string, f func(context.Context, wrp
return
}
slog.Debug("successfully finished serving invocation")
})
}
var sub *nats.Subscription
if c.queueGroup != nil {
slog.Debug("serving with queue group", "instance", instance, "name", name, "group", *c.queueGroup)
sub, err = c.conn.QueueSubscribe(invocationSubject(c.prefix, instance, name), *c.queueGroup, serveFunction)
} else {
slog.Debug("serving", "instance", instance, "name", name)
sub, err = c.conn.Subscribe(invocationSubject(c.prefix, instance, name), serveFunction)
}

if err != nil {
return nil, fmt.Errorf("failed to serve `%s` for instance `%s`: %w", name, instance, err)
}
Expand Down
Loading