| 89 | } |
| 90 | |
| 91 | func (q *MsgQueue) run() { |
| 92 | for { |
| 93 | // wait until there is something on the queue or we are closed |
| 94 | q.cond.L.Lock() |
| 95 | for q.size == 0 && !q.closed { |
| 96 | q.cond.Wait() |
| 97 | } |
| 98 | if q.closed { |
| 99 | q.cond.L.Unlock() |
| 100 | return |
| 101 | } |
| 102 | item := q.q[q.front] |
| 103 | q.front = (q.front + 1) % BufferSize |
| 104 | q.size-- |
| 105 | q.cond.L.Unlock() |
| 106 | |
| 107 | // process item without holding lock |
| 108 | if item.err == nil { |
| 109 | // real message |
| 110 | if q.l != nil { |
| 111 | q.l(q.ctx, item.msg) |
| 112 | continue |
| 113 | } |
| 114 | if q.le != nil { |
| 115 | q.le(q.ctx, item.msg, nil) |
| 116 | continue |
| 117 | } |
| 118 | // unhittable |
| 119 | continue |
| 120 | } |
| 121 | // if the listener wants errors, send it. |
| 122 | if q.le != nil { |
| 123 | q.le(q.ctx, nil, item.err) |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | func (q *MsgQueue) Enqueue(msg []byte) { |
| 129 | q.cond.L.Lock() |