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

Expose nats.Context option for nats.KeyWatcher #904

Merged
merged 3 commits into from Mar 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion kv.go
Expand Up @@ -93,6 +93,8 @@ type KeyValueStatus interface {

// KeyWatcher is what is returned when doing a watch.
type KeyWatcher interface {
// Context returns watcher context optionally provided by nats.Context option.
Context() context.Context
// Updates returns a channel to read any updates to entries.
Updates() <-chan KeyValueEntry
// Stop will stop this watcher.
Expand Down Expand Up @@ -625,6 +627,15 @@ type watcher struct {
initDone bool
initPending uint64
received uint64
ctx context.Context
}

// Context returns the context for the watcher if set.
func (w *watcher) Context() context.Context {
if w == nil {
return nil
}
return w.ctx
}

// Updates returns the interior channel.
Expand Down Expand Up @@ -667,7 +678,7 @@ func (kv *kvs) Watch(keys string, opts ...WatchOpt) (KeyWatcher, error) {
keys = b.String()

// We will block below on placing items on the chan. That is by design.
w := &watcher{updates: make(chan KeyValueEntry, 256)}
w := &watcher{updates: make(chan KeyValueEntry, 256), ctx: o.ctx}

update := func(m *Msg) {
tokens, err := getMetadataFields(m.Reply)
Expand Down