-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_flow_count.go
102 lines (91 loc) · 2.56 KB
/
redis_flow_count.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
package public
import (
"fmt"
"sync/atomic"
"time"
"github.com/e421083458/go_gateway/golang_common/lib"
"github.com/garyburd/redigo/redis"
)
type RedisFlowCountService struct {
AppID string
Interval time.Duration
QPS int64
Unix int64
TickerCount int64
TotalCount int64
}
func NewRedisFlowCountService(appID string, interval time.Duration) *RedisFlowCountService {
reqCounter := &RedisFlowCountService{
AppID: appID,
Interval: interval,
QPS: 0,
Unix: 0,
}
go func() {
defer func() {
if err := recover(); err != nil {
fmt.Println(err)
}
}()
ticker := time.NewTicker(interval)
for {
<-ticker.C
tickerCount := atomic.LoadInt64(&reqCounter.TickerCount) //获取数据
atomic.StoreInt64(&reqCounter.TickerCount, 0) //重置数据
currentTime := time.Now()
dayKey := reqCounter.GetDayKey(currentTime)
hourKey := reqCounter.GetHourKey(currentTime)
if err := RedisConfPipline(func(c redis.Conn) {
c.Send("INCRBY", dayKey, tickerCount)
c.Send("EXPIRE", dayKey, 86400*2)
c.Send("INCRBY", hourKey, tickerCount)
c.Send("EXPIRE", hourKey, 86400*2)
}); err != nil {
fmt.Println("RedisConfPipline err", err)
continue
}
totalCount, err := reqCounter.GetDayData(currentTime)
if err != nil {
fmt.Println("reqCounter.GetDayData err", err)
continue
}
nowUnix := time.Now().Unix()
if reqCounter.Unix == 0 {
reqCounter.Unix = time.Now().Unix()
continue
}
tickerCount = totalCount - reqCounter.TotalCount
if nowUnix > reqCounter.Unix {
reqCounter.TotalCount = totalCount
reqCounter.QPS = tickerCount / (nowUnix - reqCounter.Unix)
reqCounter.Unix = time.Now().Unix()
}
}
}()
return reqCounter
}
func (o *RedisFlowCountService) GetDayKey(t time.Time) string {
dayStr := t.In(lib.TimeLocation).Format("20060102")
return fmt.Sprintf("%s_%s_%s", RedisFlowDayKey, dayStr, o.AppID)
}
func (o *RedisFlowCountService) GetHourKey(t time.Time) string {
hourStr := t.In(lib.TimeLocation).Format("2006010215")
return fmt.Sprintf("%s_%s_%s", RedisFlowHourKey, hourStr, o.AppID)
}
func (o *RedisFlowCountService) GetHourData(t time.Time) (int64, error) {
return redis.Int64(RedisConfDo("GET", o.GetHourKey(t)))
}
func (o *RedisFlowCountService) GetDayData(t time.Time) (int64, error) {
return redis.Int64(RedisConfDo("GET", o.GetDayKey(t)))
}
//原子增加
func (o *RedisFlowCountService) Increase() {
go func() {
defer func() {
if err := recover(); err != nil {
fmt.Println(err)
}
}()
atomic.AddInt64(&o.TickerCount, 1)
}()
}