Debian Codegraph @

_examples/custom_engine_tarantool/tntengine/presence.go

raw
1 package tntengine
2
3 import (
4 "errors"
5 "fmt"
6 "time"
7
8 "github.com/FZambia/tarantool"
9
10 "github.com/centrifugal/centrifuge"
11 "github.com/vmihailenco/msgpack/v5"
12 )
13
14 // DefaultPresenceTTL is a default value for presence TTL in Tarantool.
15 const DefaultPresenceTTL = 60 * time.Second
16
17 // PresenceManagerConfig is a config for Tarantool-based PresenceManager.
18 type PresenceManagerConfig struct {
19 // PresenceTTL is an interval how long to consider presence info
20 // valid after receiving presence update. This allows to automatically
21 // clean up unnecessary presence entries after TTL passed.
22 PresenceTTL time.Duration
23
24 // Shards is a list of Tarantool instances to shard data by channel.
25 Shards []*Shard
26 }
27
28 // NewPresenceManager initializes Tarantool-based centrifuge.PresenceManager.
29 func NewPresenceManager(n *centrifuge.Node, config PresenceManagerConfig) (*PresenceManager, error) {
30 if len(config.Shards) == 0 {
31 return nil, errors.New("no Tarantool shards provided in configuration")
32 }
33 if len(config.Shards) > 1 {
34 n.Log(centrifuge.NewLogEntry(centrifuge.LogLevelInfo, fmt.Sprintf("Tarantool sharding enabled: %d shards", len(config.Shards))))
35 }
36 e := &PresenceManager{
37 node: n,
38 shards: config.Shards,
39 config: config,
40 sharding: len(config.Shards) > 1,
41 }
42 return e, nil
43 }
44
45 var _ centrifuge.PresenceManager = (*PresenceManager)(nil)
46
47 // PresenceManager uses Tarantool to implement centrifuge.PresenceManager functionality.
48 type PresenceManager struct {
49 node *centrifuge.Node
50 sharding bool
51 config PresenceManagerConfig
52 shards []*Shard
53 }
54
55 type presenceRequest struct {
56 Channel string
57 }
58
59 func (m PresenceManager) Presence(ch string) (map[string]*centrifuge.ClientInfo, error) {
60 s := consistentShard(ch, m.shards)
61 res, err := s.Exec(tarantool.Call("centrifuge.presence", presenceRequest{Channel: ch}))
62 if err != nil {
63 return nil, err
64 }
65 if len(res.Data) == 0 {
66 return nil, errors.New("malformed presence result")
67 }
68 presenceInterfaceSlice, ok := res.Data[0].([]interface{})
69 if !ok {
70 return nil, errors.New("malformed presence format: map expected")
71 }
72 presence := make(map[string]*centrifuge.ClientInfo, len(presenceInterfaceSlice))
73 for _, v := range presenceInterfaceSlice {
74 presenceRow, ok := v.([]interface{})
75 if !ok {
76 return nil, errors.New("malformed presence format: tuple expected")
77 }
78 clientID, ok := presenceRow[1].(string)
79 if !ok {
80 return nil, errors.New("malformed presence format: string client id expected")
81 }
82 userID, ok := presenceRow[2].(string)
83 if !ok {
84 return nil, errors.New("malformed presence format: string user id expected")
85 }
86 connInfo, ok := presenceRow[3].(string)
87 if !ok {
88 return nil, errors.New("malformed presence format: string conn info expected")
89 }
90 chanInfo, ok := presenceRow[4].(string)
91 if !ok {
92 return nil, errors.New("malformed presence format: string chan info expected")
93 }
94 ci := &centrifuge.ClientInfo{
95 ClientID: clientID,
96 UserID: userID,
97 }
98 if len(connInfo) > 0 {
99 ci.ConnInfo = []byte(connInfo)
100 }
101 if len(chanInfo) > 0 {
102 ci.ChanInfo = []byte(chanInfo)
103 }
104 presence[clientID] = ci
105 }
106 return presence, nil
107 }
108
109 type presenceStatsRequest struct {
110 Channel string
111 }
112
113 type presenceStatsResponse struct {
114 NumClients uint32
115 NumUsers uint32
116 }
117
118 func (m *presenceStatsResponse) DecodeMsgpack(d *msgpack.Decoder) error {
119 var err error
120 var l int
121 if l, err = d.DecodeArrayLen(); err != nil {
122 return err
123 }
124 if l != 2 {
125 return fmt.Errorf("array len doesn't match: %d", l)
126 }
127 if m.NumClients, err = d.DecodeUint32(); err != nil {
128 return err
129 }
130 if m.NumUsers, err = d.DecodeUint32(); err != nil {
131 return err
132 }
133 return nil
134 }
135
136 func (m PresenceManager) PresenceStats(ch string) (centrifuge.PresenceStats, error) {
137 s := consistentShard(ch, m.shards)
138 var resp presenceStatsResponse
139 err := s.ExecTyped(tarantool.Call("centrifuge.presence_stats", presenceStatsRequest{Channel: ch}), &resp)
140 if err != nil {
141 return centrifuge.PresenceStats{}, err
142 }
143 return centrifuge.PresenceStats{NumClients: int(resp.NumClients), NumUsers: int(resp.NumUsers)}, err
144 }
145
146 type addPresenceRequest struct {
147 Channel string
148 TTL int
149 ClientID string
150 UserID string
151 ConnInfo string
152 ChanInfo string
153 }
154
155 func (m PresenceManager) AddPresence(ch string, clientID string, info *centrifuge.ClientInfo) error {
156 s := consistentShard(ch, m.shards)
157 ttl := DefaultPresenceTTL
158 if m.config.PresenceTTL > 0 {
159 ttl = m.config.PresenceTTL
160 }
161 _, err := s.Exec(tarantool.Call("centrifuge.add_presence", addPresenceRequest{
162 Channel: ch,
163 TTL: int(ttl.Seconds()),
164 ClientID: clientID,
165 UserID: info.UserID,
166 ConnInfo: string(info.ConnInfo),
167 ChanInfo: string(info.ChanInfo),
168 }))
169 return err
170 }
171
172 type removePresenceRequest struct {
173 Channel string
174 ClientID string
175 }
176
177 func (m PresenceManager) RemovePresence(ch string, clientID string) error {
178 s := consistentShard(ch, m.shards)
179 _, err := s.Exec(tarantool.Call("centrifuge.remove_presence", removePresenceRequest{Channel: ch, ClientID: clientID}))
180 return err
181 }