| // Copyright 2023 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| // A queue is an unbounded queue of some item (new connections and streams). |
| type queue[T any] struct { |
| // The gate condition is set if the queue is non-empty or closed. |
| func newQueue[T any]() queue[T] { |
| return queue[T]{gate: newGate()} |
| // close closes the queue, causing pending and future pop operations |
| // to return immediately with err. |
| func (q *queue[T]) close(err error) { |
| // put appends an item to the queue. |
| // It returns true if the item was added, false if the queue is closed. |
| func (q *queue[T]) put(v T) bool { |
| // get removes the first item from the queue, blocking until ctx is done, an item is available, |
| // or the queue is closed. |
| func (q *queue[T]) get(ctx context.Context, testHooks connTestHooks) (T, error) { |
| if err := q.gate.waitAndLock(ctx, testHooks); err != nil { |
| func (q *queue[T]) unlock() { |
| q.gate.unlock(q.err != nil || len(q.q) > 0) |