Last active
April 7, 2021 16:06
-
-
Save wallyqs/fbe9a153d0f46229bd117ec74f3f778b to your computer and use it in GitHub Desktop.
NATS Context Option
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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