Skip to content

Commit 888e2ee

Browse files
committed
Look up last stored offset
1 parent d448426 commit 888e2ee

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

go-stream/offset_tracking_receive.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package main
22

33
import (
4+
"errors"
45
"fmt"
5-
"github.com/rabbitmq/rabbitmq-stream-go-client/pkg/amqp"
6-
"github.com/rabbitmq/rabbitmq-stream-go-client/pkg/stream"
76
"os"
87
"sync/atomic"
8+
9+
"github.com/rabbitmq/rabbitmq-stream-go-client/pkg/amqp"
10+
"github.com/rabbitmq/rabbitmq-stream-go-client/pkg/stream"
911
)
1012

1113
func main() {
@@ -34,7 +36,6 @@ func main() {
3436
}
3537
if atomic.AddInt64(&messageCount, 1)%10 == 0 {
3638
consumerContext.Consumer.StoreOffset()
37-
fmt.Println("storing offset")
3839
}
3940
if string(message.GetData()) == "marker" {
4041
lastOffset.Store(consumerContext.Consumer.GetOffset())
@@ -44,11 +45,20 @@ func main() {
4445
}
4546
}
4647

48+
consumerName := "offset-tracking-tutorial"
49+
var offsetSpecification stream.OffsetSpecification
50+
storedOffset, err := env.QueryOffset(consumerName, streamName)
51+
if errors.Is(err, stream.OffsetNotFoundError) {
52+
offsetSpecification = stream.OffsetSpecification{}.First()
53+
} else {
54+
offsetSpecification = stream.OffsetSpecification{}.Offset(storedOffset + 1)
55+
}
56+
4757
_, err = env.NewConsumer(streamName, messagesHandler,
4858
stream.NewConsumerOptions().
4959
SetManualCommit().
50-
SetConsumerName("offset-tracking-tutorial").
51-
SetOffset(stream.OffsetSpecification{}.LastConsumed()))
60+
SetConsumerName(consumerName).
61+
SetOffset(offsetSpecification))
5262
CheckErrReceive(err)
5363
fmt.Println("Started consuming...")
5464
_ = <-ch

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy