From e0e03e37422898b7ef8ce59cce34a192f304d63c Mon Sep 17 00:00:00 2001 From: Matthew DeVenny Date: Fri, 18 Mar 2022 06:27:11 -0700 Subject: [PATCH] Expose nats.Context option for nats.KeyWatcher (#904) * Expose KeyWatcher context to facilitate KeyValue interface extension Signed-off-by: Matthew DeVenny * Expose KeyWatcher context to facilitate KeyValue interface extension Signed-off-by: Matthew DeVenny * Rename GetContext to Context Signed-off-by: Matthew DeVenny --- kv.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/kv.go b/kv.go index aff84e487..5ba116027 100644 --- a/kv.go +++ b/kv.go @@ -94,6 +94,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. @@ -743,6 +745,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. @@ -785,7 +796,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)