Debian Codegraph @

examples/docs/learn-jetstream-worker-pool-max-pending/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.
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 // Raise MaxAckPending so a larger pool can hold more orders in progress at
31 // once. The cap is shared across the whole "shipping" consumer, not per
32 // worker, so size it to at least your worker count. CreateOrUpdateConsumer
33 // updates the existing consumer in place.
34 _, err = js.CreateOrUpdateConsumer(ctx, "ORDERS", jetstream.ConsumerConfig{
35 Durable: "shipping",
36 AckPolicy: jetstream.AckExplicitPolicy,
37 MaxAckPending: 5000,
38 })
39 if err != nil {
40 panic(err)
41 }
42 fmt.Println("shipping MaxAckPending set to 5000")
43 // NATS-DOC-END
44 }