|
6 | 6 | "io" |
7 | 7 | "log" |
8 | 8 | "os" |
| 9 | + "time" |
9 | 10 |
|
10 | 11 | ydb "github.com/ydb-platform/ydb-go-sdk/v3" |
11 | 12 | "github.com/ydb-platform/ydb-go-sdk/v3/topic/topicoptions" |
@@ -68,6 +69,73 @@ func Example_alterTopic() { |
68 | 69 | } |
69 | 70 | } |
70 | 71 |
|
| 72 | +func Example_createTopicWithConsumerAvailabilityPeriod() { |
| 73 | + ctx := context.TODO() |
| 74 | + connectionString := os.Getenv("YDB_CONNECTION_STRING") |
| 75 | + if connectionString == "" { |
| 76 | + connectionString = "grpc://localhost:2136/local" |
| 77 | + } |
| 78 | + db, err := ydb.Open(ctx, connectionString) |
| 79 | + if err != nil { |
| 80 | + log.Printf("failed connect: %v", err) |
| 81 | + |
| 82 | + return |
| 83 | + } |
| 84 | + defer db.Close(ctx) // cleanup resources |
| 85 | + |
| 86 | + // Create topic with consumer that has 24-hour availability period |
| 87 | + // Messages for this consumer won't expire for at least 24 hours even if not committed |
| 88 | + availabilityPeriod := 24 * time.Hour |
| 89 | + err = db.Topic().Create(ctx, "topic-path", |
| 90 | + topicoptions.CreateWithConsumer(topictypes.Consumer{ |
| 91 | + Name: "my-consumer", |
| 92 | + Important: true, |
| 93 | + AvailabilityPeriod: &availabilityPeriod, // Messages available for at least 24 hours |
| 94 | + SupportedCodecs: []topictypes.Codec{topictypes.CodecRaw, topictypes.CodecGzip}, |
| 95 | + }), |
| 96 | + ) |
| 97 | + if err != nil { |
| 98 | + log.Printf("failed create topic: %v", err) |
| 99 | + |
| 100 | + return |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func Example_alterConsumerAvailabilityPeriod() { |
| 105 | + ctx := context.TODO() |
| 106 | + connectionString := os.Getenv("YDB_CONNECTION_STRING") |
| 107 | + if connectionString == "" { |
| 108 | + connectionString = "grpc://localhost:2136/local" |
| 109 | + } |
| 110 | + db, err := ydb.Open(ctx, connectionString) |
| 111 | + if err != nil { |
| 112 | + log.Printf("failed connect: %v", err) |
| 113 | + |
| 114 | + return |
| 115 | + } |
| 116 | + defer db.Close(ctx) // cleanup resources |
| 117 | + |
| 118 | + // Set availability period to 48 hours for existing consumer |
| 119 | + err = db.Topic().Alter(ctx, "topic-path", |
| 120 | + topicoptions.AlterConsumerWithAvailabilityPeriod("my-consumer", 48*time.Hour), |
| 121 | + ) |
| 122 | + if err != nil { |
| 123 | + log.Printf("failed alter consumer availability period: %v", err) |
| 124 | + |
| 125 | + return |
| 126 | + } |
| 127 | + |
| 128 | + // Reset availability period to default value |
| 129 | + err = db.Topic().Alter(ctx, "topic-path", |
| 130 | + topicoptions.AlterConsumerResetAvailabilityPeriod("my-consumer"), |
| 131 | + ) |
| 132 | + if err != nil { |
| 133 | + log.Printf("failed reset consumer availability period: %v", err) |
| 134 | + |
| 135 | + return |
| 136 | + } |
| 137 | +} |
| 138 | + |
71 | 139 | func Example_describeTopic() { |
72 | 140 | ctx := context.TODO() |
73 | 141 | connectionString := os.Getenv("YDB_CONNECTION_STRING") |
|
0 commit comments