This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
redis.go
118 lines (107 loc) · 2.84 KB
/
redis.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
package api
import (
"context"
"github.com/golang/protobuf/proto"
"github.com/gomodule/redigo/redis"
pb "github.com/iguagile/iguagile-room-proto/room"
)
const (
channelServer = "channel_servers"
channelRoom = "channel_rooms"
)
const (
registerServerMessage = iota
unregisterServerMessage
registerRoomMessage
unregisterRoomMessage
)
func (s *RoomAPIServer) subscribe(ctx context.Context, psc redis.PubSubConn) error {
for {
select {
case <-ctx.Done():
return nil
default:
}
switch v := psc.Receive().(type) {
case redis.Message:
if len(v.Data) <= 1 {
s.Logger.Printf("invalid message %v\n", v)
break
}
switch v.Channel {
case channelRoom:
room := &pb.Room{}
if err := proto.Unmarshal(v.Data[1:], room); err != nil {
s.Logger.Println(err)
break
}
switch v.Data[0] {
case registerRoomMessage:
s.registerRoom(room)
case unregisterRoomMessage:
s.unregisterRoom(room)
default:
s.Logger.Printf("invalid message type %v\n", v)
}
case channelServer:
server := &pb.Server{}
if err := proto.Unmarshal(v.Data[1:], server); err != nil {
s.Logger.Println(err)
break
}
switch v.Data[0] {
case registerServerMessage:
s.registerServer(server)
case unregisterServerMessage:
s.unregisterServer(server)
default:
s.Logger.Printf("invalid message type %v\n", v)
}
default:
s.Logger.Printf("invalid channel%v\n", v)
}
case redis.Subscription:
s.Logger.Printf("Subscribe %v %v %v\n", v.Channel, v.Kind, v.Count)
case error:
return v
}
}
}
func (s *RoomAPIServer) registerRoom(room *pb.Room) {
s.roomManager.Store(&Room{
RoomID: int(room.RoomId),
RequirePassword: room.RequirePassword,
MaxUser: int(room.MaxUser),
ConnectedUser: int(room.ConnectedUser),
Server: Server{
Host: room.Server.Host,
Port: int(room.Server.Port),
ServerID: int(room.Server.ServerId),
},
ApplicationName: room.ApplicationName,
Version: room.Version,
})
if server := s.serverManager.LoadServer(int(room.Server.ServerId)); server != nil {
if r := s.roomManager.FindRoom(int(room.RoomId)); room != nil {
server.Load += int(room.ConnectedUser*room.ConnectedUser) - r.MaxUser*r.MaxUser
}
}
}
func (s *RoomAPIServer) unregisterRoom(room *pb.Room) {
if server := s.serverManager.LoadServer(int(room.Server.ServerId)); server != nil {
server.Load -= int(room.ConnectedUser * room.ConnectedUser)
}
s.roomManager.Delete(int(room.RoomId))
}
func (s *RoomAPIServer) registerServer(server *pb.Server) {
s.serverManager.Store(&Server{
Host: server.Host,
Port: int(server.Port),
ServerID: int(server.ServerId),
APIPort: int(server.ApiPort),
Token: server.Token,
})
}
func (s *RoomAPIServer) unregisterServer(server *pb.Server) {
s.serverManager.Delete(int(server.ServerId))
}