/
suspensions.go
46 lines (40 loc) · 1.13 KB
/
suspensions.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
package server
import (
"sync"
)
// suspender is a list that keep track of suspender orchestrators
// and the count until which they are suspended
type suspender struct {
mu sync.Mutex
list map[string]int // list of orchestrator => refresh count at which the orchestrator is no longer suspended
count int
}
// newSuspender returns the pointer to a new Suspender instance
func newSuspender() *suspender {
return &suspender{
list: make(map[string]int),
}
}
// suspend an orchestrator for 'penalty' refreshes
func (s *suspender) suspend(orch string, penalty int) {
s.mu.Lock()
defer s.mu.Unlock()
s.list[orch] += penalty
}
// Suspended returns a non-zero value if the orchestrator is suspended
// 'orch' is the service URI of the orchestrator
// The value returned is the suspension penalty associated with the orchestrator whereby lower is better
func (s *suspender) Suspended(orch string) int {
s.mu.Lock()
defer s.mu.Unlock()
if s.list[orch] < s.count {
delete(s.list, orch)
}
return s.list[orch]
}
// signalRefresh increases Suspender.count
func (s *suspender) signalRefresh() {
s.mu.Lock()
defer s.mu.Unlock()
s.count++
}