sdk-go icon indicating copy to clipboard operation
sdk-go copied to clipboard

NATS Jetstream optimistic concurrency headers

Open lilytrotter opened this issue 10 months ago • 2 comments

Hi, folks! Is it possible to access the special NATS headers on send, for example Nats-Expected-Last-Sequence

These headers would allow event sourcing with CloudEvents. The neat thing is that consumers don't need to do anything. It's more like a quality of service between NATS client and the server.

If not currently possible, are there workarounds? Could we find a way to add support without breaking the current API?

lilytrotter avatar Mar 31 '24 14:03 lilytrotter

If I understand the current nats_jetstream protocol implementation correctly, it ignores this header during the CE conversion: https://github.com/cloudevents/sdk-go/blob/e6a74efbacbf4ac70fc8ed598e678f94b6be7a6d/protocol/nats_jetstream/v2/message.go#L57-L97

IMHO it can be added without breaking the API similar to what we do in the Kafka protocol bindings.

embano1 avatar Apr 01 '24 07:04 embano1

I'd be happy with a change to enable this, but not sure on the correct approach. I'm not too familiar to how other protocol bindings work, but http just has an exported struct field:

https://github.com/cloudevents/sdk-go/blob/6408c0101c4ea93de70e28cd4624cb8c40f79027/v2/protocol/http/message.go#L37-L49

If you did something similar, users can then just do the following:

var msg binding.Message

if msg, ok := msg.(*jetstreamv2.Message); ok {
  fmt.Println(msg.Header)
}

dan-j avatar Apr 18 '24 10:04 dan-j