cloudevents / sdk-go

Go SDK for CloudEvents
https://cloudevents.github.io/sdk-go/
Apache License 2.0
785 stars 218 forks source link

NATS Jetstream optimistic concurrency headers #1029

Open lilytrotter opened 3 months ago

lilytrotter commented 3 months ago

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?

embano1 commented 3 months ago

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.

dan-j commented 2 months ago

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)
}