Home > Software design >  natsClient.Subscribe("some_subject", some_callback): Does the 'some_callback' ca
natsClient.Subscribe("some_subject", some_callback): Does the 'some_callback' ca

Time:12-01

Essentially what the subject says. I'm interested to know whether each time the callback method is invoked the nats-lib for golang ensures that the callback will run on its very own goroutine (I'm 99% sure that it does but I need to make sure because I can't find any explicit piece of documentation in nats that conclusively states it so - feel free to copy paste any links if I've missed something).

If the callback does indeed run in it's own goroutine I'm also interested to know whether:

a. the same goroutine is used every time for a specific subscription

or b. an ephimeral goroutine is created for each firing of the callback: in this case the goroutine gets disposed of after the callback has done its work

CodePudding user response:

When you call conn.Subscribe("some_subject", someCallback), it executes this piece of code if the callback is defined:

// If we have an async callback, start up a sub specific
    // Go routine to deliver the messages.
    if cb != nil {
        sub.typ = AsyncSubscription
        sub.pCond = sync.NewCond(&sub.mu)
        go nc.waitForMsgs(sub)
    }

It launches a goroutine for the created subscription. When a message arrives, it executes the defined callback inside the waitForMsgs goroutine.

In short, the answer is a., it uses the same goroutine every time for a specific subscription.

  • Related