Debian Codegraph @

vnet/chunk_queue.go

raw symbols SCIP
1 package vnet
2
3 import (
4 "sync"
5 )
6
7 type chunkQueue struct {
8 chunks []Chunk
9 maxSize int // 0 or negative value: unlimited
10 maxBytes int // 0 or negative value: unlimited
11 currentBytes int
12 mutex sync.RWMutex
13 }
14
15 func newChunkQueue(maxSize int, maxBytes int) *chunkQueue {
16 return &chunkQueue{
17 chunks: []Chunk{},
18 maxSize: maxSize,
19 maxBytes: maxBytes,
20 currentBytes: 0,
21 mutex: sync.RWMutex{},
22 }
23 }
24
25 func (q *chunkQueue) push(c Chunk) bool {
26 q.mutex.Lock()
27 defer q.mutex.Unlock()
28
29 if q.maxSize > 0 && len(q.chunks) >= q.maxSize {
30 return false // dropped
31 }
32 if q.maxBytes > 0 && q.currentBytes+len(c.UserData()) >= q.maxBytes {
33 return false
34 }
35
36 q.currentBytes += len(c.UserData())
37 q.chunks = append(q.chunks, c)
38 return true
39 }
40
41 func (q *chunkQueue) pop() (Chunk, bool) {
42 q.mutex.Lock()
43 defer q.mutex.Unlock()
44
45 if len(q.chunks) == 0 {
46 return nil, false
47 }
48
49 c := q.chunks[0]
50 q.chunks = q.chunks[1:]
51 q.currentBytes -= len(c.UserData())
52
53 return c, true
54 }
55
56 func (q *chunkQueue) peek() Chunk {
57 q.mutex.RLock()
58 defer q.mutex.RUnlock()
59
60 if len(q.chunks) == 0 {
61 return nil
62 }
63
64 return q.chunks[0]
65 }