-
Notifications
You must be signed in to change notification settings - Fork 3
/
managerBind.go
95 lines (83 loc) · 2.07 KB
/
managerBind.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
package pushKit
import (
"fmt"
"github.com/richelieu-yang/chimera/v2/src/core/setKit"
"github.com/richelieu-yang/chimera/v2/src/core/strKit"
)
func BindId(channel Channel, id string) {
if strKit.IsEmpty(id) {
return
}
/* 写锁 */
idMap.LockFunc(func() {
if old, ok := bsidMap.Map[id]; ok {
_ = old.Close(fmt.Sprintf("id(%s) is replaced by new channel", id))
}
idMap.Map[id] = channel
// 此处无需更新 channel 的信息(id)
})
}
func BindBsid(channel Channel, bsid string) {
// 防止: 传参有误 || 重复绑定
if strKit.IsEmpty(bsid) || channel.GetBsid() == bsid {
return
}
// 先解绑 bsid(有的话)
UnbindBsid(channel)
/* 写锁 */
bsidMap.LockFunc(func() {
// 处理特殊情况: 已经有别的 channel 绑定了此 bsid
old := bsidMap.Map[bsid]
if old != nil {
_ = old.Close(fmt.Sprintf("bsid(%s) is replaced by new channel", bsid))
delete(bsidMap.Map, bsid)
}
bsidMap.Map[bsid] = channel
// 更新 channel 的信息
channel.SetBsid(bsid)
})
}
func BindUser(channel Channel, user string) {
// 防止: 传参有误 || 重复绑定
if strKit.IsEmpty(user) || channel.GetUser() == user {
return
}
// 先解绑 user(有的话)
UnbindUser(channel)
/* map写锁 */
userMap.LockFunc(func() {
userSet := userMap.Map[user]
if userSet == nil {
userSet = setKit.NewSetWithLock[Channel]()
userMap.Map[user] = userSet
}
/* set写锁 */
userSet.LockFunc(func() {
_ = userSet.Set.Add(channel)
// 更新 channel 的信息
channel.SetUser(user)
})
})
}
func BindGroup(channel Channel, group string) {
// 防止: 传参有误 || 重复绑定
if strKit.IsEmpty(group) || channel.GetGroup() == group {
return
}
// 先解绑 group(有的话)
UnbindGroup(channel)
/* map写锁 */
groupMap.LockFunc(func() {
groupSet := groupMap.Map[group]
if groupSet == nil {
groupSet = setKit.NewSetWithLock[Channel]()
groupMap.Map[group] = groupSet
}
/* set写锁 */
groupSet.LockFunc(func() {
_ = groupSet.Set.Add(channel)
// 更新 channel 的信息
channel.SetGroup(group)
})
})
}