use a ring buffer for the datagram queue (#4223)

This commit is contained in:
Marten Seemann
2024-01-01 11:50:26 +07:00
committed by GitHub
parent 1fce81f8bb
commit 22b7f7744e
3 changed files with 33 additions and 13 deletions

View File

@@ -8,7 +8,7 @@ type RingBuffer[T any] struct {
full bool
}
// Init preallocs a buffer with a certain size.
// Init preallocates a buffer with a certain size.
func (r *RingBuffer[T]) Init(size int) {
r.ring = make([]T, size)
}
@@ -62,6 +62,16 @@ func (r *RingBuffer[T]) PopFront() T {
return t
}
// PeekFront returns the next element.
// It must not be called when the buffer is empty, that means that
// callers might need to check if there are elements in the buffer first.
func (r *RingBuffer[T]) PeekFront() T {
if r.Empty() {
panic("github.com/quic-go/quic-go/internal/utils/ringbuffer: peek from an empty queue")
}
return r.ring[r.headPos]
}
// Grow the maximum size of the queue.
// This method assume the queue is full.
func (r *RingBuffer[T]) grow() {