-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathworker.go
49 lines (46 loc) · 1.11 KB
/
worker.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
package httprc
import (
"context"
"fmt"
"sync"
)
type worker struct {
httpcl HTTPClient
incoming chan any
next <-chan Resource
nextsync <-chan synchronousRequest
errSink ErrorSink
traceSink TraceSink
}
func (w worker) Run(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
ctx = withTraceSink(ctx, w.traceSink)
ctx = withHTTPClient(ctx, w.httpcl)
for {
select {
case <-ctx.Done():
return
case r := <-w.next:
w.traceSink.Put(ctx, fmt.Sprintf("httprc worker: syncing %q", r.URL()))
if err := r.Sync(ctx); err != nil {
w.errSink.Put(ctx, err)
}
r.SetBusy(false)
select {
case <-ctx.Done():
case w.incoming <- adjustIntervalRequest{resource: r}:
}
case sr := <-w.nextsync:
w.traceSink.Put(ctx, fmt.Sprintf("httprc worker: syncing %q (synchronous)", sr.resource.URL()))
if err := sr.resource.Sync(ctx); err != nil {
sendReply(ctx, sr.reply, struct{}{}, err)
}
sr.resource.SetBusy(false)
sendReply(ctx, sr.reply, struct{}{}, nil)
select {
case <-ctx.Done():
case w.incoming <- adjustIntervalRequest{resource: sr.resource}:
}
}
}
}