This repository has been archived by the owner on Jun 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathcache.go
244 lines (204 loc) · 5.1 KB
/
cache.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
package main
import (
"fmt"
"time"
"github.com/tideland/golib/redis"
)
var (
rds *redis.Database
rdsCircularBuffer string
rdsGetIPCache string
rdsSetIPCache string
)
// how many log lines to buffer for the scrollback
const CHATLOGLINES = 150
func redisGetConn() *redis.Connection {
again:
conn, err := rds.Connection()
if err != nil {
D("Error getting a redis connection", err)
if conn != nil {
conn.Return()
}
time.Sleep(500 * time.Millisecond)
goto again
}
return conn
}
func initRedis(addr string, db int64, pw string) {
var err error
rds, err = redis.Open(
redis.TcpConnection(addr, 1*time.Second),
redis.Index(int(db), pw),
redis.PoolSize(50),
)
if err != nil {
F("Error making the redis pool", err)
}
conn := redisGetConn()
defer conn.Return()
rdsCircularBuffer, err = conn.DoString("SCRIPT", "LOAD", `
local key = KEYS[1]
local maxlength = tonumber(ARGV[1])
local payload = ARGV[2]
if not key then
return {err = "INVALID KEY"}
end
if not payload then
return {err = "INVALID PAYLOAD"}
end
if not maxlength then
return {err = "INVALID MAXLENGTH"}
end
-- push the payload onto the end
redis.call("RPUSH", key, payload)
local delcount = 0
-- get rid of excess lines from the front
local numlines = redis.call("LLEN", key)
for _ = numlines - 1, maxlength, -1 do
redis.call("LPOP", key)
delcount = delcount + 1
end
return delcount
`)
if err != nil {
F("Circular buffer script loading error", err)
}
rdsGetIPCache, err = conn.DoString("SCRIPT", "LOAD", `
local key = KEYS[1]
return redis.call("ZRANGEBYSCORE", key, 1, 3)
`)
if err != nil {
F("Get IP Cache script loading error", err)
}
rdsSetIPCache, err = conn.DoString("SCRIPT", "LOAD", `
local key, value, maxlength = KEYS[1], ARGV[1], 3
local count = redis.call("ZCOUNT", key, 1, maxlength)
local existingscore = redis.call("ZSCORE", key, value)
if existingscore then
-- renumber all the elements and make this one the last
local elements = redis.call("ZRANGEBYSCORE", key, 1, maxlength)
local i = 1
for _, v in ipairs(elements) do
if v == value then
redis.call("ZADD", key, count, v)
else
redis.call("ZADD", key, i, v)
i = i + 1
end
end
return
end
if count == maxlength then
-- delete the first element, modify the other elements score down
-- and add the new one to the end
redis.call("ZREMRANGEBYSCORE", key, 1, 1)
local elements = redis.call("ZRANGEBYSCORE", key, 2, maxlength)
local i = 1
for _, v in ipairs(elements) do
redis.call("ZADD", key, i, v)
i = i + 1
end
return redis.call("ZADD", key, count, value)
else
-- otherwise just insert it with the next score
return redis.call("ZADD", key, count + 1, value)
end
`)
if err != nil {
F("Set IP Cache script loading error", err)
}
}
func cacheIPForUser(userid Userid, ip string) {
if ip == "127.0.0.1" {
return
}
conn := redisGetConn()
defer conn.Return()
_, err := conn.Do("EVALSHA", rdsSetIPCache, 1, fmt.Sprintf("CHAT:userips-%d", userid), ip)
if err != nil {
D("cacheIPForUser redis error", err)
}
}
func getIPCacheForUser(userid Userid) []string {
conn := redisGetConn()
defer conn.Return()
ips, err := conn.DoStrings("EVALSHA", rdsGetIPCache, 1, fmt.Sprintf("CHAT:userips-%d", userid))
if err != nil {
D("getIPCacheForUser redis error", err)
}
return ips
}
func isSubErr(sub *redis.Subscription, err error) bool {
if err != nil {
D("Getting a subscription failed with error", err)
if sub != nil {
sub.Close()
}
time.Sleep(500 * time.Millisecond)
return true
}
return false
}
func setupRedisSubscription(channel string, redisdb int64, cb func(*redis.PublishedValue)) {
again:
sub, err := rds.Subscription()
if isSubErr(sub, err) {
goto again
}
err = sub.Subscribe(fmt.Sprintf("%s-%d", channel, redisdb))
if isSubErr(sub, err) {
goto again
}
for {
result, err := sub.Pop()
if isSubErr(sub, err) {
goto again
}
if result.Value.IsNil() {
continue
}
cb(result)
}
}
func redisGetBytes(key string) ([]byte, error) {
conn := redisGetConn()
defer conn.Return()
result, err := conn.Do("GET", key)
if err != nil {
return []byte{}, err
}
value, err := result.ValueAt(0)
if err != nil {
return []byte{}, err
}
return value.Bytes(), err
}
func cacheChatEvent(msg *message) {
conn := redisGetConn()
defer conn.Return()
data, err := Pack(msg.event, msg.data.([]byte))
if err != nil {
D("cacheChatEvent pack error", err)
return
}
_, err = conn.Do(
"EVALSHA",
rdsCircularBuffer,
1,
"CHAT:chatlog",
CHATLOGLINES,
data,
)
if err != nil {
D("cacheChatEvent redis error", err)
}
}
func cacheConnectedUsers(marshallednames []byte) {
conn := redisGetConn()
defer conn.Return()
_, err := conn.DoOK("SET", "CHAT:connectedUsers", marshallednames)
if err != nil {
D("Error caching connected users.", err)
}
}