-
Notifications
You must be signed in to change notification settings - Fork 0
/
keeper.go
64 lines (57 loc) · 1.18 KB
/
keeper.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
package gocaptain
import (
"time"
)
type ServiceKeeper struct {
client *CaptainClient
lastKeepTs int64
KeepAlive int64
CheckInterval time.Duration
Stop chan bool
started bool
}
func NewServiceKeeper(client *CaptainClient) *ServiceKeeper {
return &ServiceKeeper{client, 0, 10, 1000, make(chan bool), false}
}
func (this *ServiceKeeper) Start() {
this.started = true
for {
this.client.ShuffleOrigin()
this.watch()
this.keep()
select {
case <-this.Stop:
break
case <-time.After(this.CheckInterval * time.Millisecond):
}
}
}
func (this *ServiceKeeper) watch() {
defer SilentOnPanic()
flags := this.client.CheckDirty()
if flags[0] {
dirties := this.client.CheckServiceVersions()
for _, name := range dirties {
this.client.ReloadService(name)
}
}
if flags[1] {
dirties := this.client.CheckKvVersions()
for _, key := range dirties {
this.client.ReloadKv(key)
}
}
}
func (this *ServiceKeeper) keep() {
defer SilentOnPanic()
now := time.Now().Unix()
if now-this.lastKeepTs > this.KeepAlive {
this.client.KeepService()
this.lastKeepTs = now
}
}
func (this *ServiceKeeper) Quit() {
if this.started {
this.Stop <- true
}
}