This repository has been archived by the owner on Dec 1, 2019. It is now read-only.
/
scan.go
100 lines (94 loc) · 1.87 KB
/
scan.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
package redis
import (
"context"
"strings"
"time"
"github.com/gomodule/redigo/redis"
)
type node struct {
child map[string]*node
scan func(string)
}
// Handles a Redis prefix. The prefix must either be empty or end in a colon.
func (s *RedisService) ScanPrefix(prefix string, handler func(string)) {
cur := &s.root
for {
ind := strings.Index(prefix, ":")
if ind == -1 {
break
}
name := prefix[:ind]
prefix = prefix[ind+1:]
if cur.scan != nil {
panic("Scanner: conflicting prefix")
}
if cur.child == nil {
cur.child = make(map[string]*node)
}
nex := &node{}
cur.child[name] = nex
cur = nex
}
if prefix != "" {
panic("ScanPrefix: invalid prefix")
}
if cur.child != nil || cur.scan != nil {
panic("ScanPrefix: conflicting prefix")
}
cur.scan = handler
}
func (r *RedisService) RunScanner(ctx context.Context) error {
if r.Ratio < 0 || r.Ratio > 1 {
panic("redis: invalid ratio")
}
r.cursor = 0
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
startTime := time.Now()
if err := r.loop(ctx); err != nil {
r.ErrorHandler(err)
}
dur := time.Now().Sub(startTime)
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(time.Duration(float64(dur) / r.Ratio)):
}
}
}
func (r *RedisService) loop(ctx context.Context) error {
res, err := redis.Values(r.DoContext(ctx, "SCAN", r.cursor, "COUNT", r.BatchSize))
if err != nil {
return err
}
r.cursor, err = redis.Int64(res[0], nil)
if err != nil {
return err
}
keys, err := redis.Strings(res[1], nil)
if err != nil {
return err
}
for _, key := range keys {
cur := &r.root
for cur.child != nil {
ind := strings.Index(key, ":")
name := key[:ind]
if nex, ok := cur.child[name]; ok {
cur = nex
key = key[ind+1:]
} else {
cur = nil
break
}
}
if cur != nil && cur.scan != nil {
cur.scan(key)
}
}
return nil
}