nats-io / nats.go

Golang client for NATS, the cloud native messaging system.
https://nats.io
Apache License 2.0
5.58k stars 698 forks source link

Add identification property to subscriptions #1731

Open maurobalbi opened 4 weeks ago

maurobalbi commented 4 weeks ago

Proposed change

Add an additional property to allow identification of individual subscriptions e.g.

sub, _  := nc.Subscribe(...)

sub.SetId("special subscription")

Use case

I have an admittedly special case where multiple subscriptions subscribe to the same subject in the same application. In case of a slow subscriber, it's currently hard to figure out exactly which subscription is causing the error:

sub1, _ := nats.Subscribe("topic", func() { time.Sleep(100 * time.Second) })

sub2, _ := nats.Subscribe("topic", func() {} )

in the error callback only the subject and queue are available:

natsErrHandler(nc *nats.Conn, sub *nats.Subscription, natsErr error) {
    if natsErr == nats.ErrSlowConsumer {
       // which subscription is causing the error?
    }
)

adding the Id property would allow us to identify the subscription in the handler:

sub1, _ := nats.Subscribe("topic", func() { time.Sleep(100 * time.Second) })
sub1.SetId("sub1")

sub2, _ := nats.Subscribe("topic", func() {} )
sub2.SetId("sub2")

// ..... 
natsErrHandler(nc *nats.Conn, sub *nats.Subscription, natsErr error) {
    if natsErr == nats.ErrSlowConsumer {
       fmt.Println("slow consumer in subscription: ", sub.Id())
    }
)

Contribution

Yes, seems a straightforward implementation.

Jarema commented 4 weeks ago

Hey.

There is ongoing work on that topic while working on iterators: https://github.com/nats-io/nats.go/pull/1728