forked from yudeguang/ratelimit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userStatistics.go
52 lines (48 loc) · 1.16 KB
/
userStatistics.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
package ratelimit
import (
"fmt"
"github.com/yudeguang/slice"
"sort"
)
/*
某用户剩余访问次数,例:
RemainingVisits("username")
*/
func (this *Rule) RemainingVisits(key interface{}) []int {
arr := make([]int, 0, len(this.rules))
for i := range this.rules {
arr = append(arr, this.rules[i].RemainingVisits(key))
}
return arr
}
/*
以IP作为用户名,此用户剩余访问次数,例:
RemainingVisitsByIP4("127.0.0.1")
*/
func (this *Rule) RemainingVisitsByIP4(ip string) []int {
ipInt64 := ip4StringToInt64(ip)
if ipInt64 == 0 {
return []int{}
}
return this.RemainingVisits(ipInt64)
}
//获得当前所有的在线用户,注意所有用int64存储的用户会被默认认为是IP地址,会被自动转换为IP的字符串形式输出以方便查看
func (this *Rule) GetCurOnlineUsers() []string {
var users []string
for i := range this.rules {
f := func(k, v interface{}) bool {
var user string
switch k.(type) {
case int64:
user = int64ToIp4String(k.(int64))
default:
user = fmt.Sprint(k)
}
users = slice.InsertIgnoreString(users, user)
return true
}
this.rules[i].indexes.Range(f)
}
sort.Strings(users)
return users
}