-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.go
477 lines (423 loc) · 10.7 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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package ksbus
import (
"fmt"
"net"
"net/http"
"net/rpc"
"net/url"
"time"
"encoding/gob"
"github.com/kamalshkeir/kmap"
"github.com/kamalshkeir/ksmux"
"github.com/kamalshkeir/ksmux/ws"
"github.com/kamalshkeir/lg"
)
type Server struct {
ID string
Address string
Path string
Bus *Bus
App *ksmux.Router
busMidws []func(ksmux.Handler) ksmux.Handler
onWsClose func(connID string)
onDataWS func(data map[string]any, conn *ws.Conn, originalRequest *http.Request) error
onServerData []func(data any, conn *ws.Conn)
onId func(data map[string]any)
sendToServerConnections *kmap.SafeMap[string, *ws.Conn]
beforeUpgradeWs func(r *http.Request) bool
rpcServer *rpc.Server
idConnRPC *kmap.SafeMap[string, *RPCConn]
rpcMaxQueueSize int
}
type RPCConn struct {
Id string
msgChan chan map[string]any
}
type WithRpc struct {
}
type ServerOpts struct {
ID string
Address string
BusPath string
BusMidws []func(ksmux.Handler) ksmux.Handler
OnWsClose func(connID string)
OnDataWS func(data map[string]any, conn *ws.Conn, originalRequest *http.Request) error
OnServerData []func(data any, conn *ws.Conn)
OnId func(data map[string]any)
OnUpgradeWs func(r *http.Request) bool
WithRPCAddress string
WithOtherRouter *ksmux.Router
WithOtherBus *Bus
}
func NewDefaultServerOptions() ServerOpts {
return ServerOpts{
ID: GenerateUUID(),
Address: "localhost:9313",
BusPath: "/ws/bus",
WithOtherRouter: ksmux.New(),
WithOtherBus: New(),
OnUpgradeWs: func(r *http.Request) bool { return true },
}
}
func NewServer(options ...ServerOpts) *Server {
var opts ServerOpts
if len(options) == 0 {
opts = NewDefaultServerOptions()
} else {
opts = options[0]
}
if opts.ID == "" {
opts.ID = GenerateUUID()
}
if opts.Address == "" {
opts.Address = "localhost:9313"
}
if opts.BusPath == "" {
opts.BusPath = "/ws/bus"
}
if opts.WithOtherBus == nil {
opts.WithOtherBus = New()
}
if opts.OnUpgradeWs != nil {
ws.DefaultUpgraderKSMUX.CheckOrigin = opts.OnUpgradeWs
}
if opts.WithOtherRouter == nil {
opts.WithOtherRouter = ksmux.New(ksmux.Config{
Address: opts.Address,
})
} else {
if opts.WithOtherRouter.Address() != "" {
opts.Address = opts.WithOtherRouter.Address()
} else if opts.WithOtherRouter.Config.Domain != "" {
opts.Address = opts.WithOtherRouter.Config.Domain
}
}
if len(opts.OnServerData) == 0 {
opts.OnServerData = []func(data any, conn *ws.Conn){}
}
server := Server{
ID: opts.ID,
Address: opts.Address,
Path: opts.BusPath,
Bus: opts.WithOtherBus,
App: opts.WithOtherRouter,
sendToServerConnections: kmap.New[string, *ws.Conn](20),
onWsClose: opts.OnWsClose,
onDataWS: opts.OnDataWS,
onServerData: opts.OnServerData,
onId: opts.OnId,
beforeUpgradeWs: opts.OnUpgradeWs,
idConnRPC: kmap.New[string, *RPCConn](10),
rpcMaxQueueSize: 1000,
}
if len(opts.BusMidws) > 0 {
server.busMidws = opts.BusMidws
}
if opts.WithRPCAddress != "" {
err := server.EnableRPC(opts.WithRPCAddress)
if err != nil {
lg.Fatal("Failed to enable RPC:", "err", err)
}
}
server.handleWS()
return &server
}
func (s *Server) OnWsClose(fn func(connID string)) {
s.onWsClose = fn
}
func (s *Server) OnServerData(fn func(data any, conn *ws.Conn)) {
s.onServerData = append(s.onServerData, fn)
}
func (s *Server) OnDataWs(fn func(data map[string]any, conn *ws.Conn, originalRequest *http.Request) error) {
s.onDataWS = fn
}
func (s *Server) OnId(fn func(data map[string]any)) {
s.onId = fn
}
func (s *Server) WithPprof(path ...string) {
s.App.WithPprof(path...)
}
func (s *Server) WithMetrics(httpHandler http.Handler, path ...string) {
s.App.WithMetrics(httpHandler, path...)
}
func (s *Server) Subscribe(topic string, fn func(data map[string]any, unsub Unsub)) (unsub Unsub) {
return s.Bus.Subscribe(topic, fn, func(data map[string]any) {
id, okID := data["id"]
if eventID, ok := data["event_id"]; ok && okID && id == s.ID {
s.Publish(eventID.(string), map[string]any{
"ok": "done",
"from": s.ID,
})
}
delete(data, "event_id")
})
}
func (s *Server) Unsubscribe(topic string) {
s.Bus.Unsubscribe(topic)
}
func (srv *Server) Publish(topic string, data map[string]any) {
if _, ok := data["from"]; !ok {
data["from"] = srv.ID
}
data["topic"] = topic
srv.Bus.Publish(topic, data)
}
func (s *Server) PublishToID(id string, data map[string]any) {
if _, ok := data["from"]; !ok {
data["from"] = s.ID
}
if rpcConn, ok := s.idConnRPC.Get(id); ok {
msg := map[string]any{
"to_id": id,
"from": data["from"],
}
for k, v := range data {
if k != "to_id" && k != "from" {
msg[k] = v
}
}
select {
case rpcConn.msgChan <- msg:
default:
<-rpcConn.msgChan
rpcConn.msgChan <- msg
}
return
}
s.Bus.PublishToID(id, data)
}
func (s *Server) PublishWaitRecv(topic string, data map[string]any, onRecv func(data map[string]any), onExpire func(eventId string, topic string)) {
if _, ok := data["from"]; !ok {
data["from"] = s.ID
}
data["topic"] = topic
eventId := GenerateUUID()
data["event_id"] = eventId
done := make(chan struct{})
subs := s.Subscribe(eventId, func(data map[string]any, unsub Unsub) {
done <- struct{}{}
if onRecv != nil {
onRecv(data)
}
unsub.Unsubscribe()
})
s.Publish(topic, data)
free:
for {
select {
case <-done:
break free
case <-time.After(500 * time.Millisecond):
if onExpire != nil {
onExpire(eventId, topic)
}
subs.Unsubscribe()
break free
}
}
}
func (s *Server) PublishToIDWaitRecv(id string, data map[string]any, onRecv func(data map[string]any), onExpire func(eventId string, toID string)) {
if _, ok := data["from"]; !ok {
data["from"] = s.ID
}
eventId := GenerateUUID()
data["event_id"] = eventId
done := make(chan struct{})
subs := s.Subscribe(eventId, func(data map[string]any, unsub Unsub) {
done <- struct{}{}
if onRecv != nil {
onRecv(data)
}
unsub.Unsubscribe()
})
s.PublishToID(id, data)
free:
for {
select {
case <-done:
break free
case <-time.After(500 * time.Millisecond):
if onExpire != nil {
onExpire(eventId, id)
}
subs.Unsubscribe()
break free
}
}
}
func (s *Server) RemoveTopic(topic string) {
s.Bus.RemoveTopic(topic)
}
func (s *Server) PublishToServer(addr string, data map[string]any, secure ...bool) error {
sch := "ws"
if len(secure) > 0 && secure[0] {
sch = "wss"
}
u := url.URL{Scheme: sch, Host: addr, Path: s.Path}
conn, ok := s.sendToServerConnections.Get(addr)
if !ok {
var err error
conn, _, err = ws.DefaultDialer.Dial(u.String(), nil)
if err != nil {
lg.Printfs("rdSendToServer Dial %s error:%v\n", u.String(), err)
return err
}
}
dd := map[string]any{
"action": "server_message",
"addr": addr,
"data": data,
"via_server": s.ID,
}
if err := conn.WriteJSON(dd); err != nil {
lg.Printfs("rdSendToServer WriteJSON on %s error:%v\n", u.String(), err)
return err
}
return nil
}
// RUN
func (s *Server) Run() {
s.App.Run()
}
func (s *Server) RunTLS() {
s.App.RunTLS()
}
func (s *Server) RunAutoTLS() {
s.App.RunAutoTLS()
}
func (s *Server) EnableRPC(address string) error {
// Register types for gob encoding
gob.Register(map[string]interface{}{})
s.rpcServer = rpc.NewServer()
busRPC := &BusRPC{server: s}
err := s.rpcServer.RegisterName("BusRPC", busRPC)
if err != nil {
return err
}
s.rpcServer.HandleHTTP(rpc.DefaultRPCPath, rpc.DefaultDebugPath)
listener, err := net.Listen("tcp", address)
if err != nil {
return err
}
go http.Serve(listener, nil)
return nil
}
type BusRPC struct {
server *Server
}
func (b *BusRPC) Ping(req *RPCRequest, resp *RPCResponse) error {
if _, ok := b.server.idConnRPC.Get(req.From); !ok {
rpcConn := &RPCConn{
Id: req.From,
msgChan: make(chan map[string]any, b.server.rpcMaxQueueSize),
}
b.server.idConnRPC.Set(req.From, rpcConn)
}
return nil
}
func (b *BusRPC) Subscribe(req *RPCRequest, resp *RPCResponse) error {
rpcConn, ok := b.server.idConnRPC.Get(req.From)
if !ok {
return fmt.Errorf("client not registered")
}
sub := Subscriber{
bus: b.server.Bus,
Id: req.From,
Topic: req.Topic,
Ch: rpcConn.msgChan,
}
if subs, found := b.server.Bus.topicSubscribers.Get(req.Topic); found {
subs = append(subs, sub)
b.server.Bus.topicSubscribers.Set(req.Topic, subs)
} else {
b.server.Bus.topicSubscribers.Set(req.Topic, []Subscriber{sub})
}
return nil
}
func (b *BusRPC) Unsubscribe(req *RPCRequest, resp *RPCResponse) error {
if subs, ok := b.server.Bus.topicSubscribers.Get(req.Topic); ok {
for i := range subs {
if subs[i].Id == req.From {
subs = append(subs[:i], subs[i+1:]...)
b.server.Bus.topicSubscribers.Set(req.Topic, subs)
break
}
}
}
return nil
}
func (b *BusRPC) Publish(req *RPCRequest, resp *RPCResponse) error {
req.Data["from"] = req.From
msg := map[string]any{
"from": req.From,
"topic": req.Topic,
}
// Copy all other fields
for k, v := range req.Data {
if k != "from" && k != "topic" {
msg[k] = v
}
}
b.server.Bus.Publish(req.Topic, msg)
return nil
}
func (b *BusRPC) PublishToID(req *RPCRequest, resp *RPCResponse) error {
req.Data["from"] = req.From
msg := map[string]any{
"to_id": req.Id,
"from": req.From,
}
for k, v := range req.Data {
if k != "to_id" && k != "from" {
msg[k] = v
}
}
if eventID, ok := req.Data["event_id"]; ok {
msg["event_id"] = eventID
}
if req.Id == b.server.ID {
if b.server.onId != nil {
b.server.onId(msg)
if eventID, ok := msg["event_id"]; ok {
b.server.Bus.Publish(eventID.(string), map[string]any{
"ok": "done",
"from": b.server.ID,
})
}
return nil
}
}
if rpcConn, ok := b.server.idConnRPC.Get(req.Id); ok {
select {
case rpcConn.msgChan <- msg:
default:
<-rpcConn.msgChan
rpcConn.msgChan <- msg
}
return nil
}
b.server.Bus.PublishToID(req.Id, msg)
return nil
}
func (b *BusRPC) RemoveTopic(req *RPCRequest, resp *RPCResponse) error {
req.Data["from"] = req.From
b.server.Bus.RemoveTopic(req.Topic)
return nil
}
func (b *BusRPC) Poll(req *RPCRequest, resp *RPCResponse) error {
rpcConn, ok := b.server.idConnRPC.Get(req.From)
if !ok {
return fmt.Errorf("client not registered")
}
select {
case msg := <-rpcConn.msgChan:
resp.Data = msg
return nil
default:
resp.Data = nil
return nil
}
}
func (s *Server) SetRPCMaxQueueSize(size int) {
s.rpcMaxQueueSize = size
}