-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
172 lines (132 loc) · 2.79 KB
/
manager.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package testplant
import (
"math"
"sync"
"time"
)
type Manager struct {
setB chan float64
newMessage chan struct{}
// Communication with workers
processed chan uint
// State
// Workers -- they need concurrency
workers []*Worker // Active workers
workersMutex sync.Mutex
queue *Queue // Pending work for workers
x, y uint
mu_p float64
// Beta needs a concurrency primitive
beta uint
betaMutex sync.Mutex
// Derivative keep, also needing a concurrency primitive
dx, dy uint
dTimestamp time.Time
dMutex sync.Mutex
// Settings
mu_p0, sigma_p0 uint
unit time.Duration
}
func NewManager(mu_p0, sigma_p0 uint, unit time.Duration) *Manager {
m := &Manager{
setB: make(chan float64),
newMessage: make(chan struct{}),
processed: make(chan uint),
workers: []*Worker{},
queue: NewQueue(),
mu_p0: mu_p0,
sigma_p0: sigma_p0,
unit: unit,
dTimestamp: time.Now(),
}
go m.run()
return m
}
func (m *Manager) run() {
for {
select {
case b := <-m.setB:
m.setBeta(bToBeta(b))
case p, ok := <-m.newMessage:
if !ok {
close(m.queue.Send)
return
}
m.queue.Send <- p
m.x++
m.dMutex.Lock()
m.dx++
m.dMutex.Unlock()
case d := <-m.processed:
// Update median and total
nmu_p := m.mu_p
nmu_p *= float64(m.y)
nmu_p += float64(d)
m.y++
nmu_p /= float64(m.y)
// Changed atomically so to avoid concurrency issues
m.mu_p = nmu_p
m.dMutex.Lock()
m.dy++
m.dMutex.Unlock()
}
}
}
func (m *Manager) SetB() chan float64 {
return m.setB
}
func (m *Manager) Message() chan struct{} {
return m.newMessage
}
func (m *Manager) Add(messages uint) {
m.queue.Add(messages)
}
func (m *Manager) setBeta(beta uint) {
m.betaMutex.Lock()
m.beta = beta
m.betaMutex.Unlock()
go func() {
m.workersMutex.Lock()
for len(m.workers) > int(beta) {
go m.workers[len(m.workers)-1].Kill()
m.workers = m.workers[:len(m.workers)-1]
}
for len(m.workers) < int(beta) {
m.workers = append(m.workers, NewWorker(m.queue, m.processed, m.mu_p0, m.sigma_p0, m.unit))
}
m.workersMutex.Unlock()
}()
}
func (m *Manager) DXY(unit time.Duration) (dx, dy float64) {
m.dMutex.Lock()
now := time.Now()
elapsed := now.Sub(m.dTimestamp)
m.dTimestamp = now
elapsed /= unit
dx, dy = float64(m.dx)/float64(elapsed), float64(m.dy)/float64(elapsed)
m.dx, m.dy = 0, 0
m.dMutex.Unlock()
return
}
func (m *Manager) X() uint {
return m.x
}
func (m *Manager) Y() uint {
return m.y
}
func (m *Manager) XmY() uint {
return m.x - m.y
}
func (m *Manager) MuP() (float64, bool) {
return m.mu_p, true
}
func (m *Manager) Beta() uint {
return uint(len(m.workers))
}
func (m *Manager) Q() uint {
return m.queue.pending
}
func bToBeta(b float64) uint {
// Round either way
return uint(math.Round(b))
}