This repository has been archived by the owner on Jun 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathnamescache.go
180 lines (158 loc) · 3.6 KB
/
namescache.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
//go:generate ffjson namescache.go
package main
import (
"sync"
"sync/atomic"
)
// ffjson: skip
type namesCache struct {
users map[Userid]*User
marshallednames []byte
usercount uint32
ircnames [][]string
sync.RWMutex
}
// ffjson: skip
type userChan struct {
user *User
c chan *User
}
type NamesOut struct {
Users []*SimplifiedUser `json:"users"`
Connections uint32 `json:"connectioncount"`
}
var namescache = namesCache{
users: make(map[Userid]*User),
RWMutex: sync.RWMutex{},
}
func initNamesCache() {
}
func (nc *namesCache) getIrcNames() [][]string {
nc.RLock()
defer nc.RUnlock()
return nc.ircnames
}
func (nc *namesCache) marshalNames(updateircnames bool) {
users := make([]*SimplifiedUser, 0, len(nc.users))
var allnames []string
if updateircnames {
allnames = make([]string, 0, len(nc.users))
}
for _, u := range nc.users {
u.RLock()
n := atomic.LoadInt32(&u.connections)
if n <= 0 {
continue
}
users = append(users, u.simplified)
if updateircnames {
prefix := ""
switch {
case u.featureGet(ISADMIN):
prefix = "~" // +q
case u.featureGet(ISBOT):
prefix = "&" // +a
case u.featureGet(ISMODERATOR):
prefix = "@" // +o
case u.featureGet(ISVIP):
prefix = "%" // +h
case u.featureGet(ISSUBSCRIBER):
prefix = "+" // +v
}
allnames = append(allnames, prefix+u.nick)
}
}
if updateircnames {
l := 0
var namelines [][]string
var names []string
for _, name := range allnames {
if l+len(name) > 400 {
namelines = append(namelines, names)
l = 0
names = nil
}
names = append(names, name)
l += len(name)
}
nc.ircnames = namelines
}
n := NamesOut{
Users: users,
Connections: nc.usercount,
}
nc.marshallednames, _ = n.MarshalJSON()
cacheConnectedUsers(nc.marshallednames)
for _, u := range nc.users {
u.RUnlock()
}
}
func (nc *namesCache) getNames() []byte {
nc.RLock()
defer nc.RUnlock()
return nc.marshallednames
}
func (nc *namesCache) get(id Userid) *User {
nc.RLock()
defer nc.RUnlock()
u, _ := nc.users[id]
return u
}
func (nc *namesCache) add(user *User) *User {
nc.Lock()
defer nc.Unlock()
nc.usercount++
var updateircnames bool
if u, ok := nc.users[user.id]; ok {
atomic.AddInt32(&u.connections, 1)
} else {
updateircnames = true
atomic.AddInt32(&user.connections, 1)
su := &SimplifiedUser{
Nick: user.nick,
Features: user.simplified.Features,
}
user.simplified = su
nc.users[user.id] = user
}
nc.marshalNames(updateircnames)
return nc.users[user.id]
}
func (nc *namesCache) disconnect(user *User) {
nc.Lock()
defer nc.Unlock()
var updateircnames bool
if user != nil {
nc.usercount--
if u, ok := nc.users[user.id]; ok {
conncount := atomic.AddInt32(&u.connections, -1)
if conncount <= 0 {
// we do not delete the users so that the lastmessage is preserved for
// anti-spam purposes, sadly this means memory usage can only go up
updateircnames = true
}
}
} else {
nc.usercount--
}
nc.marshalNames(updateircnames)
}
func (nc *namesCache) refresh(user *User) {
nc.RLock()
defer nc.RUnlock()
if u, ok := nc.users[user.id]; ok {
u.Lock()
u.simplified.Nick = user.nick
u.simplified.Features = user.simplified.Features
u.nick = user.nick
u.features = user.features
u.Unlock()
nc.marshalNames(true)
}
}
func (nc *namesCache) addConnection() {
nc.Lock()
defer nc.Unlock()
nc.usercount++
nc.marshalNames(false)
}