-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathmanager.go
66 lines (59 loc) · 1.39 KB
/
manager.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
package filter
import (
"sync"
"time"
)
const (
// DefaultCheckInterval 敏感词检查频率(默认5秒检查一次)
DefaultCheckInterval = time.Second * 5
)
// NewDirtyManager 使用敏感词存储接口创建敏感词管理的实例
func NewDirtyManager(store DirtyStore, checkInterval ...time.Duration) *DirtyManager {
interval := DefaultCheckInterval
if len(checkInterval) > 0 {
interval = checkInterval[0]
}
manage := &DirtyManager{
store: store,
version: store.Version(),
filter: NewNodeChanFilter(store.Read()),
interval: interval,
}
if (interval != -1) {
go func() {
manage.checkVersion()
}()
}
return manage
}
// DirtyManager 提供敏感词的管理
type DirtyManager struct {
store DirtyStore
filter DirtyFilter
filterMux sync.RWMutex
version uint64
interval time.Duration
}
func (dm *DirtyManager) checkVersion() {
time.AfterFunc(dm.interval, func() {
storeVersion := dm.store.Version()
if dm.version < storeVersion {
dm.filterMux.Lock()
dm.filter = NewNodeChanFilter(dm.store.Read())
dm.filterMux.Unlock()
dm.version = storeVersion
}
dm.checkVersion()
})
}
// Store 获取敏感词存储接口
func (dm *DirtyManager) Store() DirtyStore {
return dm.store
}
// Filter 获取敏感词过滤接口
func (dm *DirtyManager) Filter() DirtyFilter {
dm.filterMux.RLock()
filter := dm.filter
dm.filterMux.RUnlock()
return filter
}