Push adds an item to the queue. If closed, returns an error.
(x T)
| 46 | |
| 47 | // Push adds an item to the queue. If closed, returns an error. |
| 48 | func (q *Queue[T]) Push(x T) error { |
| 49 | q.mu.Lock() |
| 50 | defer q.mu.Unlock() |
| 51 | if q.closed { |
| 52 | return xerrors.New("queue has been closed") |
| 53 | } |
| 54 | // Potentially mutate or skip the push using the predicate. |
| 55 | if q.pred != nil { |
| 56 | var ok bool |
| 57 | x, ok = q.pred(x) |
| 58 | if !ok { |
| 59 | return nil |
| 60 | } |
| 61 | } |
| 62 | // Remove the first item from the queue if it has gotten too big. |
| 63 | if len(q.items) >= q.size { |
| 64 | q.items = q.items[1:] |
| 65 | } |
| 66 | q.items = append(q.items, x) |
| 67 | q.cond.Broadcast() |
| 68 | return nil |
| 69 | } |
| 70 | |
| 71 | // Pop removes and returns the first item from the queue, waiting until there is |
| 72 | // something to pop if necessary. If closed, returns false. |