-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsink_context.go
More file actions
173 lines (134 loc) · 3.46 KB
/
sink_context.go
File metadata and controls
173 lines (134 loc) · 3.46 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package sink
import (
"context"
"time"
"github.com/panjf2000/ants/v2"
)
// SinkWithContext is a struct to process different request simultaneously.
//nolint:golint
type SinkWithContext[I, O any] struct {
input chan request[I, O]
addPool *ants.PoolWithFunc
callbackPool *ants.PoolWithFunc
expensivePool *ants.PoolWithFunc
logger Logger
}
// NewSinkWithContext initializes a sink with the provided config and context.
func NewSinkWithContext[I, O any](ctx context.Context, config Config[I, O]) (*SinkWithContext[I, O], error) {
err := validateConfig(config)
if err != nil {
return nil, err
}
if config.Logger == nil {
config.Logger = standardLogger{}
}
s := &SinkWithContext[I, O]{logger: config.Logger}
s.input = make(chan request[I, O])
options := []ants.Option{ants.WithLogger(config.Logger)}
s.addPool, err = ants.NewPoolWithFunc(config.AddPoolSize, s.addFunc, options...)
if err != nil {
return nil, err
}
s.callbackPool, err = ants.NewPoolWithFunc(config.CallbackPoolSize, s.callbackFunc, options...)
if err != nil {
return nil, err
}
s.expensivePool, err = ants.NewPoolWithFunc(config.ExpensivePoolSize, s.expensiveWrapper(config.ExpensiveOperation), options...)
if err != nil {
return nil, err
}
batches := batchWithContext(ctx, s.input, config.MaxItemsForBatching, config.MaxTimeoutForBatching)
go func(batches chan []request[I, O]) {
for batch := range batches {
err := s.expensivePool.Invoke(batch)
if err != nil {
s.logger.Printf("error: %v", err)
}
}
}(batches)
go func(ctx context.Context) {
<-ctx.Done()
close(s.input)
s.addPool.Release()
s.expensivePool.Release()
s.callbackPool.Release()
}(ctx)
return s, nil
}
// Add adds a value to the sink and waits for result.
func (s *SinkWithContext[I, O]) Add(value I) (O, error) {
rqs := newItem[I, O](value)
err := s.addPool.Invoke(rqs)
if err != nil {
var result O
return result, err
}
rsp := <-rqs.callback
return rsp.value, rsp.err
}
func (s *SinkWithContext[I, O]) addFunc(i interface{}) {
rq := i.(request[I, O])
s.input <- rq
}
func (s *SinkWithContext[I, O]) callbackFunc(i interface{}) {
drsp := i.(response[O])
drsp.callback <- drsp
close(drsp.callback)
}
func (s *SinkWithContext[I, O]) expensiveWrapper(f expensiveOperation[I, O]) func(i interface{}) {
return func(i interface{}) {
batch := i.([]request[I, O])
values := make([]I, len(batch))
for k := range values {
values[k] = batch[k].value
}
responses, err := f(values)
for k := range responses {
r := response[O]{
callback: batch[k].callback,
}
if err != nil {
r.err = err
} else {
r.value = responses[k]
}
err := s.callbackPool.Invoke(r)
if err != nil {
s.logger.Printf("error: %v", err)
}
}
}
}
func batchWithContext[I, O any](ctx context.Context, values <-chan request[I, O], maxItems int, maxTimeout time.Duration) chan []request[I, O] {
batches := make(chan []request[I, O])
go func() {
defer close(batches)
for keepGoing := true; keepGoing; {
var batch []request[I, O]
expire := time.After(maxTimeout)
for {
select {
case <-ctx.Done():
keepGoing = false
goto done
case value, ok := <-values:
if !ok {
keepGoing = false
goto done
}
batch = append(batch, value)
if len(batch) == maxItems {
goto done
}
case <-expire:
goto done
}
}
done:
if len(batch) > 0 {
batches <- batch
}
}
}()
return batches
}