| 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 |
// Cap ORDERS so it can't grow without bound. Fetch the current config, add |
| 31 |
// a seven-day age limit and a 1 GiB byte ceiling, and update the stream in |
| 32 |
// place. Editing limits leaves the messages already stored alone. |
| 33 |
s, err := js.Stream(ctx, "ORDERS") |
| 34 |
if err != nil { |
| 35 |
panic(err) |
| 36 |
} |
| 37 |
cfg := s.CachedInfo().Config |
| 38 |
cfg.MaxAge = 7 * 24 * time.Hour |
| 39 |
cfg.MaxBytes = 1 << 30 // 1 GiB |
| 40 |
if _, err := js.UpdateStream(ctx, cfg); err != nil { |
| 41 |
panic(err) |
| 42 |
} |
| 43 |
fmt.Println("ORDERS capped at 7d age and 1 GiB") |
| 44 |
// NATS-DOC-END |
| 45 |
} |