forked from c4pt0r/pcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
240 lines (217 loc) · 5.06 KB
/
handler.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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
redis "gopkg.in/redis.v6"
)
func errResp(w io.Writer, code int, msg string, oid, uid uint64) {
payload, _ := json.Marshal(map[string]interface{}{
"error_code": code,
"error_message": msg,
"oid": oid,
"uid": uid,
})
w.Write(payload)
}
type LikeHandler struct {
localStore Store
remoteStores Store
redisStore *redis.Client
}
func (hdlr LikeHandler) isFriend(uid1, uid2 uint64) bool {
key := fmt.Sprintf("%c%d/%d", Prefix_Rel, uid1, uid2)
if hdlr.localStore.Exists([]byte(key)) {
return true
}
return false
}
func (hdlr LikeHandler) doList(w http.ResponseWriter, oid, uid uint64, cursor string, pageSize, needFriend int) error {
var start, prefix []byte
prefix = []byte(fmt.Sprintf("%c%d/", Prefix_Like, oid))
if len(cursor) > 0 {
start = next([]byte(cursor))
} else {
start = prefix
}
// default batch size
var res []uint64
var lastKey []byte
L:
for len(res) < pageSize {
kvs := hdlr.localStore.Scan(start, 20, func(k []byte) bool {
return !bytes.HasPrefix(k, prefix)
})
// no such object
if len(kvs) == 0 {
break L
}
// get uid
for _, kv := range kvs {
s := strings.Split(string(kv.K), "/")[1]
r, _ := strconv.ParseUint(s, 10, 64)
if needFriend == 0 || (needFriend == 1 && hdlr.isFriend(uid, r)) {
res = append(res, r)
}
lastKey = kv.K
if len(res) == pageSize {
break L
}
}
// update prefix
start = next(lastKey)
}
var list []map[uint64]string
for _, uid := range res {
list = append(list, map[uint64]string{
uid: nicknameMap[uid],
})
}
payload, _ := json.Marshal(map[string]interface{}{
"oid": oid,
"like_list": list,
"cursor": string(lastKey),
})
w.Write(payload)
return nil
}
func genLikeCountKey(oid uint64) string {
return fmt.Sprintf("%c%d", Prefix_ObjectLikeCnt, oid)
}
func genRecentLikeKey(oid uint64) string {
return fmt.Sprintf("%c%d", Prefix_ObjectRecentLike, oid)
}
func (hdlr LikeHandler) doLike(w http.ResponseWriter, oid, uid uint64) error {
key := fmt.Sprintf("%c%d/%d", Prefix_Like, oid, uid)
// check if already liked
if hdlr.localStore.Exists([]byte(key)) {
return errors.New("already liked")
}
// update like count
ck := genLikeCountKey(oid)
hdlr.redisStore.Incr(ck)
// update recent like list
rk := genRecentLikeKey(oid)
hdlr.redisStore.LPush(rk, uid)
// check if need to update redis recent list
cnt, err := hdlr.redisStore.LLen(rk).Result()
if err != nil {
return err
}
if cnt > 20 {
hdlr.redisStore.RPop(rk)
}
// load recent 20 users
uidList, err := hdlr.redisStore.LRange(rk, 0, 20).Result()
if err != nil {
return err
}
// get nickname info
var recentUserList []map[string]string
for _, uid := range uidList {
id, _ := strconv.ParseUint(uid, 10, 64)
recentUserList = append(recentUserList, map[string]string{
uid: nicknameMap[id],
})
}
// async persist to disk
go func() {
hdlr.localStore.Put(&KV{K: []byte(key)})
hdlr.remoteStores.Put(&KV{K: []byte(key)})
}()
// write response
payload, _ := json.Marshal(map[string]interface{}{
"oid": oid,
"uid": uid,
"like_list": recentUserList,
})
w.Write(payload)
return nil
}
func (hdlr LikeHandler) doIsLike(w http.ResponseWriter, oid, uid uint64) error {
key := []byte(fmt.Sprintf("%c%d/%d", Prefix_Like, oid, uid))
m := map[string]interface{}{
"oid": oid,
"uid": uid,
}
if hdlr.localStore.Exists(key) {
m["is_like"] = 1
} else {
m["is_like"] = 0
}
payload, _ := json.Marshal(m)
w.Write(payload)
return nil
}
func (hdlr LikeHandler) doCount(w http.ResponseWriter, oid uint64) error {
ck := genLikeCountKey(oid)
cnt, err := hdlr.redisStore.Get(ck).Int64()
if err != nil {
return err
}
// write payload
payload, _ := json.Marshal(map[string]interface{}{
"oid": oid,
"count": cnt,
})
w.Write(payload)
return nil
}
func (hdlr LikeHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
m, err := url.ParseQuery(req.URL.RawQuery)
if err != nil {
errResp(w, 500, "invlid url", 0, 0)
return
}
// get action
action := m.Get("action")
// get oid
oid, err := strconv.ParseUint(m.Get("oid"), 10, 64)
if err != nil {
errResp(w, 500, "invlid oid", 0, 0)
return
}
// get uid, default 0
uid, _ := strconv.ParseUint(m.Get("uid"), 10, 64)
switch action {
case "like":
err := hdlr.doLike(w, oid, uid)
if err != nil {
errResp(w, 602, err.Error(), oid, uid)
return
}
case "is_like":
err := hdlr.doIsLike(w, oid, uid)
if err != nil {
errResp(w, 702, err.Error(), oid, uid)
return
}
case "count":
err := hdlr.doCount(w, oid)
if err != nil {
errResp(w, 802, err.Error(), oid, uid)
return
}
case "list":
cursor := m.Get("cursor")
isFriend, _ := strconv.Atoi(m.Get("is_friend"))
pageSize, _ := strconv.Atoi(m.Get("page_size"))
if pageSize == 0 {
pageSize = 10
}
err := hdlr.doList(w, oid, uid, cursor, pageSize, isFriend)
if err != nil {
errResp(w, 802, err.Error(), oid, uid)
return
}
default:
errResp(w, 502, "no such action", oid, uid)
return
}
}