Skip to content

Commit

Permalink
[IMPROVED] Performance of respToken (#1575)
Browse files Browse the repository at this point in the history
Signed-off-by: Piotr Piotrowski <piotr@synadia.com>
  • Loading branch information
piotrpio committed Feb 29, 2024
1 parent a04478f commit 3fefbb2
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,6 @@ type Conn struct {
respSub string // The wildcard subject
respSubPrefix string // the wildcard prefix including trailing .
respSubLen int // the length of the wildcard prefix excluding trailing .
respScanf string // The scanf template to extract mux token
respMux *Subscription // A single response subscription
respMap map[string]chan *Msg // Request map for the response msg channels
respRand *rand.Rand // Used for generating suffix
Expand Down Expand Up @@ -3938,7 +3937,6 @@ func (nc *Conn) createNewRequestAndSend(subj string, hdr, data []byte) (chan *Ms
nc.mu.Unlock()
return nil, token, err
}
nc.respScanf = strings.Replace(nc.respSub, "*", "%s", -1)
nc.respMux = s
}
nc.mu.Unlock()
Expand Down Expand Up @@ -4119,16 +4117,14 @@ func (nc *Conn) NewRespInbox() string {
}

// respToken will return the last token of a literal response inbox
// which we use for the message channel lookup. This needs to do a
// scan to protect itself against the server changing the subject.
// which we use for the message channel lookup. This needs to verify the subject
// prefix matches to protect itself against the server changing the subject.
// Lock should be held.
func (nc *Conn) respToken(respInbox string) string {
var token string
n, err := fmt.Sscanf(respInbox, nc.respScanf, &token)
if err != nil || n != 1 {
return ""
if token, found := strings.CutPrefix(respInbox, nc.respSubPrefix); found {
return token
}
return token
return ""
}

// Subscribe will express interest in the given subject. The subject
Expand Down

0 comments on commit 3fefbb2

Please sign in to comment.