-
Notifications
You must be signed in to change notification settings - Fork 1
/
moderator-core.go
116 lines (98 loc) · 2.38 KB
/
moderator-core.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
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
/*
© 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package parl
import (
"fmt"
"sync"
)
const (
defaultParallelism = 20
)
/*
ModeratorCore invokes functions at a limited level of parallelism.
ModeratorCore is a ticketing system.
ModeratorCore does not have a cancel feature.
m := NewModeratorCore(20, ctx)
m.Do(func() (err error) { // waiting here for a ticket
// got a ticket!
…
return or panic // ticket automatically returned
m.String() → waiting: 2(20)
*/
type ModeratorCore struct {
parallelism uint64
cond *sync.Cond
active uint64 // behind lock
waiting uint64 // behind lock
}
// moderatorCore is a parl-private version of ModeratorCore
type moderatorCore struct {
*ModeratorCore
}
// NewModerator creates a new Moderator used to limit parallelism
func NewModeratorCore(parallelism uint64) (mo *ModeratorCore) {
if parallelism < 1 {
parallelism = defaultParallelism
}
return &ModeratorCore{
parallelism: parallelism,
cond: sync.NewCond(&sync.Mutex{}),
}
}
// Do calls fn limited by the moderator’s parallelism.
// Do blocks until a ticket is available
// Do uses the same thread.
func (mo *ModeratorCore) Do(fn func()) {
mo.getTicket() // blocking
defer mo.returnTicket() // we will always get a ticket, and it should be returned
if fn != nil {
fn()
}
}
func (mo *ModeratorCore) getTicket() {
mo.cond.L.Lock()
defer mo.cond.L.Unlock()
isWaiting := false
for {
if mo.active < mo.parallelism {
mo.active++
// maintain waiting counter
if isWaiting {
mo.waiting--
}
return
}
// maintain waiting counter
if !isWaiting {
isWaiting = true
mo.waiting++
}
// block until cond.Notify or cond.Broadcast
mo.cond.Wait()
}
}
func (mo *ModeratorCore) returnTicket() {
mo.cond.L.Lock()
defer mo.cond.Signal()
defer mo.cond.L.Unlock()
mo.active--
}
func (mo *ModeratorCore) Status() (parallelism uint64, active uint64, waiting uint64) {
parallelism = mo.parallelism
mo.cond.L.Lock()
defer mo.cond.L.Unlock()
active = mo.active
waiting = mo.waiting
return
}
func (mo *ModeratorCore) String() (s string) {
parallelism, active, waiting := mo.Status()
if active < parallelism {
s = fmt.Sprintf("available: %d(%d)", parallelism-active, parallelism)
} else {
s = fmt.Sprintf("waiting: %d(%d)", waiting, parallelism)
}
return
}