/
redis.go
128 lines (109 loc) · 2.86 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
package setup
import (
"github.com/go-redis/redis"
conf "github.com/pp553933054/micro-go-book/ch13-seckill/pkg/config"
"github.com/pp553933054/micro-go-book/ch13-seckill/sk-app/service/srv_redis"
"github.com/unknwon/com"
"log"
"time"
)
//初始化Redis
func InitRedis() {
log.Printf("init redis %s", conf.Redis.Password)
client := redis.NewClient(&redis.Options{
Addr: conf.Redis.Host,
Password: conf.Redis.Password,
DB: conf.Redis.Db,
})
_, err := client.Ping().Result()
if err != nil {
log.Printf("Connect redis failed. Error : %v", err)
}
log.Printf("init redis success")
conf.Redis.RedisConn = client
loadBlackList(client)
initRedisProcess()
}
//加载黑名单列表
func loadBlackList(conn *redis.Client) {
conf.SecKill.IPBlackMap = make(map[string]bool, 10000)
conf.SecKill.IDBlackMap = make(map[int]bool, 10000)
//用户Id
idList, err := conn.HGetAll(conf.Redis.IdBlackListHash).Result()
if err != nil {
log.Printf("hget all failed. Error : %v", err)
return
}
for _, v := range idList {
id, err := com.StrTo(v).Int()
if err != nil {
log.Printf("invalid user id [%v]", id)
continue
}
conf.SecKill.IDBlackMap[id] = true
}
//用户Ip
ipList, err := conn.HGetAll(conf.Redis.IpBlackListHash).Result()
if err != nil {
log.Printf("hget all failed. Error : %v", err)
return
}
for _, v := range ipList {
conf.SecKill.IPBlackMap[v] = true
}
//go syncIpBlackList(conn)
//go syncIdBlackList(conn)
return
}
//同步用户ID黑名单
func syncIdBlackList(conn *redis.Client) {
for {
idArr, err := conn.BRPop(time.Minute, conf.Redis.IdBlackListQueue).Result()
if err != nil {
log.Printf("brpop id failed, err : %v", err)
continue
}
id, _ := com.StrTo(idArr[1]).Int()
conf.SecKill.RWBlackLock.Lock()
{
conf.SecKill.IDBlackMap[id] = true
}
conf.SecKill.RWBlackLock.Unlock()
}
}
//同步用户IP黑名单
func syncIpBlackList(conn *redis.Client) {
var ipList []string
lastTime := time.Now().Unix()
for {
ipArr, err := conn.BRPop(time.Minute, conf.Redis.IpBlackListQueue).Result()
if err != nil {
log.Printf("brpop ip failed, err : %v", err)
continue
}
ip := ipArr[1]
curTime := time.Now().Unix()
ipList = append(ipList, ip)
if len(ipList) > 100 || curTime-lastTime > 5 {
conf.SecKill.RWBlackLock.Lock()
{
for _, v := range ipList {
conf.SecKill.IPBlackMap[v] = true
}
}
conf.SecKill.RWBlackLock.Unlock()
lastTime = curTime
log.Printf("sync ip list from redis success, ip[%v]", ipList)
}
}
}
//初始化redis进程
func initRedisProcess() {
log.Printf("initRedisProcess %d %d", conf.SecKill.AppWriteToHandleGoroutineNum, conf.SecKill.AppReadFromHandleGoroutineNum)
for i := 0; i < conf.SecKill.AppWriteToHandleGoroutineNum; i++ {
go srv_redis.WriteHandle()
}
for i := 0; i < conf.SecKill.AppReadFromHandleGoroutineNum; i++ {
go srv_redis.ReadHandle()
}
}