-
Notifications
You must be signed in to change notification settings - Fork 0
/
flow_count_handler.go
41 lines (34 loc) · 998 Bytes
/
flow_count_handler.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
package public
import (
"sync"
"time"
)
var FlowCounterHandler *FlowCounter
type FlowCounter struct {
RedisFlowCountMap map[string]*RedisFlowCountService
RedisFlowCountSlice []*RedisFlowCountService
Locker sync.RWMutex
}
func NewFlowCounter() *FlowCounter {
return &FlowCounter{
RedisFlowCountMap: map[string]*RedisFlowCountService{},
RedisFlowCountSlice: []*RedisFlowCountService{},
Locker: sync.RWMutex{},
}
}
func init() {
FlowCounterHandler = NewFlowCounter()
}
func (counter *FlowCounter) GetCounter(serverName string) (*RedisFlowCountService, error) {
for _, item := range counter.RedisFlowCountSlice {
if item.AppID == serverName {
return item, nil
}
}
newCounter := NewRedisFlowCountService(serverName, 1*time.Second)
counter.RedisFlowCountSlice = append(counter.RedisFlowCountSlice, newCounter)
counter.Locker.Lock()
defer counter.Locker.Unlock()
counter.RedisFlowCountMap[serverName] = newCounter
return newCounter, nil
}