Total Lines | 29 |
Duplicated Lines | 0 % |
Changes | 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 |