/
wshub.go
144 lines (116 loc) · 3.41 KB
/
wshub.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
package wshub
import (
"fmt"
"sync"
"github.com/csaldiasdev/distws/internal/repository"
"github.com/csaldiasdev/distws/internal/wshub/rpc/client"
"github.com/csaldiasdev/distws/internal/wshub/rpc/server"
"github.com/google/uuid"
"github.com/gorilla/websocket"
"github.com/rs/zerolog/log"
)
type Hub struct {
nodeId string
repository repository.Repository
mu sync.Mutex
rpcNodes map[string]*client.HubRpcClient
connections map[uuid.UUID]WsConnection
messageToConnectionChan chan messageToConnection
registerChan chan registerConnection
unRegisterChan chan uuid.UUID
}
type messageToConnection struct {
id uuid.UUID
byteMsg []byte
}
type registerConnection struct {
userId uuid.UUID
connectionId uuid.UUID
conn WsConnection
}
func NewHub(id string, ip string, rpcPort uint, repo repository.Repository) *Hub {
h := &Hub{
nodeId: id,
repository: repo,
rpcNodes: make(map[string]*client.HubRpcClient),
connections: make(map[uuid.UUID]WsConnection),
messageToConnectionChan: make(chan messageToConnection),
registerChan: make(chan registerConnection),
unRegisterChan: make(chan uuid.UUID),
}
h.run()
go server.ListenAndServeHubRpc(ip, rpcPort, func(userId string, message []byte) error {
refConnections, err := h.repository.GetByUserId(uuid.MustParse(userId))
if err != nil {
return err
}
for _, v := range refConnections {
if v.NodeId != h.nodeId {
continue
}
h.messageToConnectionChan <- messageToConnection{uuid.MustParse(v.Id), message}
}
return nil
})
return h
}
func (h *Hub) AddHubNode(nodeId string, ip string, rpcPort uint) error {
h.mu.Lock()
defer h.mu.Unlock()
rpcCli, err := client.NewHubRpcClient(fmt.Sprintf("%s:%d", ip, rpcPort))
if err != nil {
return err
}
h.rpcNodes[nodeId] = rpcCli
return nil
}
func (h *Hub) AddWebSocketConnection(wsConn *websocket.Conn, userId uuid.UUID) {
conn, id := NewConnection(wsConn, h.unRegisterChan)
h.registerChan <- registerConnection{conn: conn, connectionId: id, userId: userId}
conn.SendMessage([]byte(fmt.Sprintf("CONNECTED FROM NODE: %s", h.nodeId)))
}
func (h *Hub) MessageToUser(userId uuid.UUID, byteMsg []byte) error {
refConnections, err := h.repository.GetByUserId(userId)
if err != nil {
return err
}
for _, v := range refConnections {
if v.NodeId == h.nodeId {
h.messageToConnectionChan <- messageToConnection{uuid.MustParse(v.Id), byteMsg}
} else {
rpcCli, ok := h.rpcNodes[v.NodeId]
if ok {
rpcCli.MessageToUser(userId.String(), byteMsg)
}
}
}
return nil
}
func (h *Hub) run() {
go func() {
for {
select {
case c := <-h.registerChan:
h.connections[c.connectionId] = c.conn
h.repository.Insert(c.connectionId, c.userId, uuid.MustParse(h.nodeId))
log.Info().
Str("userId", c.userId.String()).
Str("connectionId", c.connectionId.String()).
Str("nodeId", h.nodeId).
Msg("User registered")
case c := <-h.unRegisterChan:
delete(h.connections, c)
h.repository.DeleteConnection(c)
log.Info().
Str("connectionId", c.String()).
Str("nodeId", h.nodeId).
Msg("Connection unregistered")
case m := <-h.messageToConnectionChan:
wsc, found := h.connections[m.id]
if found {
wsc.SendMessage(m.byteMsg)
}
}
}
}()
}