Skip to content

Commit

Permalink
Added message subject string interning
Browse files Browse the repository at this point in the history
  • Loading branch information
moredure committed Sep 15, 2021
1 parent eb89c8a commit 12db3f2
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions nats.go
Expand Up @@ -577,6 +577,9 @@ type Subscription struct {
// Type of Subscription
typ SubscriptionType

// Whether subject in messages and Subject in subscription may differ
smd bool

// Async linked list
pHead *Msg
pTail *Msg
Expand Down Expand Up @@ -2692,7 +2695,10 @@ func (nc *Conn) processMsg(data []byte) {
}

// Copy them into string
subj := string(nc.ps.ma.subject)
subj := sub.Subject
if sub.smd {
subj = string(nc.ps.ma.subject)
}
reply := string(nc.ps.ma.reply)

// Doing message create outside of the sub's lock to reduce contention.
Expand Down Expand Up @@ -3746,6 +3752,11 @@ func badQueue(qname string) bool {
return strings.ContainsAny(qname, " \t\r\n")
}

// wildcard will check a subject name for wildcardness.
func wildcard(subj string) bool {
return strings.ContainsAny(subj, "*>")
}

// subscribe is the internal subscribe function that indicates interest in a subject.
func (nc *Conn) subscribe(subj, queue string, cb MsgHandler, ch chan *Msg, isSync bool, js *jsSub) (*Subscription, error) {
if nc == nil {
Expand Down Expand Up @@ -3779,7 +3790,14 @@ func (nc *Conn) subscribeLocked(subj, queue string, cb MsgHandler, ch chan *Msg,
return nil, ErrBadSubscription
}

sub := &Subscription{Subject: subj, Queue: queue, mcb: cb, conn: nc, jsi: js}
sub := &Subscription{
Subject: subj,
Queue: queue,
mcb: cb,
conn: nc,
jsi: js,
smd: wildcard(subj) || js != nil,
}
// Set pending limits.
if ch != nil {
sub.pMsgsLimit = cap(ch)
Expand Down

0 comments on commit 12db3f2

Please sign in to comment.