forked from korovkin/limiter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
limiter.go
91 lines (77 loc) · 2.05 KB
/
limiter.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
package limiter
import (
"sync/atomic"
)
const (
// DefaultLimit is the default concurrency limit
DefaultLimit = 100
)
// ConcurrencyLimiter object
type ConcurrencyLimiter struct {
limit int
tickets chan int
numInProgress int32
}
// NewConcurrencyLimiter allocates a new ConcurrencyLimiter
func NewConcurrencyLimiter(limit int) *ConcurrencyLimiter {
if limit <= 0 {
limit = DefaultLimit
}
// allocate a limiter instance
c := &ConcurrencyLimiter{
limit: limit,
tickets: make(chan int, limit),
}
// allocate the tickets:
for i := 0; i < c.limit; i++ {
c.tickets <- i
}
return c
}
// Execute adds a function to the execution queue.
// if num of go routines allocated by this instance is < limit
// launch a new go routine to execute job
// else wait until a go routine becomes available
func (c *ConcurrencyLimiter) Execute(job func()) int {
ticket := <-c.tickets
atomic.AddInt32(&c.numInProgress, 1)
go func() {
defer func() {
c.tickets <- ticket
atomic.AddInt32(&c.numInProgress, -1)
}()
// run the job
job()
}()
return ticket
}
// ExecuteWithTicket adds a job into an execution queue and returns a ticket id.
// if num of go routines allocated by this instance is < limit
// launch a new go routine to execute job
// else wait until a go routine becomes available
func (c *ConcurrencyLimiter) ExecuteWithTicket(job func(ticket int)) int {
ticket := <-c.tickets
atomic.AddInt32(&c.numInProgress, 1)
go func() {
defer func() {
c.tickets <- ticket
atomic.AddInt32(&c.numInProgress, -1)
}()
// run the job
job(ticket)
}()
return ticket
}
// Wait will block all the previously Executed jobs completed running.
//
// IMPORTANT: calling the Wait function while keep calling Execute leads to
// un-desired race conditions
func (c *ConcurrencyLimiter) Wait() {
for i := 0; i < c.limit; i++ {
_ = <-c.tickets
}
}
// GetNumInProgress returns a (racy) counter of how many go routines are active right now
func (c *ConcurrencyLimiter) GetNumInProgress() int32 {
return atomic.LoadInt32(&c.numInProgress)
}