Skip to content

Instantly share code, notes, and snippets.

@wallyqs
Last active April 7, 2021 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wallyqs/fbe9a153d0f46229bd117ec74f3f778b to your computer and use it in GitHub Desktop.
Save wallyqs/fbe9a153d0f46229bd117ec74f3f778b to your computer and use it in GitHub Desktop.
NATS Context Option
func ExampleContext() {
nc, err := nats.Connect("localhost")
if err != nil {
log.Fatal(err)
}
// Base context
ctx := nats.Context(context.Background())
js, err := nc.JetStream()
if err != nil {
log.Fatal(err)
}
// New context from base context.
ctx2, cancel := context.WithCancel(ctx)
defer cancel()
js.AddStream(&nats.StreamConfig{Name: "foo"}, nats.Context(ctx2))
ctx3, cancel := context.WithTimeout(ctx2, 2*time.Second)
defer cancel()
js.Publish("foo", []byte("Hello JS!"), nats.Context(ctx3))
// Output:
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment