| 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 |
// Add a per-subject ceiling so one noisy subject can't evict another's |
| 31 |
// messages. MaxMsgsPerSubject keeps the most recent N messages for every |
| 32 |
// subject independently, alongside the whole-stream limits. |
| 33 |
s, err := js.Stream(ctx, "ORDERS") |
| 34 |
if err != nil { |
| 35 |
panic(err) |
| 36 |
} |
| 37 |
cfg := s.CachedInfo().Config |
| 38 |
cfg.MaxMsgsPerSubject = 100000 |
| 39 |
if _, err := js.UpdateStream(ctx, cfg); err != nil { |
| 40 |
panic(err) |
| 41 |
} |
| 42 |
fmt.Println("ORDERS now keeps 100000 messages per subject") |
| 43 |
// NATS-DOC-END |
| 44 |
} |