/
semaphore.go
72 lines (56 loc) · 1.47 KB
/
semaphore.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
package semaphore
import (
"context"
sem "github.com/marusama/semaphore"
"google.golang.org/grpc/codes"
"v/errs"
)
var (
parallel = 1
semaphore *sem.Semaphore
)
type (
// Semaphore counting resizable semaphore synchronization primitive.
Semaphore = sem.Semaphore
)
// SetParallel sets a given number to the parallelism number.
func SetParallel(i int) error {
const op = "semaphore.SetParallel"
if i < 1 {
return &errs.Error{Op: op, Code: codes.InvalidArgument, Message: "must be 1 or more"}
}
parallel = i
return nil
}
// New initializes a new instance of the Semaphore, specifying the maximum number of concurrent entries.
func New(limit int) sem.Semaphore {
return sem.New(limit)
}
// Acquire enters the semaphore a specified number of times, blocking only until ctx is done.
func Acquire(ctx context.Context, n int) error {
return (*get()).Acquire(ctx, n)
}
// TryAcquire acquires the semaphore without blocking.
func TryAcquire(n int) bool {
return (*get()).TryAcquire(n)
}
// Release exits the semaphore a specified number of times and returns the previous count.
func Release(n int) int {
return (*get()).Release(n)
}
// GetLimit returns current semaphore limit.
func GetLimit() int {
return (*get()).GetLimit()
}
// GetCount returns current number of occupied entries in semaphore.
func GetCount() int {
return (*get()).GetCount()
}
func get() *sem.Semaphore {
if semaphore != nil {
return semaphore
}
s := sem.New(parallel)
semaphore = &s
return semaphore
}