-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.go
217 lines (186 loc) · 4.85 KB
/
pool.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package gipc
import (
"sync"
"time"
)
var clientIdRequest = &Message{
Data: []byte("client_id_request"),
MsgType: CLIENT_CONNECT_MSGTYPE,
}
func isClientIdRequest(msg *Message) bool {
return msg.MsgType == CLIENT_CONNECT_MSGTYPE && string(msg.Data) == string(clientIdRequest.Data)
}
func StartServerPool(config *ServerConfig) (*Server, error) {
//copy to prevent modification of the reference
configName := config.Name
//creates a server exclusively for listening for new connections
cms, err := NewServer(configName+"_manager", config)
if err != nil {
return nil, err
}
cms, err = cms.run(0)
if err != nil {
return nil, err
}
//create another server for the user to interface
s, err := NewServer(configName, config)
if err != nil {
return nil, err
}
s.Connections = &ConnectionPool{
Servers: []*Server{cms, s},
ServerConfig: config,
Logger: s.logger,
mutex: &sync.Mutex{},
}
go connectionListener(cms, s)
return s.run(1)
}
func connectionListener(cms *Server, s *Server) {
clientCount := 1
for {
msg, err := cms.Read()
if err != nil {
s.logger.Errorf("ConnectionPool.read err: %s", err)
s.dispatchError(err)
continue
}
if isClientIdRequest(msg) {
err = cms.Write(CLIENT_CONNECT_MSGTYPE, intToBytes(clientCount))
if err != nil {
continue
}
if clientCount == 1 {
//we already pre-provisioned the first client
clientCount++
continue
}
cms.logger.Infof("received a request to create a new client server %d", clientCount)
//ns, err2 := NewServer(fmt.Sprintf("%s%d", s.config.ServerConfig.Name, clientCount), s.config.ServerConfig)
ns, err2 := NewServer(s.config.ServerConfig.Name, s.config.ServerConfig)
if err2 != nil {
cms.logger.Errorf("encountered an error attempting to create a client server %d %s", clientCount, err2)
continue
}
go ns.run(clientCount)
clientCount++
s.Connections.mutex.Lock()
s.Connections.Servers = append(s.Connections.Servers, ns)
s.Connections.mutex.Unlock()
}
}
}
func StartClientPool(config *ClientConfig) (*Client, error) {
//copy to prevent modification of the reference
configName := config.Name
cm, err := NewClient(configName+"_manager", config)
if err != nil {
return nil, err
}
defer cm.Close()
cm, err = start(cm)
if err != nil {
return nil, err
}
err = cm.WriteMessage(clientIdRequest)
if err != nil {
return nil, err
}
for {
message, err2 := cm.ReadTimed(5 * time.Second)
if message == TimeoutMessage {
continue
} else if err2 != nil {
cm.logger.Debugf("StartClientPool err: %s", err)
continue
} else if message.MsgType != CLIENT_CONNECT_MSGTYPE {
continue
}
clientId := bytesToInt(message.Data)
if clientId > 0 {
cm.logger.Infof("Attempting to create a new Client %d", clientId)
cc, err3 := NewClient(configName, config)
if err3 != nil {
return nil, err3
}
cc.ClientId = clientId
return start(cc)
}
}
}
func (sm *ConnectionPool) getServers() []*Server {
sm.mutex.Lock()
servers := sm.Servers
sm.mutex.Unlock()
return servers
}
func (sm *ConnectionPool) MapExec(callback func(*Server), from string) {
servers := sm.getServers()
serverLen := len(servers)
serverOp := make(chan bool, serverLen)
for i, server := range servers {
//skip the first serverManager instance
if i == 0 {
continue
}
go func(s *Server) {
callback(s)
serverOp <- true
}(server)
}
n := 0
for n < serverLen-1 {
<-serverOp
n++
sm.Logger.Debugf("sm.%sfinished for server(%d)", from, n)
}
}
func (sm *ConnectionPool) Read(callback func(*Server, *Message, error)) {
sm.MapExec(func(s *Server) {
message, err := s.Read()
callback(s, message, err)
}, "Read")
}
// ReadTimed will call ReadTimed on all connections waiting for the slowest one to finish
func (sm *ConnectionPool) ReadTimed(duration time.Duration, callback func(*Server, *Message, error)) {
sm.MapExec(func(s *Server) {
message, err := s.ReadTimed(duration)
callback(s, message, err)
}, "ReadTimed")
}
// ReadTimed will call ReadTimed on all connections waiting for the fastest one to finish
func (sm *ConnectionPool) ReadTimedFastest(duration time.Duration, callback func(*Server, *Message, error)) {
wg := make(chan bool, len(sm.getServers()))
go func() {
sm.MapExec(func(s *Server) {
message, err := s.ReadTimed(duration)
callback(s, message, err)
wg <- true
}, "ReadTimedFastest")
}()
<-wg
}
func (sm *ConnectionPool) Close() {
servers := sm.getServers()
serverLen := len(servers)
serverOp := make(chan bool, serverLen)
var primary *Server
for i, server := range servers {
//the second server is the one we want to close last
if i == 1 {
primary = server
continue
}
go func(s *Server) {
s.close()
serverOp <- true
}(server)
}
n := 0
for n < serverLen-1 {
<-serverOp
n++
sm.Logger.Debugf("sm.Close finished for server(%d)", n)
}
primary.close()
}