forked from facebookincubator/dhcplb
/
update_servers.go
155 lines (141 loc) · 3.79 KB
/
update_servers.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
/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package dhcplb
import (
"github.com/golang/glog"
"net"
"time"
)
func (s *serverImpl) StartUpdatingServerList() {
glog.Infof("Starting to update server list...")
go s.updateServersContinuous()
}
func (s *serverImpl) updateServersContinuous() {
for {
config := s.getConfig()
stable, err := config.HostSourcer.GetStableServers()
if err != nil {
glog.Error(err)
}
rc, err := config.HostSourcer.GetRCServers()
if err != nil {
glog.Error(err)
}
s.handleUpdatedList(s.stableServers, stable)
s.handleUpdatedList(s.rcServers, rc)
// update lists after making/closing sockets
err = config.Algorithm.updateStableServerList(stable)
if err != nil {
glog.Errorf("Error updating stable server list: %s", err)
}
s.stableServers = stable
err = config.Algorithm.updateRCServerList(rc)
if err != nil {
glog.Errorf("Error updating RC server list: %s", err)
}
s.rcServers = rc
<-time.NewTimer(config.ServerUpdateInterval).C
}
}
func (s *serverImpl) handleUpdatedList(old, new []*DHCPServer) {
config := s.getConfig()
added, removed := diffServersList(old, new)
if len(added) > 0 || len(removed) > 0 {
glog.Info("Server list updated")
// open connections in newly added servers
makeConnections(old, new)
// close connections in removed servers after waiting for a timeout
// wait some time to be sure that there are no requests that will try
// to send to a server that already had its connection closed
time.AfterFunc(config.FreeConnTimeout, func() {
for _, old := range removed {
err := old.disconnect()
if err != nil {
glog.Errorf("Unable to close connection to %s", old)
}
}
})
} else {
// always makeConnections even if there was no change, so that sockets don't leak
makeConnections(old, new)
}
}
type serverKey struct {
// have to store address as string otherwise serverKey can't be used as map key
Address string
Port int
}
func diffServersList(original, updated []*DHCPServer) (added, removed []*DHCPServer) {
added = make([]*DHCPServer, 0)
removed = make([]*DHCPServer, 0)
// find servers that were not in original list
originalMap := make(map[serverKey]bool)
for _, s := range original {
key := serverKey{
s.Address.String(),
s.Port,
}
originalMap[key] = true
}
for _, new := range updated {
key := serverKey{
new.Address.String(),
new.Port,
}
if _, ok := originalMap[key]; !ok {
added = append(added, new)
}
}
// find servers that are no longer in the new list
newMap := make(map[serverKey]bool)
for _, s := range updated {
key := serverKey{
s.Address.String(),
s.Port,
}
newMap[key] = true
}
for _, old := range original {
key := serverKey{
old.Address.String(),
old.Port,
}
if _, ok := newMap[key]; !ok {
removed = append(removed, old)
}
}
return added, removed
}
// makeConnections takes an original list of servers, and a new one and either
// copies connections over from the old list or opens new connections
func makeConnections(original, updated []*DHCPServer) {
originalMap := make(map[serverKey]*net.UDPConn)
for _, s := range original {
key := serverKey{
s.Address.String(),
s.Port,
}
originalMap[key] = s.conn
}
// now copy connections into the new list if needed
for _, new := range updated {
key := serverKey{
new.Address.String(),
new.Port,
}
if conn, ok := originalMap[key]; ok && conn != nil {
new.conn = conn
} else {
err := new.connect()
if err != nil {
glog.Errorf("Unable to open socket to %s", new)
}
}
}
}