/
memory_group_service.go
202 lines (163 loc) · 4.92 KB
/
memory_group_service.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package groups
import (
"context"
"sync"
"time"
"github.com/topfreegames/pitaya/config"
"github.com/topfreegames/pitaya/constants"
)
var (
memoryGroupsMu sync.RWMutex
memoryGroups map[string]*MemoryGroup
memoryOnce sync.Once
)
// MemoryGroupService base in server memory solution
type MemoryGroupService struct {
}
// MemoryGroup is the struct stored in each group key(which is the name of the group)
type MemoryGroup struct {
Uids []string
LastRefresh int64
TTL int64
}
// NewMemoryGroupService returns a new group instance
func NewMemoryGroupService(conf *config.Config) *MemoryGroupService {
memoryOnce.Do(func() {
memoryGroups = make(map[string]*MemoryGroup)
go groupTTLCleanup(conf)
})
return &MemoryGroupService{}
}
func groupTTLCleanup(conf *config.Config) {
for now := range time.Tick(conf.GetDuration("pitaya.groups.memory.tickduration")) {
memoryGroupsMu.Lock()
for groupName, mg := range memoryGroups {
if mg.TTL != 0 && now.UnixNano()-mg.LastRefresh > mg.TTL {
delete(memoryGroups, groupName)
}
}
memoryGroupsMu.Unlock()
}
}
// GroupCreate creates a group without TTL
func (c *MemoryGroupService) GroupCreate(ctx context.Context, groupName string) error {
memoryGroupsMu.Lock()
defer memoryGroupsMu.Unlock()
if _, ok := memoryGroups[groupName]; ok {
return constants.ErrGroupAlreadyExists
}
memoryGroups[groupName] = &MemoryGroup{}
return nil
}
// GroupCreateWithTTL creates a group with TTL, which the go routine will clean later
func (c *MemoryGroupService) GroupCreateWithTTL(ctx context.Context, groupName string, ttlTime time.Duration) error {
memoryGroupsMu.Lock()
defer memoryGroupsMu.Unlock()
if _, ok := memoryGroups[groupName]; ok {
return constants.ErrGroupAlreadyExists
}
memoryGroups[groupName] = &MemoryGroup{LastRefresh: time.Now().UnixNano(), TTL: ttlTime.Nanoseconds()}
return nil
}
// GroupMembers returns all member's UID in given group
func (c *MemoryGroupService) GroupMembers(ctx context.Context, groupName string) ([]string, error) {
memoryGroupsMu.Lock()
defer memoryGroupsMu.Unlock()
mg, ok := memoryGroups[groupName]
if !ok {
return nil, constants.ErrGroupNotFound
}
uids := make([]string, len(mg.Uids))
copy(uids, mg.Uids)
return uids, nil
}
// GroupContainsMember check whether an UID is contained in given group or not
func (c *MemoryGroupService) GroupContainsMember(ctx context.Context, groupName, uid string) (bool, error) {
memoryGroupsMu.Lock()
defer memoryGroupsMu.Unlock()
mg, ok := memoryGroups[groupName]
if !ok {
return false, constants.ErrGroupNotFound
}
_, contains := elementIndex(mg.Uids, uid)
return contains, nil
}
// GroupAddMember adds UID to group
func (c *MemoryGroupService) GroupAddMember(ctx context.Context, groupName, uid string) error {
memoryGroupsMu.Lock()
defer memoryGroupsMu.Unlock()
mg, ok := memoryGroups[groupName]
if !ok {
return constants.ErrGroupNotFound
}
_, contains := elementIndex(mg.Uids, uid)
if contains {
return constants.ErrMemberAlreadyExists
}
mg.Uids = append(mg.Uids, uid)
memoryGroups[groupName] = mg
return nil
}
// GroupRemoveMember removes specific UID from group
func (c *MemoryGroupService) GroupRemoveMember(ctx context.Context, groupName, uid string) error {
memoryGroupsMu.Lock()
defer memoryGroupsMu.Unlock()
mg, ok := memoryGroups[groupName]
if !ok {
return constants.ErrGroupNotFound
}
index, contains := elementIndex(mg.Uids, uid)
if contains {
mg.Uids[index] = mg.Uids[len(mg.Uids)-1]
mg.Uids = mg.Uids[:len(mg.Uids)-1]
memoryGroups[groupName] = mg
return nil
}
return constants.ErrMemberNotFound
}
// GroupRemoveAll clears all UIDs from group
func (c *MemoryGroupService) GroupRemoveAll(ctx context.Context, groupName string) error {
memoryGroupsMu.Lock()
defer memoryGroupsMu.Unlock()
mg, ok := memoryGroups[groupName]
if !ok {
return constants.ErrGroupNotFound
}
mg.Uids = []string{}
return nil
}
// GroupDelete deletes the whole group, including members and base group
func (c *MemoryGroupService) GroupDelete(ctx context.Context, groupName string) error {
memoryGroupsMu.Lock()
defer memoryGroupsMu.Unlock()
_, ok := memoryGroups[groupName]
if !ok {
return constants.ErrGroupNotFound
}
delete(memoryGroups, groupName)
return nil
}
// GroupCountMembers get current member amount in group
func (c *MemoryGroupService) GroupCountMembers(ctx context.Context, groupName string) (int, error) {
memoryGroupsMu.Lock()
defer memoryGroupsMu.Unlock()
mg, ok := memoryGroups[groupName]
if !ok {
return 0, constants.ErrGroupNotFound
}
return len(mg.Uids), nil
}
// GroupRenewTTL will renew lease TTL
func (c *MemoryGroupService) GroupRenewTTL(ctx context.Context, groupName string) error {
memoryGroupsMu.Lock()
defer memoryGroupsMu.Unlock()
mg, ok := memoryGroups[groupName]
if !ok {
return constants.ErrGroupNotFound
}
if mg.TTL != 0 {
mg.LastRefresh = time.Now().UnixNano()
return nil
}
return constants.ErrMemoryTTLNotFound
}