generated from snivilised/astrolib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generic-pool.go
152 lines (124 loc) Β· 3.11 KB
/
generic-pool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package pants
import (
"context"
"time"
"github.com/snivilised/pants/internal/third/ants"
"github.com/snivilised/pants/internal/third/lo"
"github.com/snivilised/pants/locale"
)
// functionalPool
type functionalPool struct {
pool *ants.PoolWithFunc
}
// Post submits a task to the pool.
func (p *functionalPool) Post(ctx context.Context, job InputParam) error {
return p.pool.Invoke(ctx, job)
}
// Release closes this pool and releases the worker queue.
func (p *functionalPool) Release(ctx context.Context) {
p.pool.Release(ctx)
}
// Running returns the number of workers currently running.
func (p *functionalPool) Running() int {
return p.pool.Running()
}
// Waiting returns the number of tasks waiting to be executed.
func (p *functionalPool) Waiting() int {
return p.pool.Waiting()
}
func (p *functionalPool) GetOptions() *Options {
return p.pool.GetOptions()
}
// taskPool
type taskPool struct {
pool *ants.Pool
}
// Post submits a task to the pool.
func (p *taskPool) Post(ctx context.Context, task TaskFunc) error {
return p.pool.Submit(ctx, task)
}
// Release closes this pool and releases the worker queue.
func (p *taskPool) Release(ctx context.Context) {
p.pool.Release(ctx)
}
// Running returns the number of workers currently running.
func (p *taskPool) Running() int {
return p.pool.Running()
}
// Waiting returns the number of tasks waiting to be executed.
func (p *taskPool) Waiting() int {
return p.pool.Waiting()
}
func (p *taskPool) GetOptions() *Options {
return p.pool.GetOptions()
}
func source[I any](ctx context.Context,
wg WaitGroup, o *ants.Options,
injectable injectable[I],
closable closable,
) *Duplex[I] {
inputDupCh := NewDuplex(make(SourceStream[I], o.Input.BufferSize))
wg.Add(1)
go func(ctx context.Context, inputCh SourceStreamR[I]) {
defer func() {
closable.terminate()
wg.Done()
}()
for {
select {
case <-ctx.Done():
return
case input, ok := <-inputCh:
if !ok {
return
}
_ = injectable.inject(input)
}
}
}(ctx, inputDupCh.ReaderCh)
return inputDupCh
}
func newOutputInfo[O any](o *Options) *outputInfo[O] {
if o.Output == nil {
return nil
}
return &outputInfo[O]{
cancelDupCh: NewDuplex(make(CancelStream, 1)),
outputDupCh: NewDuplex(make(JobOutputStream[O], o.Output.BufferSize)),
}
}
// fromOutputs assumes o.Output is defined
func fromOutputInfo[O any](o *Options, oi *outputInfo[O]) *outputInfoW[O] {
const never = time.Hour * 50000
timeout := lo.TernaryF(o.Output != nil,
func() time.Duration {
return max(o.Output.TimeoutOnSend, ants.MinimumTimeoutOnSend)
},
func() time.Duration {
return never
},
)
return &outputInfoW[O]{
cancelCh: oi.cancelDupCh.WriterCh,
outputCh: oi.outputDupCh.WriterCh,
timeoutOnSend: timeout,
}
}
func respond[O any](ctx context.Context,
wi *outputInfoW[O], output *JobOutput[O],
) (err error) {
select {
case wi.outputCh <- *output:
return nil
case <-time.After(wi.timeoutOnSend):
select {
case <-ctx.Done():
err = ctx.Err()
case wi.cancelCh <- CancelWorkSignal{}:
err = locale.ErrTimeout
}
case <-ctx.Done():
err = ctx.Err()
}
return err
}