Debian Codegraph @

examples/docs/learn-jetstream-mirrors-and-sources-mirrorLag/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 // NATS-DOC-START
30 // A mirror is eventually consistent. Read its Lag before trusting it to
31 // hold what the upstream just received: 0 means fully caught up.
32 stream, err := js.Stream(ctx, "ORDERS-ARCHIVE")
33 if err != nil {
34 panic(err)
35 }
36
37 info, err := stream.Info(ctx)
38 if err != nil {
39 panic(err)
40 }
41
42 fmt.Printf("Upstream: %s\n", info.Mirror.Name)
43 fmt.Printf("Lag: %d\n", info.Mirror.Lag)
44 fmt.Printf("Last seen: %s ago\n", info.Mirror.Active)
45 // NATS-DOC-END
46 }