-
Notifications
You must be signed in to change notification settings - Fork 2k
/
async_op.go
70 lines (55 loc) · 1.43 KB
/
async_op.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
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package cacheutil
import (
"sync"
"github.com/pkg/errors"
)
var (
ErrAsyncBufferFull = errors.New("the async buffer is full")
)
type AsyncOperationProcessor struct {
// Channel used to notify internal goroutines when they should quit.
stop chan struct{}
// Channel used to enqueue async operations.
asyncQueue chan func()
// Wait group used to wait all workers on stopping.
workers sync.WaitGroup
}
// NewAsyncOperationProcessor creates an async processor with given bufferSize and concurrency.
func NewAsyncOperationProcessor(bufferSize, concurrency int) *AsyncOperationProcessor {
p := &AsyncOperationProcessor{
stop: make(chan struct{}, 1),
asyncQueue: make(chan func(), bufferSize),
}
p.workers.Add(concurrency)
for i := 0; i < concurrency; i++ {
go p.asyncQueueProcessLoop()
}
return p
}
func (p *AsyncOperationProcessor) Stop() {
close(p.stop)
// Wait until all workers have terminated.
p.workers.Wait()
}
func (p *AsyncOperationProcessor) asyncQueueProcessLoop() {
defer p.workers.Done()
for {
select {
case op := <-p.asyncQueue:
op()
case <-p.stop:
return
}
}
}
// EnqueueAsync enqueues op to async queue. If enqueue failed, ErrAsyncBufferFull is returned.
func (p *AsyncOperationProcessor) EnqueueAsync(op func()) error {
select {
case p.asyncQueue <- op:
return nil
default:
return ErrAsyncBufferFull
}
}