/
logic.go
109 lines (90 loc) · 2.49 KB
/
logic.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
package internal
import (
"fmt"
consul "github.com/hashicorp/consul/api"
"log"
"os"
"os/signal"
"syscall"
"time"
)
const (
RenewSessionTime = "5s"
NotLeaderPoolInterval = 1
LeaderPoolInterval = 3
)
var OsExit = os.Exit
type Clienter interface {
Agent() *consul.Agent
Session() *consul.Session
KV() *consul.KV
}
type KeyValuer interface {
Acquire(p *consul.KVPair, q *consul.WriteOptions) (bool, *consul.WriteMeta, error)
Release(p *consul.KVPair, q *consul.WriteOptions) (bool, *consul.WriteMeta, error)
}
type KvRetriever func(c Clienter) KeyValuer
func KvFromClient(c Clienter) KeyValuer {
return c.KV()
}
func ForWaitLeadership(client Clienter, kvRetriever KvRetriever, p *consul.KVPair, isLeader chan bool, h string) {
for {
WaitLeadership(client, kvRetriever, p, isLeader, h)
}
}
func WaitLeadership(client Clienter, kvRetriever KvRetriever, p *consul.KVPair, isLeader chan bool, h string) {
kv := kvRetriever(client)
leader, _, err := kv.Acquire(p, nil)
isLeader <- leader
if err != nil {
fmt.Printf("error trying to acquire leadership, %s\n", err)
}
if leader {
fmt.Printf("I'M THE LEADER %s-%d\n\n", h, os.Getpid())
t := time.NewTimer(LeaderPoolInterval * time.Second)
<-t.C
return
}
fmt.Printf("Im not the leader %s-%d\n\n", h, os.Getpid())
t := time.NewTimer(NotLeaderPoolInterval * time.Second)
<-t.C
}
func RenewSession(client Clienter, sessionID string, doneChan chan struct{}) {
// RenewPeriodic is used to periodically invoke Session until a doneChan is closed.
// This is used in a long running goroutine to ensure a session stays valid.
err := client.Session().RenewPeriodic(
RenewSessionTime,
sessionID,
nil,
doneChan,
)
if err != nil {
log.Fatalf("could not renew consul session, %s", err)
}
}
func ListenShutdown(c Clienter, kvRetriever KvRetriever, sig chan os.Signal, isLeader chan bool, p *consul.KVPair) {
var leader bool
var s os.Signal
signal.Notify(sig, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT)
for {
select {
case s = <-sig:
if leader {
kv := kvRetriever(c)
success, _, err := kv.Release(p, nil)
log.Printf("Release leadership from client \n")
if err != nil || !success {
log.Printf("could not release session")
OsExit(1)
}
} else {
log.Printf("Client was not leader, so leadership should not be released \n")
}
log.Printf("Shutting down system due to %s \n", s)
OsExit(0)
// Add leader values channel processing to avoid locks
case leader = <-isLeader:
default:
}
}
}