Debian Codegraph @

examples/docs/learn-jetstream-get-direct-enable/main.go

raw
1 package main
2
3 import (
4 "context"
5 "fmt"
6 "time"
7
8 "github.com/nats-io/nats.go"
9 "github.com/nats-io/nats.go/jetstream"
10 )
11
12 func main() {
13 // Connect to the NATS server.
14 nc, err := nats.Connect("nats://localhost:4222")
15 if err != nil {
16 panic(err)
17 }
18 defer nc.Close()
19
20 // Create a JetStream context (the stream manager).
21 js, err := jetstream.New(nc)
22 if err != nil {
23 panic(err)
24 }
25
26 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
27 defer cancel()
28
29 // Look up the ORDERS stream.
30 stream, err := js.Stream(ctx, "ORDERS")
31 if err != nil {
32 panic(err)
33 }
34
35 // NATS-DOC-START
36 // Read the current config, turn on direct access, and push the update.
37 // AllowDirect lets any replica serve single-message gets.
38 info, err := stream.Info(ctx)
39 if err != nil {
40 panic(err)
41 }
42
43 cfg := info.Config
44 cfg.AllowDirect = true
45
46 updated, err := js.UpdateStream(ctx, cfg)
47 if err != nil {
48 panic(err)
49 }
50
51 fmt.Printf("AllowDirect: %v\n", updated.CachedInfo().Config.AllowDirect)
52 // NATS-DOC-END
53 }