Passed
Push — master ( 6ba115...bb6332 )
by Stefano
01:37
created

termination.*Handler.SignalTermination   A

Complexity

Conditions 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
package termination
2
3
import "sync"
4
5
func NewTerminationHandler(attempts int) *Handler {
6
	return &Handler{terminationAttemptsLeft: attempts}
7
}
8
9
type Handler struct {
10
	terminationAttemptsLeft int
11
	mx                      sync.RWMutex
12
}
13
14
func (h *Handler) SignalTermination() {
15
	h.mx.Lock()
16
	defer h.mx.Unlock()
17
18
	if h.terminationAttemptsLeft <= 0 {
19
		return
20
	}
21
22
	h.terminationAttemptsLeft--
23
}
24
25
func (h *Handler) ShouldTerminate() bool {
26
	h.mx.RLock()
27
	defer h.mx.RUnlock()
28
29
	return h.terminationAttemptsLeft <= 0
30
}
31