-
Notifications
You must be signed in to change notification settings - Fork 669
/
connections.go
51 lines (39 loc) · 909 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
50
51
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package pubsub
import (
"sync"
"github.com/ava-labs/avalanchego/utils/set"
)
type connections struct {
lock sync.RWMutex
conns set.Set[*connection]
connsList []Filter
}
func newConnections() *connections {
return &connections{}
}
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()
c.conns.Remove(conn)
c.createConnsList()
}
func (c *connections) Add(conn *connection) {
c.lock.Lock()
defer c.lock.Unlock()
c.conns.Add(conn)
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
}