Skip to content

Commit

Permalink
use a buffered channel in the acceptQueue (#53)
Browse files Browse the repository at this point in the history
There's a race condition here. A user might call Next to obtain the next
stream, and get a nil result. He then calls Chan get notified about new
incoming streams. If a new stream is accepted between the call to Next and the
call to Chan, he won't be notified about that stream, unless the channel is
buffered.
  • Loading branch information
marten-seemann authored Dec 8, 2022
1 parent 4037504 commit 67c4ac1
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ const closeWebtransportSessionCapsuleType http3.CapsuleType = 0x2843

type acceptQueue[T any] struct {
mx sync.Mutex
c chan struct{}
// The channel is used to notify consumers (via Chan) about new incoming items.
// Needs to be buffered to preserve the notification if an item is enqueued
// between a call to Next and to Chan.
c chan struct{}
// Contains all the streams waiting to be accepted.
// There's no explicit limit to the length of the queue, but it is implicitly
// limited by the stream flow control provided by QUIC.
queue []T
}

func newAcceptQueue[T any]() *acceptQueue[T] {
return &acceptQueue[T]{c: make(chan struct{})}
return &acceptQueue[T]{c: make(chan struct{}, 1)}
}

func (q *acceptQueue[T]) Add(str T) {
Expand Down

0 comments on commit 67c4ac1

Please sign in to comment.