mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-29 06:35:07 +03:00
WIP have peer actors queue packets, temporarily a single simple FIFO queue with head drop
This commit is contained in:
parent
9834f222db
commit
945930aa2c
6 changed files with 91 additions and 338 deletions
39
src/yggdrasil/packetqueue.go
Normal file
39
src/yggdrasil/packetqueue.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
package yggdrasil
|
||||
|
||||
import "github.com/yggdrasil-network/yggdrasil-go/src/util"
|
||||
|
||||
// TODO take max size from config
|
||||
const MAX_PACKET_QUEUE_SIZE = 1048576 // 1 MB
|
||||
|
||||
// TODO separate queues per e.g. traffic flow
|
||||
type packetQueue struct {
|
||||
packets [][]byte
|
||||
size uint32
|
||||
}
|
||||
|
||||
func (q *packetQueue) cleanup() {
|
||||
for q.size > MAX_PACKET_QUEUE_SIZE {
|
||||
if packet, success := q.pop(); success {
|
||||
util.PutBytes(packet)
|
||||
} else {
|
||||
panic("attempted to drop packet from empty queue")
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (q *packetQueue) push(packet []byte) {
|
||||
q.packets = append(q.packets, packet)
|
||||
q.size += uint32(len(packet))
|
||||
q.cleanup()
|
||||
}
|
||||
|
||||
func (q *packetQueue) pop() ([]byte, bool) {
|
||||
if len(q.packets) > 0 {
|
||||
packet := q.packets[0]
|
||||
q.packets = q.packets[1:]
|
||||
q.size -= uint32(len(packet))
|
||||
return packet, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue