-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
218 lines (185 loc) · 5.88 KB
/
handler.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package hardware
import (
"sync"
"sync/atomic"
"time"
)
/*
Feature-spec of the handler:
- can handle async request, for example concurrent users or one user toggling power in the frontend fast
- ideal for async job requests, like frontend
- Acts synchronous if one power job is awaited after the other
- ideal for normal scripting
Time to complete:
(n) synchronous requests -> n * repeats * 20 ms
(n) asynchronous requests -> n * (repeats * 20 ms + cooldown) - cooldown
*/
type jobQueueType struct {
JobQueue []PowerJob
m sync.RWMutex
}
type resultQueueType struct {
JobResults []JobResult
m sync.RWMutex
}
var jobQueue = jobQueueType{
JobQueue: make([]PowerJob, 0),
}
var jobResults = resultQueueType{
JobResults: make([]JobResult, 0),
}
// Whether a job daemon loop is already running
// var daemonRunning bool
var daemonRunning atomic.Value
// Contains the queue for all pending jobs
// var jobQueue []PowerJob = make([]PowerJob, 0)
// temporarely stores the result of each executed job
// var jobResults []JobResult = make([]JobResult, 0)
var jobsWithErrorInHandlerCount atomic.Value
// Time to be waited after each job (in milliseconds)
const cooldown = 500
// Main interface for interacting with the queuing system
// Usage: SetPower("s1", true)
// Waits until all jobs are completed, can return an error
func SetPower(switchName string, powerOn bool) error {
uniqueId := time.Now().UnixNano()
addJobToQueue(switchName, powerOn, uniqueId)
result := consumeResult(uniqueId)
return result.Error
}
// Used for adding a job to a queue, keeps track of daemons and spawns them if needed
// Waits until the daemon quits, waiting for all (and the new) job(s) to be completed.
func addJobToQueue(switchId string, turnOn bool, id int64) {
item := PowerJob{Switch: switchId, Power: turnOn, Id: id}
jobQueue.m.Lock()
jobQueue.JobQueue = append(jobQueue.JobQueue, item)
jobQueue.m.Unlock()
if !daemonRunning.Load().(bool) {
jobsWithErrorInHandlerCount.Store(0)
jobResults.m.Lock()
jobResults.JobResults = make([]JobResult, 0)
jobResults.m.Unlock()
daemonRunning.Store(true)
ch := make(chan bool)
go jobDaemon(ch)
// TODO: Evaluate whether to replace with waitgroup
for {
select {
case <-ch:
return
default:
time.Sleep(time.Millisecond * 50)
if hasFinished(id) {
return
}
}
}
} else {
for daemonRunning.Load().(bool) && !hasFinished(id) {
time.Sleep(time.Millisecond * 50)
}
}
}
// Executes each job one after another
// Jobs can be added while the daemon is running
// If all jobs are completed, the daemon terminates itself in order to save resources
// If a new job should be executed whilst no daemon is active, a new daemon is created.
func jobDaemon(ch chan bool) {
for {
jobQueue.m.RLock() // Lock for getting length
length := len(jobQueue.JobQueue)
jobQueue.m.RUnlock() // Unlock if condition
if length == 0 {
daemonRunning.Store(false)
ch <- true
break
}
jobQueue.m.RLock()
currentJob := jobQueue.JobQueue[0]
jobQueue.m.RUnlock()
// Call the function which interacts with the hardware
err := setPowerOnAllNodes(currentJob.Switch, currentJob.Power)
jobResults.m.Lock()
jobResults.JobResults = append(jobResults.JobResults, JobResult{Id: currentJob.Id, Error: err})
jobResults.m.Unlock()
// Increase `job-with-error count` if the job failed
if err != nil {
jobsWithErrorInHandlerCount.Store(jobsWithErrorInHandlerCount.Load().(int) + 1)
}
// Removes the first value in the queue, in this case the freshly completed job
var tempQueue []PowerJob = make([]PowerJob, 0)
jobQueue.m.RLock() // Lock while loop is iterating
for i, item := range jobQueue.JobQueue {
if i != 0 {
tempQueue = append(tempQueue, item)
}
}
jobQueue.m.RUnlock() // Unlock loop
jobQueue.m.Lock()
jobQueue.JobQueue = tempQueue
jobQueue.m.Unlock()
// Only sleep if other jobs are in the current queue
jobQueue.m.RLock() // Lock for the if condition
if len(jobQueue.JobQueue) > 0 {
time.Sleep(cooldown * time.Millisecond)
}
jobQueue.m.RUnlock() // Unlock if condition
}
}
// This `garbage collector` consumes the result after it has been passed to the client
// If a client cancels a request, the according response is not consumed. This response is cleared when a new handler is launched
// After removing the desired result from the slice, it is returned for further processing
func consumeResult(id int64) JobResult {
var resultsTemp []JobResult = make([]JobResult, 0)
var returnValue JobResult
jobResults.m.Lock() // Lock for iteration
for _, result := range jobResults.JobResults {
if result.Id != id {
resultsTemp = append(resultsTemp, result)
} else {
returnValue = result
}
}
jobResults.JobResults = resultsTemp
jobResults.m.Unlock()
return returnValue
}
// Checks if the job with the current id has finished
func hasFinished(id int64) bool {
jobResults.m.RLock()
defer jobResults.m.RUnlock()
for _, result := range jobResults.JobResults {
if result.Id == id {
return true
}
}
return false
}
func Init() {
// Needed for initializing atomics
// Initialize thread-safe variables, for more info, look at the top for mutexes
jobsWithErrorInHandlerCount.Store(0)
daemonRunning.Store(false)
}
// Returns the number of currently pending jobs in the queue
func GetPendingJobCount() int {
jobQueue.m.RLock()
defer jobQueue.m.RUnlock()
return len(jobQueue.JobQueue)
}
// Returns the number of registered failed jobs of the last running daemon (can also be the current daemon)
func GetJobsWithErrorInHandler() uint16 {
return uint16(jobsWithErrorInHandlerCount.Load().(int))
}
// Returns the current state of the job queue
func GetPendingJobs() []PowerJob {
jobQueue.m.RLock()
defer jobQueue.m.RUnlock()
return jobQueue.JobQueue
}
// Returns the current state of the results queue
func GetResults() []JobResult {
jobResults.m.RLock()
defer jobResults.m.RUnlock()
return jobResults.JobResults
}