-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.go
212 lines (172 loc) · 4.64 KB
/
pool.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
203
204
205
206
207
208
209
210
211
212
package eventpool
import (
"sync"
"github.com/niakr1s/chatty-server/app/er"
"github.com/niakr1s/chatty-server/app/events"
log "github.com/sirupsen/logrus"
)
// Pool ...
type Pool struct {
sync.Mutex
// events.Event inputs here
inputCh chan events.Event
userCh map[string]*EventChan
userChFilter func(username string) FilterPass
innerCh []*EventChan
}
// NewPool ...
func NewPool() *Pool {
return &Pool{
inputCh: make(chan events.Event, 10),
userCh: make(map[string]*EventChan),
userChFilter: func(username string) FilterPass { return FilterPassAlways },
innerCh: make([]*EventChan, 0),
}
}
// WithUserChFilter ...
func (p *Pool) WithUserChFilter(f func(username string) FilterPass) *Pool {
p.userChFilter = f
return p
}
// GetInputChan ...
func (p *Pool) GetInputChan() chan<- events.Event {
return p.inputCh
}
// GetUserChan gets chan for user
// chans are created with default filter events.FilterPassIfUserInChat
func (p *Pool) GetUserChan(username string) <-chan events.Event {
p.Lock()
defer p.Unlock()
return p.getUserChan(username)
}
// GetUserChan gets chan for user
// chans are created with default filter events.FilterPassIfUserInChat
func (p *Pool) getUserChan(username string) <-chan events.Event {
if _, ok := p.userCh[username]; !ok {
p.createUserChan(username)
}
return p.userCh[username].Ch
}
func (p *Pool) createUserChan(username string) {
p.userCh[username] = NewEventChan().
WithFilter(p.userChFilter(username))
}
func (p *Pool) removeUserChan(username string) {
if u, ok := p.userCh[username]; ok {
close(u.Ch)
delete(p.userCh, username)
}
}
// CreateChanNoFilter creates chan with no filter
func (p *Pool) CreateChanNoFilter() <-chan events.Event {
return p.CreateChan(FilterPassAlways)
}
// CreateChan creates chan with filter
func (p *Pool) CreateChan(filter FilterPass) <-chan events.Event {
p.Lock()
defer p.Unlock()
return p.createChan(filter)
}
// CreateChan creates chan with filter
func (p *Pool) createChan(filter FilterPass) <-chan events.Event {
ec := NewEventChan().WithFilter(filter)
p.innerCh = append(p.innerCh, ec)
return ec.Ch
}
// Run ...
func (p *Pool) Run() {
go func() {
for {
event := <-p.inputCh
log.Tracef("event: %v", event)
p.Lock()
p.beforeSending(event)
p.sendInUserChans(event)
p.sendInInnerChans(event)
p.Unlock()
}
}()
}
func (p *Pool) beforeSending(event events.Event) {
p.processLogoutEvent(event)
p.processLoginEvent(event)
p.processChatJoinEvent(event)
p.processChatLeaveEvent(event)
}
func (p *Pool) processChatJoinEvent(event events.Event) {
chatJoin, ok := event.(*events.ChatJoinEvent)
if !ok {
return
}
inChat, err := chatJoin.InChat()
// discarding global events
if err != nil {
return
}
systemMessageEvent := events.NewSystemMessageChatJoinEvent(inChat, chatJoin.UserName, chatJoin.Time)
go func() {
p.inputCh <- systemMessageEvent
}()
}
func (p *Pool) processChatLeaveEvent(event events.Event) {
chatLeave, ok := event.(*events.ChatLeaveEvent)
if !ok {
return
}
inChat, err := chatLeave.InChat()
// discarding global events
if err != nil {
return
}
systemMessageEvent := events.NewSystemMessageChatLeaveEvent(inChat, chatLeave.UserName, chatLeave.Time)
go func() {
p.inputCh <- systemMessageEvent
}()
}
func (p *Pool) processLogoutEvent(event events.Event) {
if logout, ok := event.(*events.LogoutEvent); ok {
if _, err := logout.InChat(); err == er.ErrGlobalEvent {
p.removeUserChan(logout.UserName)
}
}
}
func (p *Pool) processLoginEvent(event events.Event) {
if login, ok := event.(*events.LoginEvent); ok {
if _, err := login.InChat(); err == er.ErrGlobalEvent {
p.removeUserChan(login.UserName) // to renew channel
p.createUserChan(login.UserName)
}
}
}
func (p *Pool) sendInUserChans(event events.Event) {
for k, eventCh := range p.userCh {
if eventCh.Filter(event) {
select {
case eventCh.Ch <- event:
default:
// if channel is full - close and delete it, it's safe for map
p.removeUserChan(k)
}
}
}
}
func (p *Pool) sendInInnerChans(event events.Event) {
n := len(p.innerCh)
for i := 0; i < n; i++ {
eventCh := p.innerCh[i]
if eventCh.Filter(event) {
select {
case eventCh.Ch <- event:
default:
// if channel is full - close it and set it to nil
close(eventCh.Ch)
eventCh = nil
// swapping with last element
p.innerCh[i], p.innerCh[n-1] = p.innerCh[n-1], p.innerCh[i]
i--
n--
}
}
}
p.innerCh = p.innerCh[:n]
}