Files
quic-go/testutils/simnet/packetq.go
Marten Seemann 7772755df2 add simnet package to simulate a net.PacketConn in memory (#5385)
* Implement simnet

* simnet: remove separate license

* simnet: remove go.mod, use standard require package

* simnet: add README

* simnet: use synctest wrapper in tests

* simnet: minor code cleanup

* simnet: expose Packet.Data

* simnet: explose Simnet.Router

* simnet: remove SimpleFirewallRouter

* simnet: remove stray fmt.Println in tests

* fix deadline check for write deadlines

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* simnet: fix SetReadDeadline logic

---------

Co-authored-by: Marco Munizaga <git@marcopolo.io>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-10-18 09:19:46 +02:00

57 lines
1.1 KiB
Go

package simnet
import "sync"
type packetQueue struct {
byteCountLimit int
currentByteCount int
queue []packetWithDeliveryTime
closed bool
cond *sync.Cond
}
func newPacketQ(byteCountLimit int) *packetQueue {
q := &packetQueue{
byteCountLimit: byteCountLimit,
}
q.cond = sync.NewCond(&sync.Mutex{})
return q
}
func (q *packetQueue) Close() {
q.cond.L.Lock()
defer q.cond.L.Unlock()
q.cond.Broadcast()
q.closed = true
}
func (q *packetQueue) Push(p packetWithDeliveryTime) {
q.cond.L.Lock()
defer q.cond.L.Unlock()
if q.closed {
return
}
if q.currentByteCount+len(p.Data) > q.byteCountLimit {
return
}
q.queue = append(q.queue, p)
q.currentByteCount += len(p.Data)
q.cond.Signal()
}
func (q *packetQueue) Pop() (packetWithDeliveryTime, bool) {
q.cond.L.Lock()
defer q.cond.L.Unlock()
for len(q.queue) == 0 && !q.closed {
// Block until a packet is added
q.cond.Wait()
}
if q.closed {
return packetWithDeliveryTime{}, false
}
p := q.queue[0]
q.queue = q.queue[1:]
q.currentByteCount -= len(p.Data)
return p, true
}