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

pkg/cmd/termination/handler.go   A

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
dl 0
loc 29
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A termination.*Handler.SignalTermination 0 9 2
A termination.*Handler.ShouldTerminate 0 5 1
A termination.NewTerminationHandler 0 2 1
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