Debian Codegraph @

server/ipqueue.go

raw
1 // Copyright 2021-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 "errors"
18 "sync"
19 "sync/atomic"
20 )
21
22 const ipQueueDefaultMaxRecycleSize = 4 * 1024
23
24 // This is a generic intra-process queue.
25 type ipQueue[T any] struct {
26 inprogress int64
27 sync.Mutex
28 ch chan struct{}
29 elts []T
30 pos int
31 pool *sync.Pool
32 sz uint64 // Calculated size (only if calc != nil)
33 name string
34 m *sync.Map
35 ipQueueOpts[T]
36 }
37
38 type ipQueueOpts[T any] struct {
39 mrs int // Max recycle size
40 calc func(e T) uint64 // Calc function for tracking size
41 msz uint64 // Limit by total calculated size
42 mlen int // Limit by number of entries
43 }
44
45 type ipQueueOpt[T any] func(*ipQueueOpts[T])
46
47 // This option allows to set the maximum recycle size when attempting
48 // to put back a slice to the pool.
49 func ipqMaxRecycleSize[T any](max int) ipQueueOpt[T] {
50 return func(o *ipQueueOpts[T]) {
51 o.mrs = max
52 }
53 }
54
55 // This option enables total queue size counting by passing in a function
56 // that evaluates the size of each entry as it is pushed/popped. This option
57 // enables the size() function.
58 func ipqSizeCalculation[T any](calc func(e T) uint64) ipQueueOpt[T] {
59 return func(o *ipQueueOpts[T]) {
60 o.calc = calc
61 }
62 }
63
64 // This option allows setting the maximum queue size. Once the limit is
65 // reached, then push() will stop returning true and no more entries will
66 // be stored until some more are popped. The ipQueue_SizeCalculation must
67 // be provided for this to work.
68 func ipqLimitBySize[T any](max uint64) ipQueueOpt[T] {
69 return func(o *ipQueueOpts[T]) {
70 o.msz = max
71 }
72 }
73
74 // This option allows setting the maximum queue length. Once the limit is
75 // reached, then push() will stop returning true and no more entries will
76 // be stored until some more are popped.
77 func ipqLimitByLen[T any](max int) ipQueueOpt[T] {
78 return func(o *ipQueueOpts[T]) {
79 o.mlen = max
80 }
81 }
82
83 var errIPQLenLimitReached = errors.New("IPQ len limit reached")
84 var errIPQSizeLimitReached = errors.New("IPQ size limit reached")
85
86 func newIPQueue[T any](s *Server, name string, opts ...ipQueueOpt[T]) *ipQueue[T] {
87 q := &ipQueue[T]{
88 ch: make(chan struct{}, 1),
89 pool: &sync.Pool{
90 New: func() any {
91 // Reason we use pointer to slice instead of slice is explained
92 // here: https://staticcheck.io/docs/checks#SA6002
93 res := make([]T, 0, 32)
94 return &res
95 },
96 },
97 name: name,
98 m: &s.ipQueues,
99 ipQueueOpts: ipQueueOpts[T]{
100 mrs: ipQueueDefaultMaxRecycleSize,
101 },
102 }
103 for _, o := range opts {
104 o(&q.ipQueueOpts)
105 }
106 s.ipQueues.Store(name, q)
107 return q
108 }
109
110 // Add the element `e` to the queue, notifying the queue channel's `ch` if the
111 // entry is the first to be added, and returns the length of the queue after
112 // this element is added.
113 func (q *ipQueue[T]) push(e T) (int, error) {
114 q.Lock()
115 l := len(q.elts) - q.pos
116 if q.mlen > 0 && l == q.mlen {
117 q.Unlock()
118 return l, errIPQLenLimitReached
119 }
120 if q.calc != nil {
121 sz := q.calc(e)
122 if q.msz > 0 && q.sz+sz > q.msz {
123 q.Unlock()
124 return l, errIPQSizeLimitReached
125 }
126 q.sz += sz
127 }
128 if q.elts == nil {
129 // What comes out of the pool is already of size 0, so no need for [:0].
130 q.elts = *(q.pool.Get().(*[]T))
131 }
132 q.elts = append(q.elts, e)
133 q.Unlock()
134 if l == 0 {
135 select {
136 case q.ch <- struct{}{}:
137 default:
138 }
139 }
140 return l + 1, nil
141 }
142
143 // Returns the whole list of elements currently present in the queue,
144 // emptying the queue. This should be called after receiving a notification
145 // from the queue's `ch` notification channel that indicates that there
146 // is something in the queue.
147 // However, in cases where `drain()` may be called from another go
148 // routine, it is possible that a routine is notified that there is
149 // something, but by the time it calls `pop()`, the drain() would have
150 // emptied the queue. So the caller should never assume that pop() will
151 // return a slice of 1 or more, it could return `nil`.
152 func (q *ipQueue[T]) pop() []T {
153 if q == nil {
154 return nil
155 }
156 q.Lock()
157 if len(q.elts)-q.pos == 0 {
158 q.Unlock()
159 return nil
160 }
161 var elts []T
162 if q.pos == 0 {
163 elts = q.elts
164 } else {
165 elts = q.elts[q.pos:]
166 }
167 q.elts, q.pos, q.sz = nil, 0, 0
168 atomic.AddInt64(&q.inprogress, int64(len(elts)))
169 q.Unlock()
170 return elts
171 }
172
173 // Returns the first element from the queue, if any. See comment above
174 // regarding calling after being notified that there is something and
175 // the use of drain(). In short, the caller should always check the
176 // boolean return value to ensure that the value is genuine and not a
177 // default empty value.
178 func (q *ipQueue[T]) popOne() (T, bool) {
179 q.Lock()
180 l := len(q.elts) - q.pos
181 if l == 0 {
182 q.Unlock()
183 var empty T
184 return empty, false
185 }
186 e := q.elts[q.pos]
187 if l--; l > 0 {
188 q.pos++
189 if q.calc != nil {
190 q.sz -= q.calc(e)
191 }
192 // We need to re-signal
193 select {
194 case q.ch <- struct{}{}:
195 default:
196 }
197 } else {
198 // We have just emptied the queue, so we can reuse unless it is too big.
199 if cap(q.elts) <= q.mrs {
200 q.elts = q.elts[:0]
201 } else {
202 q.elts = nil
203 }
204 q.pos, q.sz = 0, 0
205 }
206 q.Unlock()
207 return e, true
208 }
209
210 // After a pop(), the slice can be recycled for the next push() when
211 // a first element is added to the queue.
212 // This will also decrement the "in progress" count with the length
213 // of the slice.
214 // WARNING: The caller MUST never reuse `elts`.
215 func (q *ipQueue[T]) recycle(elts *[]T) {
216 // If invoked with a nil list, nothing to do.
217 if elts == nil || *elts == nil {
218 return
219 }
220 // Update the in progress count.
221 if len(*elts) > 0 {
222 atomic.AddInt64(&q.inprogress, int64(-(len(*elts))))
223 }
224 // We also don't want to recycle huge slices, so check against the max.
225 // q.mrs is normally immutable but can be changed, in a safe way, in some tests.
226 if cap(*elts) > q.mrs {
227 return
228 }
229 (*elts) = (*elts)[:0]
230 q.pool.Put(elts)
231 }
232
233 // Returns the current length of the queue.
234 func (q *ipQueue[T]) len() int {
235 q.Lock()
236 defer q.Unlock()
237 return len(q.elts) - q.pos
238 }
239
240 // Returns the calculated size of the queue (if ipQueue_SizeCalculation has been
241 // passed in), otherwise returns zero.
242 func (q *ipQueue[T]) size() uint64 {
243 q.Lock()
244 defer q.Unlock()
245 return q.sz
246 }
247
248 // Empty the queue and consumes the notification signal if present.
249 // Returns the number of items that were drained from the queue.
250 // Note that this could cause a reader go routine that has been
251 // notified that there is something in the queue (reading from queue's `ch`)
252 // may then get nothing if `drain()` is invoked before the `pop()` or `popOne()`.
253 func (q *ipQueue[T]) drain() int {
254 if q == nil {
255 return 0
256 }
257 q.Lock()
258 olen := len(q.elts) - q.pos
259 q.elts, q.pos, q.sz = nil, 0, 0
260 // Consume the signal if it was present to reduce the chance of a reader
261 // routine to be think that there is something in the queue...
262 select {
263 case <-q.ch:
264 default:
265 }
266 q.Unlock()
267 return olen
268 }
269
270 // Since the length of the queue goes to 0 after a pop(), it is good to
271 // have an insight on how many elements are yet to be processed after a pop().
272 // For that reason, the queue maintains a count of elements returned through
273 // the pop() API. When the caller will call q.recycle(), this count will
274 // be reduced by the size of the slice returned by pop().
275 func (q *ipQueue[T]) inProgress() int64 {
276 return atomic.LoadInt64(&q.inprogress)
277 }
278
279 // Remove this queue from the server's map of ipQueues.
280 // All ipQueue operations (such as push/pop/etc..) are still possible.
281 func (q *ipQueue[T]) unregister() {
282 if q == nil {
283 return
284 }
285 q.m.Delete(q.name)
286 }