| 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 |
// Make sure the FULFILLMENT WorkQueue stream exists. |
| 30 |
if _, err := js.CreateOrUpdateStream(ctx, jetstream.StreamConfig{ |
| 31 |
Name: "FULFILLMENT", |
| 32 |
Subjects: []string{"fulfill.>"}, |
| 33 |
Retention: jetstream.WorkQueuePolicy, |
| 34 |
}); err != nil { |
| 35 |
panic(err) |
| 36 |
} |
| 37 |
|
| 38 |
// NATS-DOC-START |
| 39 |
// Load FULFILLMENT and take its current config. |
| 40 |
stream, err := js.Stream(ctx, "FULFILLMENT") |
| 41 |
if err != nil { |
| 42 |
panic(err) |
| 43 |
} |
| 44 |
cfg := stream.CachedInfo().Config |
| 45 |
|
| 46 |
// Try to turn the WorkQueue stream into a Limits stream by flipping |
| 47 |
// retention and pushing the update back. |
| 48 |
cfg.Retention = jetstream.LimitsPolicy |
| 49 |
if _, err := js.UpdateStream(ctx, cfg); err != nil { |
| 50 |
// The server refuses: a stream's retention policy is fixed for its |
| 51 |
// lifetime (err 10052: stream configuration update can not change |
| 52 |
// retention policy to/from workqueue). To change it, recreate the stream. |
| 53 |
fmt.Printf("retention switch rejected: %v\n", err) |
| 54 |
return |
| 55 |
} |
| 56 |
fmt.Println("update unexpectedly succeeded") |
| 57 |
// NATS-DOC-END |
| 58 |
} |