This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
endpoint.go
162 lines (135 loc) · 3.31 KB
/
endpoint.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package serversets
import (
"encoding/json"
"fmt"
"sync"
"time"
"github.com/samuel/go-zookeeper/zk"
)
// An Endpoint is a service (host and port) registered on Zookeeper
// to be discovered by clients/watchers.
type Endpoint struct {
*ServerSet
PingRate time.Duration // default/initial is 1 second
CloseEvent chan struct{}
done chan struct{}
wg sync.WaitGroup
host string
port int
key string
ping func() error
alive bool
}
// RegisterEndpoint registers a host and port as alive. It creates the appropriate
// Zookeeper nodes and watchers will be notified this server/endpoint is available.
func (ss *ServerSet) RegisterEndpoint(host string, port int, ping func() error) (*Endpoint, error) {
endpoint := &Endpoint{
ServerSet: ss,
PingRate: time.Second,
CloseEvent: make(chan struct{}, 1),
done: make(chan struct{}),
host: host,
port: port,
ping: ping,
alive: true,
}
if ping != nil {
endpoint.alive = endpoint.ping() == nil
}
connection, sessionEvents, err := ss.connectToZookeeper()
if err != nil {
return nil, err
}
err = endpoint.update(connection)
if err != nil {
return nil, err
}
// spawn goroutine to deal with connection/session issues.
endpoint.wg.Add(1)
go func() {
defer endpoint.wg.Done()
for {
select {
case event := <-sessionEvents:
if event.Type == zk.EventSession && event.State == zk.StateExpired {
connection.Close()
connection = nil
}
case <-endpoint.done:
connection.Close()
return
}
if connection == nil {
connection, sessionEvents, err = ss.connectToZookeeper()
if err != nil {
panic(fmt.Errorf("unable to reconnect to zookeeper after session expired: %v", err))
}
err = endpoint.update(connection)
if err != nil {
panic(fmt.Errorf("unable to reregister endpoint after session expired: %v", err))
}
}
}
}()
if ping != nil {
endpoint.wg.Add(1)
go func() {
defer endpoint.wg.Done()
for {
select {
case <-time.After(endpoint.PingRate):
case <-endpoint.done:
return
}
alive := endpoint.ping() == nil
if alive != endpoint.alive {
endpoint.alive = alive
err := endpoint.update(connection)
if err != nil {
panic(fmt.Errorf("unable to reregister after ping change: %v", err))
}
}
}
}()
}
return endpoint, nil
}
// Close blocks until the client connection to Zookeeper is closed.
// If already called, will simply return, even if in the process of closing.
func (ep *Endpoint) Close() {
select {
case <-ep.done:
return
default:
}
close(ep.done)
ep.wg.Wait()
ep.CloseEvent <- struct{}{}
return
}
func (ep *Endpoint) update(connection *zk.Conn) error {
// don't create/remove the node if we're dead
if !ep.alive {
if ep.key != "" {
err := connection.Delete(ep.key, 0)
ep.key = ""
return err
}
return nil
}
entityData, _ := json.Marshal(newEntity(ep.host, ep.port))
var err error
ep.key, err = ep.ServerSet.registerEndpoint(connection, entityData)
return err
}
func (ss *ServerSet) registerEndpoint(connection *zk.Conn, data []byte) (string, error) {
err := ss.createFullPath(connection)
if err != nil {
return "", err
}
return connection.Create(
ss.directoryPath()+"/"+MemberPrefix,
data,
zk.FlagEphemeral|zk.FlagSequence,
zk.WorldACL(zk.PermAll))
}