RunSync waits for the queue to be drained and then synchronously runs f. It returns an error if the queue is closed before f is run or ctx expires.
(ctx context.Context, f func())
| 39 | // RunSync waits for the queue to be drained and then synchronously runs f. |
| 40 | // It returns an error if the queue is closed before f is run or ctx expires. |
| 41 | func (q *ExecQueue) RunSync(ctx context.Context, f func()) error { |
| 42 | q.mu.Lock() |
| 43 | q.initCtxLocked() |
| 44 | shutdownCtx := q.ctx |
| 45 | q.mu.Unlock() |
| 46 | |
| 47 | ch := make(chan struct{}) |
| 48 | q.Add(f) |
| 49 | q.Add(func() { close(ch) }) |
| 50 | select { |
| 51 | case <-ch: |
| 52 | return nil |
| 53 | case <-ctx.Done(): |
| 54 | return ctx.Err() |
| 55 | case <-shutdownCtx.Done(): |
| 56 | return errExecQueueShutdown |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func (q *ExecQueue) run(f func()) { |
| 61 | f() |