-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
94 lines (75 loc) · 1.82 KB
/
main.go
File metadata and controls
94 lines (75 loc) · 1.82 KB
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
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"github.com/garyburd/redigo/redis"
"io"
"os"
)
var (
SkipRDB bool
Replace bool
Path string
counter uint64
proxyPort int
proxyHost string
proxyPassword string
)
func getConn(addr string, auth string) (redis.Conn, error) {
if auth != "" {
return redis.Dial("tcp", addr, redis.DialPassword(auth))
}
return redis.Dial("tcp", addr)
}
func main() {
flag.StringVar(&Path, "path", "./bloom_filter.rdb", "rdb file path")
flag.BoolVar(&SkipRDB, "skip-rdb", false, "skip doing command")
flag.BoolVar(&Replace, "replace", true, "use restore command with replace")
flag.StringVar(&proxyHost, "proxy-host", "", "Proxy listening interface, default is on all interfaces")
flag.IntVar(&proxyPort, "proxy-port", 6380, "Proxy port for listening")
flag.StringVar(&proxyPassword, "proxy-password", "", "Proxy password")
flag.Parse()
ch1 := make(chan *RedisCommand, 10)
chs := []chan *RedisCommand{ch1}
var result string
if fileObj, err := os.Open(Path); err == nil {
defer fileObj.Close()
contents, _ := io.ReadAll(fileObj)
result = string(contents)
}
go func() {
err := ParseRDB(bufio.NewReader(bytes.NewBufferString(result)), chs[0], &counter)
if err != nil {
// todo handle error
panic(err)
}
close(ch1)
}()
conn, err := getConn(fmt.Sprintf("%s:%d", proxyHost, proxyPort), proxyPassword)
if err != nil {
panic(err)
}
defer func(conn redis.Conn) {
err := conn.Close()
if err != nil {
panic(err)
}
}(conn)
for cmd := range ch1 {
args := make([]interface{}, len(cmd.Command[1:]))
for i, arg := range cmd.Command[1:] {
args[i] = arg
}
// todo pipeline send
err := conn.Send(cmd.Command[0], args...)
if err != nil {
panic(err)
}
err = conn.Flush()
if err != nil {
panic(err)
}
}
}