-
Notifications
You must be signed in to change notification settings - Fork 3
/
in_memory_storage.go
179 lines (156 loc) · 3.68 KB
/
in_memory_storage.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
package prekeyserver
import (
"sync"
"github.com/otrv4/gotrx"
)
func isInMemoryStorageDescriptor(desc string) bool {
return desc == "in-memory"
}
type inMemoryStorageFactory struct{}
func (*inMemoryStorageFactory) createStorage() storage {
return createInMemoryStorage()
}
type inMemoryStorageEntry struct {
clientProfiles map[uint32]*gotrx.ClientProfile
prekeyProfiles map[uint32]*prekeyProfile
prekeyMessages map[uint32][]*prekeyMessage
sync.Mutex
}
type inMemoryStorage struct {
perUser map[string]*inMemoryStorageEntry
sync.RWMutex
}
func (s *inMemoryStorageEntry) retrieve() []*prekeyEnsemble {
s.Lock()
defer s.Unlock()
entries := []*prekeyEnsemble{}
for itag, cp := range s.clientProfiles {
pp, ok := s.prekeyProfiles[itag]
pms, ok2 := s.prekeyMessages[itag]
if ok && ok2 && pp != nil && len(pms) > 0 {
entries = append(entries, &prekeyEnsemble{
cp: cp,
pp: pp,
pm: pms[0],
})
s.prekeyMessages[itag] = pms[1:]
}
}
return entries
}
func createInMemoryStorage() *inMemoryStorage {
return &inMemoryStorage{
perUser: make(map[string]*inMemoryStorageEntry),
}
}
func (s *inMemoryStorage) storageEntryFor(from string) *inMemoryStorageEntry {
s.RLock()
se, ok := s.perUser[from]
s.RUnlock()
if !ok {
se = &inMemoryStorageEntry{
clientProfiles: make(map[uint32]*gotrx.ClientProfile),
prekeyProfiles: make(map[uint32]*prekeyProfile),
prekeyMessages: make(map[uint32][]*prekeyMessage),
}
s.Lock()
defer s.Unlock()
s.perUser[from] = se
}
return se
}
func (s *inMemoryStorage) storeClientProfile(from string, cp *gotrx.ClientProfile) error {
if cp != nil {
se := s.storageEntryFor(from)
se.Lock()
defer se.Unlock()
se.clientProfiles[cp.InstanceTag] = cp
}
return nil
}
func (s *inMemoryStorage) storePrekeyProfile(from string, pp *prekeyProfile) error {
if pp != nil {
se := s.storageEntryFor(from)
se.Lock()
defer se.Unlock()
se.prekeyProfiles[pp.instanceTag] = pp
}
return nil
}
func (s *inMemoryStorage) storePrekeyMessages(from string, pms []*prekeyMessage) error {
if len(pms) == 0 {
return nil
}
se := s.storageEntryFor(from)
se.Lock()
defer se.Unlock()
spms := se.prekeyMessages[pms[0].instanceTag]
spms = append(spms, pms...)
se.prekeyMessages[pms[0].instanceTag] = spms
return nil
}
func (s *inMemoryStorage) numberStored(from string, tag uint32) uint32 {
s.RLock()
defer s.RUnlock()
pu, ok := s.perUser[from]
if !ok {
return 0
}
return uint32(len(pu.prekeyMessages[tag]))
}
func (s *inMemoryStorage) retrieveFor(from string) []*prekeyEnsemble {
s.RLock()
pu, ok := s.perUser[from]
s.RUnlock()
if !ok {
return nil
}
return pu.retrieve()
}
func (s *inMemoryStorageEntry) cleanupClientProfiles() {
toRemove := []uint32{}
for itag, cp := range s.clientProfiles {
if cp.HasExpired() {
toRemove = append(toRemove, itag)
}
}
for _, itag := range toRemove {
delete(s.clientProfiles, itag)
}
}
func (s *inMemoryStorageEntry) cleanupPrekeyProfiles() {
toRemove := []uint32{}
for itag, pp := range s.prekeyProfiles {
if pp.hasExpired() {
toRemove = append(toRemove, itag)
}
}
for _, itag := range toRemove {
delete(s.prekeyProfiles, itag)
}
}
func (s *inMemoryStorageEntry) hasAnyEntries() bool {
return len(s.clientProfiles) != 0 ||
len(s.prekeyProfiles) != 0 ||
len(s.prekeyMessages) != 0
}
func (s *inMemoryStorageEntry) cleanup() bool {
s.Lock()
defer s.Unlock()
s.cleanupClientProfiles()
s.cleanupPrekeyProfiles()
return s.hasAnyEntries()
}
func (s *inMemoryStorage) cleanup() {
s.Lock()
defer s.Unlock()
toRemove := []string{}
for pu, pus := range s.perUser {
if !pus.cleanup() {
toRemove = append(toRemove, pu)
}
}
for _, pu := range toRemove {
delete(s.perUser, pu)
}
}