| 1 |
// Copyright 2020-2025 The NATS Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package server |
| 15 |
|
| 16 |
import ( |
| 17 |
"strconv" |
| 18 |
"sync" |
| 19 |
) |
| 20 |
|
| 21 |
type outMsg struct { |
| 22 |
subj string |
| 23 |
rply string |
| 24 |
hdr []byte |
| 25 |
msg []byte |
| 26 |
} |
| 27 |
|
| 28 |
type sendq struct { |
| 29 |
mu sync.Mutex |
| 30 |
q *ipQueue[*outMsg] |
| 31 |
s *Server |
| 32 |
a *Account |
| 33 |
} |
| 34 |
|
| 35 |
func (s *Server) newSendQ(acc *Account) *sendq { |
| 36 |
sq := &sendq{s: s, q: newIPQueue[*outMsg](s, "SendQ"), a: acc} |
| 37 |
s.startGoRoutine(sq.internalLoop) |
| 38 |
return sq |
| 39 |
} |
| 40 |
|
| 41 |
func (sq *sendq) internalLoop() { |
| 42 |
sq.mu.Lock() |
| 43 |
s, q := sq.s, sq.q |
| 44 |
sq.mu.Unlock() |
| 45 |
|
| 46 |
defer s.grWG.Done() |
| 47 |
|
| 48 |
c := s.createInternalSystemClient() |
| 49 |
c.registerWithAccount(sq.a) |
| 50 |
c.noIcb = true |
| 51 |
|
| 52 |
defer c.closeConnection(ClientClosed) |
| 53 |
|
| 54 |
// To optimize for not converting a string to a []byte slice. |
| 55 |
var ( |
| 56 |
subj [256]byte |
| 57 |
rply [256]byte |
| 58 |
szb [10]byte |
| 59 |
hdb [10]byte |
| 60 |
_msg [4096]byte |
| 61 |
msg = _msg[:0] |
| 62 |
) |
| 63 |
|
| 64 |
for s.isRunning() { |
| 65 |
select { |
| 66 |
case <-s.quitCh: |
| 67 |
return |
| 68 |
case <-q.ch: |
| 69 |
pms := q.pop() |
| 70 |
for _, pm := range pms { |
| 71 |
c.pa.subject = append(subj[:0], pm.subj...) |
| 72 |
c.pa.size = len(pm.msg) + len(pm.hdr) |
| 73 |
c.pa.szb = append(szb[:0], strconv.Itoa(c.pa.size)...) |
| 74 |
if len(pm.rply) > 0 { |
| 75 |
c.pa.reply = append(rply[:0], pm.rply...) |
| 76 |
} else { |
| 77 |
c.pa.reply = nil |
| 78 |
} |
| 79 |
msg = msg[:0] |
| 80 |
if len(pm.hdr) > 0 { |
| 81 |
c.pa.hdr = len(pm.hdr) |
| 82 |
c.pa.hdb = append(hdb[:0], strconv.Itoa(c.pa.hdr)...) |
| 83 |
msg = append(msg, pm.hdr...) |
| 84 |
msg = append(msg, pm.msg...) |
| 85 |
msg = append(msg, _CRLF_...) |
| 86 |
} else { |
| 87 |
c.pa.hdr = -1 |
| 88 |
c.pa.hdb = nil |
| 89 |
msg = append(msg, pm.msg...) |
| 90 |
msg = append(msg, _CRLF_...) |
| 91 |
} |
| 92 |
c.processInboundClientMsg(msg) |
| 93 |
c.pa.szb = nil |
| 94 |
outMsgPool.Put(pm) |
| 95 |
} |
| 96 |
// TODO: should this be in the for-loop instead? |
| 97 |
c.flushClients(0) |
| 98 |
q.recycle(&pms) |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
var outMsgPool = sync.Pool{ |
| 104 |
New: func() any { |
| 105 |
return &outMsg{} |
| 106 |
}, |
| 107 |
} |
| 108 |
|
| 109 |
func (sq *sendq) send(subj, rply string, hdr, msg []byte) { |
| 110 |
if sq == nil { |
| 111 |
return |
| 112 |
} |
| 113 |
out := outMsgPool.Get().(*outMsg) |
| 114 |
out.subj, out.rply = subj, rply |
| 115 |
out.hdr = append(out.hdr[:0], hdr...) |
| 116 |
out.msg = append(out.msg[:0], msg...) |
| 117 |
sq.q.push(out) |
| 118 |
} |