-
Notifications
You must be signed in to change notification settings - Fork 10
/
xs.go
293 lines (244 loc) · 7.32 KB
/
xs.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package main
import (
"fmt"
"net"
"os"
"strings"
"sync"
"time"
schema "github.com/glycerine/goq/schema"
rpc "github.com/glycerine/rpc25519"
//"github.com/quic-go/quic-go"
)
var insecure = false
// ServerCallbackMgr handles interacting with the rpc25519
// server for transport.
type ServerCallbackMgr struct {
Srv *rpc.Server
cfg *Config
mut sync.Mutex
connMap map[string]*Nexus
jserv *JobServ
}
// Nexus tracks clients we have seen.
type Nexus struct {
Key string // e.g. "tcp://127.0.0.1:34343"
Nc net.Conn
Seqno uint64 // of the request, not yet incremented.
ReplyCh chan *rpc.Message
}
func (m *ServerCallbackMgr) removeClient(addr string) (del *Nexus) {
m.mut.Lock()
defer m.mut.Unlock()
var ok bool
del, ok = m.connMap[addr]
if ok {
delete(m.connMap, addr)
}
return
}
var ErrNotAvail = fmt.Errorf("addr not available in Mgr connMap")
func (m *ServerCallbackMgr) get(addr string) (*Nexus, error) {
m.mut.Lock()
defer m.mut.Unlock()
if !strings.Contains(addr, "://") {
addr = "tcp://" + addr
}
k, ok := m.connMap[addr]
if !ok {
return nil, ErrNotAvail
}
return k, nil
}
// addr is like localhost:8972
func NewServerCallbackMgr(addr string, cfg *Config) (m *ServerCallbackMgr, err error) {
//vv("top of NewServerCallbackMgr")
suf, err := StripNanomsgAddressPrefix(addr)
if err == nil {
addr = suf
}
tcp := insecure
if cfg.UseQUIC {
tcp = false
}
scfg := rpc.NewConfig()
scfg.ServerAddr = addr
scfg.TCPonly_no_TLS = tcp
scfg.UseQUIC = cfg.UseQUIC
scfg.CertPath = fixSlash(cfg.Home + "/.goq/certs")
scfg.PreSharedKeyPath = fixSlash(cfg.Home + "/.goq/goqclusterid")
scfg.ServerSendKeepAlive = 10 * time.Second
serverName := os.Getenv("GOQ_TESTNAME") // which test is not closing server?
s := rpc.NewServer(serverName, scfg)
m = &ServerCallbackMgr{
connMap: make(map[string]*Nexus),
Srv: s,
cfg: cfg,
}
// we now call start below separately, to avoid data race.
return m, err
}
func (m *ServerCallbackMgr) start() error {
s := m.Srv
gotAddr, err := s.Start()
_ = gotAddr
//vv("rpc server start got addr='%v'; err='%v'", gotAddr, err)
// Ready handles all callbacks from rpc25519.
s.Register2Func(m.Ready2)
s.Register1Func(m.Ready1)
return err
}
/* jea: never called, comment out. But some kind of gc might be needed
// with alot of workers in the future.
// notice disconnected client connections, return these in disco
func (m *ServerCallbackMgr) gcRegistry() (disco []*Nexus) {
active := m.Srv.ActiveClientConn() // []net.Conn
activeMap := make(map[string]bool)
for _, a := range active {
akey := netConnRemoteAddrAsKey(a)
activeMap[akey] = true
}
m.mut.Lock()
defer m.mut.Unlock()
// which do we have that are not active?
for k, cn := range m.connMap {
if !activeMap[k] {
disco = append(disco, cn)
delete(m.connMap, k)
}
}
return
}
*/
func (m *ServerCallbackMgr) register(clientConn net.Conn, seqno uint64, replyCh chan *rpc.Message) {
rkey := netConnRemoteAddrAsKey(clientConn)
//vv("registering client conn under rkey '%s'", rkey)
m.mut.Lock()
defer m.mut.Unlock()
c := &Nexus{Nc: clientConn, Seqno: seqno, ReplyCh: replyCh}
m.connMap[rkey] = c
}
func (m *ServerCallbackMgr) Close() {
//vv("SCM.Close() called")
m.mut.Lock()
defer m.mut.Unlock()
for _, c := range m.connMap {
//vv("closing c.Nc %T local='%v' remote='%v'", c.Nc, local(c.Nc), remote(c.Nc))
quicConn, ok := c.Nc.(*rpc.NetConnWrapper)
if ok {
//vv("sending quicConn.CloseWithError server shutdown.")
quicConn.Connection.CloseWithError(0, "server shutdown")
//vv("back from sending quicConn.CloseWithError server shutdown.")
} else {
//vv("sending Nc.Close()") // seen.
// Error accepting stream: INTERNAL_ERROR (local): write udp 100.86.202.68:2776->100.86.202.68:55685: use of closed network connection
c.Nc.Close()
//vv("done sending Nc.Close()") // seen.
}
}
}
func netConnRemoteAddrAsKey(nc net.Conn) string {
ra := nc.RemoteAddr()
return ra.Network() + "://" + ra.String()
}
func (m *ServerCallbackMgr) pushJobToClient(callID, addr string, j *Job) (key string, ok bool, err error) {
subject := j.Msg.String()
//vv("server: pushJobToClient: to addr:'%v' job='%v'; callID='%v'", addr, j.String(), callID)
nex, err := m.get(addr)
if err != nil {
panic(fmt.Sprintf("could not find cache net.Conn for addr '%s'", addr))
//return addr, false, err
}
jobSerz, err := m.cfg.jobToBytesWithStamp(j)
if err != nil {
return addr, false, err
}
//vv("jobSerz is %v bytes", len(jobSerz))
return m.pushToClient(callID, subject, nex, jobSerz)
}
func (m *ServerCallbackMgr) pushToClient(callID, subject string, nex *Nexus, by []byte) (key string, ok bool, err error) {
nc := nex.Nc
key = netConnRemoteAddrAsKey(nc)
//vv("pushToClient is doing m.Srv.SendMessage(); nex.Seqno=%v", nex.Seqno)
errWriteDur := time.Second * 5
err = m.Srv.SendMessage(callID, subject, key, by, nex.Seqno, &errWriteDur)
//vv("err from m.Srv.SendMessage() was '%v'", err)
if err == nil {
ok = true
} else {
vv("failed to send messsage to %s: %v\n", key, err)
nc.Close()
m.removeClient(key)
}
return
}
func (m *ServerCallbackMgr) Ready1(args *rpc.Message) {
_ = m.readyCommon(args)
}
func (m *ServerCallbackMgr) readyCommon(args *rpc.Message) *Job {
//vv("ServerCallbackMgr: Ready1() top. args.MID='%v'", args.MID)
clientConn := args.HDR.Nc
var job *Job
var err error
// harden against cross-cluster communication, where bytesToJob errors out.
defer func() {
if recover() != nil {
job = nil
err = fmt.Errorf("unknown recovered error on receive")
}
}()
// Read job submitted to the server
job, err = m.cfg.bytesToJob(args.JobSerz)
panicOn(err)
//if err != nil {
// return fmt.Errorf("error in ServerCallbackMgr.Ready() after CapnpToJob: '%v'", err)
//}
job.nc = clientConn
job.callid = args.HDR.CallID
job.callSeqno = args.HDR.Seqno
//vv("ServerCallbackMgr: Ready() sees incoming job: '%s'", job.String())
// NAT may have changed the address we have to reply to
remoteAfterNAT := remote(job.nc)
if job.Submitaddr != "" && remoteAfterNAT != job.Submitaddr {
//vv("updating job.Submitaddr from '%v' to '%v' to allow us to reply through NAT", job.Submitaddr, remoteAfterNAT)
job.Submitaddr = remoteAfterNAT
}
if job.Msg == schema.JOBMSG_REQUESTFORWORK {
if remoteAfterNAT != job.Workeraddr {
//vv("updating job.Workeraddr from '%v' to '%v' to allow us to reply through NAT", job.Workeraddr, remoteAfterNAT)
job.Workeraddr = remoteAfterNAT
}
}
m.register(clientConn, args.HDR.Seqno, nil)
select {
case m.jserv.FromRpcServer <- job:
case <-m.jserv.ListenerShutdown:
//vv("we see jserv.ListenerShutdown")
}
return job
}
func (m *ServerCallbackMgr) Ready2(args, reply *rpc.Message) error {
//vv("ServerCallbackMgr: Ready2() top. args.HDR='%v'", args.HDR)
job := m.readyCommon(args)
// wait for reply
select { // hung here in server when "goq sub" client stalls
case pReply := <-job.replyCh:
//vv("server Ready2() got pReply: '%v'", pReply)
reply.JobSerz = pReply.JobSerz
reply.JobErrs = pReply.JobErrs
case <-m.jserv.ListenerShutdown:
}
return nil
}
type localRemoteAddr interface {
RemoteAddr() net.Addr
LocalAddr() net.Addr
}
func remote(nc localRemoteAddr) string {
ra := nc.RemoteAddr()
return ra.Network() + "://" + ra.String()
}
func local(nc localRemoteAddr) string {
la := nc.LocalAddr()
return la.Network() + "://" + la.String()
}