MCPcopy
hub / github.com/nats-io/nats.go / createNewRequestAndSend

Method createNewRequestAndSend

nats.go:4582–4608  ·  view source on GitHub ↗

Helper to setup and send new request style requests. Return the chan to receive the response.

(subj string, hdr, data []byte)

Source from the content-addressed store, hash-verified

4580
4581// Helper to setup and send new request style requests. Return the chan to receive the response.
4582func (nc *Conn) createNewRequestAndSend(subj string, hdr, data []byte) (chan *Msg, string, error) {
4583 nc.mu.Lock()
4584 // Create new literal Inbox and map to a chan msg.
4585 mch := make(chan *Msg, RequestChanLen)
4586 respInbox := nc.newRespInbox()
4587 token := respInbox[nc.respSubLen:]
4588
4589 nc.respMap[token] = mch
4590 if nc.respMux == nil {
4591 // Create the response subscription we will use for all new style responses.
4592 // This will be on an _INBOX with an additional terminal token. The subscription
4593 // will be on a wildcard.
4594 s, err := nc.subscribeLocked(nc.respSub, _EMPTY_, nc.respHandler, nil, nil, false, nil)
4595 if err != nil {
4596 nc.mu.Unlock()
4597 return nil, token, err
4598 }
4599 nc.respMux = s
4600 }
4601 nc.mu.Unlock()
4602
4603 if err := nc.publish(subj, respInbox, false, hdr, data); err != nil {
4604 return nil, token, err
4605 }
4606
4607 return mch, token, nil
4608}
4609
4610// RequestMsg will send a request payload including optional headers and deliver
4611// the response message, or an error, including a timeout if no message was received properly.

Callers 2

newRequestMethod · 0.95
requestWithContextMethod · 0.95

Calls 3

newRespInboxMethod · 0.95
subscribeLockedMethod · 0.95
publishMethod · 0.95

Tested by

no test coverage detected