|
| 1 | +package async |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sync" |
| 6 | +) |
| 7 | + |
| 8 | +type Paralleler struct { |
| 9 | + concurrency int |
| 10 | + ctx context.Context |
| 11 | + locker sync.Mutex |
| 12 | + tasks []AsyncFn |
| 13 | +} |
| 14 | + |
| 15 | +func (p *Paralleler) WithConcurrency(concurrency int) *Paralleler { |
| 16 | + if concurrency < 0 { |
| 17 | + panic(ErrInvalidConcurrency) |
| 18 | + } |
| 19 | + |
| 20 | + p.concurrency = concurrency |
| 21 | + |
| 22 | + return p |
| 23 | +} |
| 24 | + |
| 25 | +func (p *Paralleler) WithContext(ctx context.Context) *Paralleler { |
| 26 | + p.ctx = ctx |
| 27 | + |
| 28 | + return p |
| 29 | +} |
| 30 | + |
| 31 | +func (p *Paralleler) Add(funcs ...AsyncFn) *Paralleler { |
| 32 | + validateAsyncFuncs(funcs...) |
| 33 | + |
| 34 | + p.locker.Lock() |
| 35 | + defer p.locker.Unlock() |
| 36 | + |
| 37 | + p.tasks = append(p.tasks, funcs...) |
| 38 | + |
| 39 | + return p |
| 40 | +} |
| 41 | + |
| 42 | +func (p *Paralleler) Run() ([][]any, error) { |
| 43 | + tasks := p.getTasks() |
| 44 | + out := make([][]any, len(tasks)) |
| 45 | + if len(tasks) == 0 { |
| 46 | + return out, nil |
| 47 | + } |
| 48 | + |
| 49 | + parent := getContext(p.ctx) |
| 50 | + ctx, canFunc := context.WithCancel(parent) |
| 51 | + defer canFunc() |
| 52 | + |
| 53 | + ch := make(chan executeResult, len(tasks)) |
| 54 | + |
| 55 | + go p.runTasks(ctx, ch, tasks) |
| 56 | + |
| 57 | + finished := 0 |
| 58 | + for finished < len(tasks) { |
| 59 | + select { |
| 60 | + case <-parent.Done(): |
| 61 | + return out, ErrContextCanceled |
| 62 | + case ret := <-ch: |
| 63 | + out[ret.Index] = ret.Out |
| 64 | + if ret.Error != nil { |
| 65 | + return out, &executionError{ |
| 66 | + index: ret.Index, |
| 67 | + err: ret.Error, |
| 68 | + } |
| 69 | + } |
| 70 | + finished++ |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return out, nil |
| 75 | +} |
| 76 | + |
| 77 | +func (p *Paralleler) getConcurrencyChan() chan empty { |
| 78 | + var conch chan empty |
| 79 | + |
| 80 | + if p.concurrency > 0 { |
| 81 | + conch = make(chan empty, p.concurrency) |
| 82 | + } |
| 83 | + |
| 84 | + return conch |
| 85 | +} |
| 86 | + |
| 87 | +func (p *Paralleler) getTasks() []AsyncFn { |
| 88 | + p.locker.Lock() |
| 89 | + |
| 90 | + tasks := p.tasks |
| 91 | + p.tasks = nil |
| 92 | + |
| 93 | + p.locker.Unlock() |
| 94 | + |
| 95 | + return tasks |
| 96 | +} |
| 97 | + |
| 98 | +func (p *Paralleler) runTasks(ctx context.Context, resCh chan executeResult, tasks []AsyncFn) { |
| 99 | + conch := p.getConcurrencyChan() |
| 100 | + |
| 101 | + for i := 0; i < len(tasks); i++ { |
| 102 | + if conch != nil { |
| 103 | + conch <- empty{} |
| 104 | + } |
| 105 | + |
| 106 | + go p.runTask(ctx, i, tasks[i], conch, resCh) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func (p *Paralleler) runTask( |
| 111 | + ctx context.Context, |
| 112 | + n int, |
| 113 | + fn AsyncFn, |
| 114 | + conch chan empty, |
| 115 | + ch chan executeResult, |
| 116 | +) { |
| 117 | + childCtx, childCanFunc := context.WithCancel(ctx) |
| 118 | + defer childCanFunc() |
| 119 | + |
| 120 | + ret, err := invokeAsyncFn(fn, childCtx, nil) |
| 121 | + |
| 122 | + if conch != nil { |
| 123 | + <-conch |
| 124 | + } |
| 125 | + |
| 126 | + select { |
| 127 | + case <-ctx.Done(): |
| 128 | + return |
| 129 | + default: |
| 130 | + ch <- executeResult{ |
| 131 | + Index: n, |
| 132 | + Error: err, |
| 133 | + Out: ret, |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments