-
Notifications
You must be signed in to change notification settings - Fork 1
/
clientstorage.go
155 lines (125 loc) · 3.67 KB
/
clientstorage.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
package main
import (
"math"
"sync"
)
type ClientStorage struct {
mu sync.RWMutex
m map[uint32]*Client
mseq []*Client
}
func NewClientStorage(initialSize int) (storage *ClientStorage) {
if initialSize <= 0 {
initialSize = 0
}
storage = &ClientStorage{
m: make(map[uint32]*Client, initialSize),
mseq: make([]*Client, 0, initialSize),
}
return
}
func (storage *ClientStorage) put(sessionId uint32, client *Client) {
storage.m[sessionId] = client
storage.mseq = append(storage.mseq, client)
return
}
func (storage *ClientStorage) Put(sessionId uint32, client *Client) {
storage.mu.Lock()
defer storage.mu.Unlock()
storage.put(sessionId, client)
return
}
func (storage *ClientStorage) get(sessionId uint32) (client *Client, ok bool) {
client, ok = storage.m[sessionId]
return
}
func (storage *ClientStorage) Has(sessionId uint32) (exists bool) {
storage.mu.RLock()
defer storage.mu.RUnlock()
_, exists = storage.get(sessionId)
return
}
func (storage *ClientStorage) Get(sessionId uint32) (client *Client, ok bool) {
storage.mu.RLock()
defer storage.mu.RUnlock()
client, ok = storage.get(sessionId)
return
}
func (storage *ClientStorage) delete(sessionId uint32) {
ptr, exists := storage.get(sessionId)
if !exists {
return
}
delete(storage.m, sessionId)
for i := 0; i < len(storage.mseq); i++ {
if storage.mseq[i] == ptr {
storage.mseq[i] = storage.mseq[len(storage.mseq)-1]
storage.mseq[len(storage.mseq)-1] = nil
storage.mseq = storage.mseq[:len(storage.mseq)-1]
break
}
}
return
}
func (storage *ClientStorage) Delete(sessionId uint32) {
storage.mu.Lock()
defer storage.mu.Unlock()
storage.delete(sessionId)
return
}
func (storage *ClientStorage) len() int {
return len(storage.mseq)
}
func (storage *ClientStorage) Len() int {
storage.mu.RLock()
defer storage.mu.RUnlock()
return storage.len()
}
func (storage *ClientStorage) snapshotMap(f func(sessionId uint32, client *Client) bool, estimatedSizeFactor float64) (snap map[uint32]*Client) {
if estimatedSizeFactor <= 0 || estimatedSizeFactor > 1 {
estimatedSizeFactor = 1
}
length := storage.len()
estimatedSize := int(math.Ceil(float64(length) * estimatedSizeFactor))
snap = make(map[uint32]*Client, estimatedSize)
for sId := 0; sId < length; sId++ {
client := storage.mseq[sId]
if !f(client.Session(), client) {
continue
}
snap[client.Session()] = client
}
return
}
func (storage *ClientStorage) SnapshotMapWithFilter(f func(sessionId uint32, client *Client) bool, estimatedSizeFactor float64) (snap map[uint32]*Client) {
storage.mu.RLock()
defer storage.mu.RUnlock()
return storage.snapshotMap(f, estimatedSizeFactor)
}
func (storage *ClientStorage) SnapshotMap() map[uint32]*Client {
return storage.SnapshotMapWithFilter(func(sId uint32, c *Client) bool { return true }, 1)
}
func (storage *ClientStorage) snapshot(f func(sessionId uint32, client *Client) bool, estimatedSizeFactor float64) (snap []*Client) {
if estimatedSizeFactor <= 0 || estimatedSizeFactor > 1 {
estimatedSizeFactor = 1
}
length := storage.len()
estimatedSize := int(math.Ceil(float64(length) * estimatedSizeFactor))
snap = make([]*Client, 0, estimatedSize)
for sId := 0; sId < length; sId++ {
client := storage.mseq[sId]
if !f(client.Session(), client) {
continue
}
snap = append(snap, client)
}
return
}
func (storage *ClientStorage) SnapshotWithFilter(f func(sessionId uint32, client *Client) bool, estimatedSizeFactor float64) []*Client {
storage.mu.RLock()
defer storage.mu.RUnlock()
return storage.snapshot(f, estimatedSizeFactor)
}
func (storage *ClientStorage) Snapshot() []*Client {
return storage.SnapshotWithFilter(func(sId uint32, c *Client) bool { return true }, 1)
}