-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
267 lines (235 loc) · 6.48 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package deprecated
// import (
// "encoding/json"
// "math"
// "os"
// "strconv"
// "time"
// "github.com/brianbroderick/logit"
// "github.com/garyburd/redigo/redis"
// )
// var (
// pool *redis.Pool
// redisPassword string
// )
// func getLog(redisKey string) (*query, error) {
// var data json.RawMessage
// conn := pool.Get()
// defer conn.Close()
// reply, err := conn.Do("LPOP", redisKey)
// if err != nil {
// return nil, err
// }
// if reply != nil {
// data, err = redis.Bytes(reply, err)
// if err != nil {
// return nil, err
// }
// query, suppressed, err := newQuery(data, redisKey)
// if err != nil {
// return nil, err
// }
// if suppressed {
// return nil, nil
// }
// return query, nil
// }
// return nil, nil
// }
// func startRedisBatch(redisKey string, task string) {
// var (
// keyLog string
// lastLog int
// lastProcessed int64
// msgLog string
// nap int
// processedMessages int64
// sleepDuration time.Duration
// )
// for {
// ok, msgCount, queueLength, err := getMultiLog(redisKey, task)
// if err != nil {
// logit.Error("Error in getMultiLog: %e", err.Error())
// }
// if ok == false {
// sleepDuration = getDuration(nap)
// nap += int(sleepDuration)
// time.Sleep((time.Second * sleepDuration))
// // idle for 20 seconds, emit message
// if (nap < 60 && nap-lastLog >= 30) || (nap >= 60 && nap-lastLog >= 60) {
// logit.Info("%s %s %s", yellow(nap), white("seconds since received data from"), yellow(redisKey))
// lastLog = nap
// processedMessages = 0
// }
// } else {
// nap = 0
// lastLog = 0
// processedMessages += msgCount
// if (processedMessages < 1000000 && processedMessages-lastProcessed >= 10000) ||
// (processedMessages >= 1000000 && processedMessages-lastProcessed >= 100000) {
// keyLog = colorKey(redisKey, processedMessages)
// msgLog = colorKey(strconv.FormatInt(processedMessages, 10), processedMessages)
// logit.Info("%s: %s messages processed since last reset. Queue length: %s", keyLog, msgLog, red(queueLength))
// lastProcessed = processedMessages
// }
// }
// }
// }
// func colorKey(str string, counter int64) string {
// var colorString string
// if counter > 100000000 {
// colorString = blue(str)
// } else if counter > 20000000 {
// colorString = green(str)
// } else if counter > 2000000 {
// colorString = cyan(str)
// } else {
// colorString = magenta(str)
// }
// return colorString
// }
// // task == "query" for normal operations.
// // task == "stats" to handle a stats payload. See stats.go for more details
// func getMultiLog(redisKey string, task string) (bool, int64, int64, error) {
// conn := pool.Get()
// defer conn.Close()
// // get list length
// l, err := conn.Do("LLEN", redisKey)
// if err != nil {
// return true, 0, 0, err
// }
// llen, err := redis.Int64(l, err)
// if err != nil {
// return true, 0, 0, err
// }
// queueLength := llen
// // LPOP at most 100 at a time
// if llen > 100 {
// llen = 100
// }
// if llen > 0 {
// conn.Send("MULTI")
// var n int64
// for n = 0; n < llen; n++ {
// conn.Send("LPOP", redisKey)
// }
// reply, err := redis.Values(conn.Do("EXEC"))
// if err != nil {
// return true, 0, queueLength, err
// }
// for _, datum := range reply {
// if datum != nil {
// q, err := redis.Bytes(datum, err)
// if err != nil {
// return true, 0, queueLength, err
// }
// if task == "query" {
// query, suppressed, err := newQuery(q, redisKey)
// if err != nil {
// logit.Error("Error in newQuery: %e", err.Error())
// } else if suppressed {
// // no-op
// } else {
// addToQueries(roundToMinute(query.timestamp), query)
// query.handleQueryDetails()
// }
// } else {
// stats, err := newStats(q, redisKey)
// if err != nil {
// logit.Error("Error in newStats: %e", err.Error())
// } else {
// data, err := json.Marshal(stats.data)
// if err != nil {
// logit.Error("Error marshalling data: %e", err.Error())
// }
// sendToStatsBulker(data)
// }
// }
// }
// }
// return true, llen, queueLength, nil
// }
// return false, 0, queueLength, nil
// }
// // SetupRedis setup redis
// func SetupRedis() {
// if os.Getenv("PLATFORM_ENV") != "test" {
// logit.Info("Redis URL: %s\n", redisURL())
// }
// pool = newPool(redisURL())
// redisPassword = redisPW()
// }
// func redisURL() string {
// // If flag is set, use that.
// if redisPtr != "" {
// return redisPtr
// }
// // Next use an environment variable, if it's set
// if value, ok := os.LookupEnv("PLS_REDIS_URL"); ok {
// return value
// }
// // Lastly return default
// // return "127.0.0.1:6379"
// // Default to Docker version
// return "redis:6379"
// }
// func redisPW() string {
// // If flag is set, use that.
// if redisPwPtr != "" {
// return redisPwPtr
// }
// // Next use an environment variable, if it's set
// if value, ok := os.LookupEnv("PLS_REDIS_PASSWORD"); ok {
// return value
// }
// // Lastly return default
// return ""
// }
// func newPool(server string) *redis.Pool {
// return &redis.Pool{
// MaxIdle: 10,
// MaxActive: 0,
// IdleTimeout: 240 * time.Second,
// Wait: true,
// Dial: func() (redis.Conn, error) {
// c, err := redis.Dial("tcp", server)
// if err != nil {
// logit.Error("Error on connecting to Redis: %e", err.Error())
// return nil, err
// }
// if redisPassword != "" {
// if _, err = c.Do("AUTH", redisPassword); err != nil {
// logit.Error("Error on authenticating to Redis: %e", err.Error())
// return nil, err
// }
// }
// return c, err
// },
// TestOnBorrow: func(c redis.Conn, t time.Time) error {
// _, err := c.Do("PING")
// return err
// },
// }
// }
// func startRedisSingle(redisKey string) {
// for {
// query, err := getLog(redisKey)
// if err != nil {
// logit.Error("Error getting log from Redis: %e", err.Error())
// }
// if query != nil {
// addToQueries(currentMinute(), query)
// query.handleQueryDetails()
// } else {
// logit.Info("No new queries found. Waiting 5 seconds.")
// time.Sleep((time.Second * 5))
// }
// }
// }
// func getDuration(nap int) time.Duration {
// sleepDuration := time.Duration(math.Ceil((float64(nap) + 0.01) / 4.0))
// if sleepDuration > 60 {
// sleepDuration = 60
// }
// return sleepDuration
// }