/
server.go
153 lines (144 loc) · 3.57 KB
/
server.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
package main
import (
"errors"
"io"
"sync"
"github.com/Symantec/Dominator/lib/log"
"github.com/Symantec/Dominator/lib/mdb"
"github.com/Symantec/Dominator/lib/srpc"
"github.com/Symantec/Dominator/proto/mdbserver"
)
type rpcType struct {
currentMdb *mdb.Mdb
logger log.Logger
rwMutex sync.RWMutex
// Protected by lock.
updateChannels map[*srpc.Conn]chan<- mdbserver.MdbUpdate
}
func startRpcd(logger log.Logger) *rpcType {
rpcObj := &rpcType{
logger: logger,
updateChannels: make(map[*srpc.Conn]chan<- mdbserver.MdbUpdate),
}
srpc.RegisterName("MdbServer", rpcObj)
return rpcObj
}
func (t *rpcType) GetMdbUpdates(conn *srpc.Conn) error {
updateChannel := make(chan mdbserver.MdbUpdate, 10)
t.rwMutex.Lock()
t.updateChannels[conn] = updateChannel
t.rwMutex.Unlock()
defer func() {
close(updateChannel)
t.rwMutex.Lock()
delete(t.updateChannels, conn)
t.rwMutex.Unlock()
}()
if t.currentMdb != nil {
mdbUpdate := mdbserver.MdbUpdate{MachinesToAdd: t.currentMdb.Machines}
if err := conn.Encode(mdbUpdate); err != nil {
return err
}
if err := conn.Flush(); err != nil {
return err
}
}
closeChannel := conn.GetCloseNotifier()
for {
var err error
select {
case mdbUpdate := <-updateChannel:
if isEmptyUpdate(mdbUpdate) {
t.logger.Printf("Queue for: %s is filling up: dropping client")
return errors.New("update queue too full")
}
if err = conn.Encode(mdbUpdate); err != nil {
break
}
if err = conn.Flush(); err != nil {
break
}
case <-closeChannel:
break
}
if err != nil {
if err != io.EOF {
t.logger.Println(err)
return err
} else {
return nil
}
}
}
}
func (t *rpcType) pushUpdateToAll(old, new *mdb.Mdb) {
t.currentMdb = new
updateChannels := t.getUpdateChannels()
if len(updateChannels) < 1 {
return
}
mdbUpdate := mdbserver.MdbUpdate{}
if old == nil {
old = &mdb.Mdb{}
}
oldMachines := make(map[string]mdb.Machine, len(old.Machines))
for _, machine := range old.Machines {
oldMachines[machine.Hostname] = machine
}
for _, newMachine := range new.Machines {
if oldMachine, ok := oldMachines[newMachine.Hostname]; ok {
if !newMachine.Compare(oldMachine) {
mdbUpdate.MachinesToUpdate = append(mdbUpdate.MachinesToUpdate,
newMachine)
}
} else {
mdbUpdate.MachinesToAdd = append(mdbUpdate.MachinesToAdd,
newMachine)
}
}
for _, machine := range new.Machines {
delete(oldMachines, machine.Hostname)
}
for name := range oldMachines {
mdbUpdate.MachinesToDelete = append(mdbUpdate.MachinesToDelete, name)
}
if isEmptyUpdate(mdbUpdate) {
t.logger.Println("Ignoring empty update")
return
}
for _, channel := range updateChannels {
sendUpdate(channel, mdbUpdate)
}
}
func (t *rpcType) getUpdateChannels() []chan<- mdbserver.MdbUpdate {
t.rwMutex.RLock()
defer t.rwMutex.RUnlock()
channels := make([]chan<- mdbserver.MdbUpdate, 0, len(t.updateChannels))
for _, channel := range t.updateChannels {
channels = append(channels, channel)
}
return channels
}
func isEmptyUpdate(mdbUpdate mdbserver.MdbUpdate) bool {
if len(mdbUpdate.MachinesToAdd) > 0 {
return false
}
if len(mdbUpdate.MachinesToUpdate) > 0 {
return false
}
if len(mdbUpdate.MachinesToDelete) > 0 {
return false
}
return true
}
func sendUpdate(channel chan<- mdbserver.MdbUpdate,
mdbUpdate mdbserver.MdbUpdate) {
defer func() { recover() }()
if cap(channel)-len(channel) < 2 {
// Not enough room for an update and a possible "too much" message next
// time around: send a "too much" message now.
channel <- mdbserver.MdbUpdate{}
return
}
channel <- mdbUpdate
}