-
Notifications
You must be signed in to change notification settings - Fork 1
/
frequency_lock.go
87 lines (80 loc) · 2.01 KB
/
frequency_lock.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
/*
* 纸喵软件
* Copyright (c) 2017~2020 http://zhimiao.org All rights reserved.
* Author: 倒霉狐狸 <mail@xiaoliu.org>
* Date: 2020/3/3 下午4:26
*/
package gutils
import (
"sync"
"time"
)
type lockItem struct {
Key string
LifeSpan time.Duration // 生命周期
CreateOn time.Time // 创建时间
}
type lockTable struct {
sync.RWMutex
CleanerDuraction time.Duration // 触发定时清理器的时间
Cleaner *time.Timer // 定时清理器
Items map[string]lockItem // 子集
}
// NewLockTable 新建
func NewLockTable() *lockTable {
return &lockTable{
Items: make(map[string]lockItem),
}
}
// IsLock 是否锁
func (l *lockTable) IsLock(key string, lockTime time.Duration) bool {
l.Lock()
if item, ok := l.Items[key]; ok {
l.Unlock()
if time.Since(item.CreateOn) > item.LifeSpan {
l.cleanerCheck()
return false
}
return true
}
l.Items[key] = lockItem{
Key: key,
LifeSpan: lockTime,
CreateOn: time.Now(),
}
cleannerDuraction := l.CleanerDuraction
l.Unlock()
if cleannerDuraction == 0 {
l.cleanerCheck()
}
return false
}
func (l *lockTable) cleanerCheck() {
l.Lock()
defer l.Unlock()
if l.Cleaner != nil {
l.Cleaner.Stop()
}
// 遍历当前限制的key, 遇到过期的将其删掉
// 其余的则从中找到最近一个将要过期的key并且将它还有多少时间过期作为下一次清理任务的定时时间
now := time.Now()
smallestDuracton := 0 * time.Second
for key, item := range l.Items {
lifeSpan := item.LifeSpan
createOn := item.CreateOn
if now.Sub(createOn) >= lifeSpan {
delete(l.Items, key)
} else {
if smallestDuracton == 0 || lifeSpan-now.Sub(createOn) < smallestDuracton {
smallestDuracton = lifeSpan - now.Sub(createOn)
}
}
}
l.CleanerDuraction = smallestDuracton
// 将最近一个将要过期的key距离现在的时间作为启动清理任务的定时时间
if l.CleanerDuraction > 0 {
l.Cleaner = time.AfterFunc(l.CleanerDuraction, func() {
go l.cleanerCheck()
})
}
}