Wait waits for the queue to be empty or shut down.
(ctx context.Context)
| 98 | |
| 99 | // Wait waits for the queue to be empty or shut down. |
| 100 | func (q *ExecQueue) Wait(ctx context.Context) error { |
| 101 | q.mu.Lock() |
| 102 | q.initCtxLocked() |
| 103 | waitCh := q.doneWaiter |
| 104 | if q.inFlight && waitCh == nil { |
| 105 | waitCh = make(chan struct{}) |
| 106 | q.doneWaiter = waitCh |
| 107 | } |
| 108 | closed := q.closed |
| 109 | shutdownCtx := q.ctx |
| 110 | q.mu.Unlock() |
| 111 | |
| 112 | if closed { |
| 113 | return errExecQueueShutdown |
| 114 | } |
| 115 | if waitCh == nil { |
| 116 | return nil |
| 117 | } |
| 118 | |
| 119 | select { |
| 120 | case <-waitCh: |
| 121 | return nil |
| 122 | case <-shutdownCtx.Done(): |
| 123 | return errExecQueueShutdown |
| 124 | case <-ctx.Done(): |
| 125 | return ctx.Err() |
| 126 | } |
| 127 | } |