/
connections.go
49 lines (38 loc) · 913 Bytes
/
connections.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
// Copyright (C) 2019-2021, Dijets, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package pubsub
import "sync"
type connections struct {
lock sync.RWMutex
conns map[*connection]struct{}
connsList []Filter
}
func newConnections() *connections {
return &connections{
conns: make(map[*connection]struct{}),
}
}
func (c *connections) Conns() []Filter {
c.lock.RLock()
defer c.lock.RUnlock()
return append([]Filter{}, c.connsList...)
}
func (c *connections) Remove(conn *connection) {
c.lock.Lock()
defer c.lock.Unlock()
delete(c.conns, conn)
c.createConnsList()
}
func (c *connections) Add(conn *connection) {
c.lock.Lock()
defer c.lock.Unlock()
c.conns[conn] = struct{}{}
c.createConnsList()
}
func (c *connections) createConnsList() {
resp := make([]Filter, 0, len(c.conns))
for c := range c.conns {
resp = append(resp, c)
}
c.connsList = resp
}