/
connection_mgr.go
221 lines (182 loc) · 5.23 KB
/
connection_mgr.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
213
214
215
216
217
218
219
220
221
package peermanager
import (
"sync"
"time"
"github.com/ellcrys/elld/util/logger"
"github.com/ellcrys/elld/util"
net "github.com/libp2p/go-libp2p-net"
ma "github.com/multiformats/go-multiaddr"
)
// ConnsInfo stores information about connections
// such as the number of inbound and outbound
// connections, etc
type ConnsInfo struct {
sync.RWMutex
inbound int
outbound int
}
// NewConnsInfo creates an instance of ConnsInfo
func NewConnsInfo(inbound, outbound int) *ConnsInfo {
return &ConnsInfo{
inbound: inbound,
outbound: outbound,
}
}
// Info returns the number of inbound and outbound connections
func (i *ConnsInfo) Info() (inbound int, outbound int) {
i.RLock()
defer i.RUnlock()
return i.inbound, i.outbound
}
// IncOutbound increments outbound count
func (i *ConnsInfo) IncOutbound() {
i.Lock()
defer i.Unlock()
i.outbound++
}
// DecrOutbound decrements outbound count
func (i *ConnsInfo) DecrOutbound() {
i.Lock()
defer i.Unlock()
i.outbound--
}
// IncInbound increments inbound count
func (i *ConnsInfo) IncInbound() {
i.Lock()
defer i.Unlock()
i.inbound++
}
// DecrInbound decrements outbound count
func (i *ConnsInfo) DecrInbound() {
i.Lock()
defer i.Unlock()
i.inbound--
}
// ConnectionManager manages the active connections
// ensuring the required number of connections at any given
// time is maintained
type ConnectionManager struct {
sync.RWMutex
pm *Manager
log logger.Logger
tickerDone chan bool
connsInfo *ConnsInfo
}
// NewConnMrg creates a new connection manager
func NewConnMrg(m *Manager, log logger.Logger) *ConnectionManager {
return &ConnectionManager{
pm: m,
log: log,
connsInfo: NewConnsInfo(0, 0),
}
}
// Manage starts connection management
func (m *ConnectionManager) Manage() {
go m.makeConnections(m.tickerDone)
}
// SetConnsInfo sets the connections information.
// Only used in tests.
func (m *ConnectionManager) SetConnsInfo(info *ConnsInfo) {
m.connsInfo = info
}
// GetConnsCount gets the inbound and outbound
// connections count.
func (m *ConnectionManager) GetConnsCount() *ConnsInfo {
m.RLock()
defer m.RUnlock()
return m.connsInfo
}
// makeConnections will attempt to send a handshake to
// addresses that have not been connected to as long
// as the max connection limit has not been reached
func (m *ConnectionManager) makeConnections(done chan bool) {
dur := time.Duration(m.pm.config.Node.ConnEstInterval)
ticker := time.NewTicker(dur * time.Second)
for {
select {
case <-ticker.C:
if m.pm.localNode.IsNetworkDisabled() {
continue
}
if !m.pm.RequirePeers() {
continue
}
// Get unconnected/unacquainted peers
peers := m.pm.GetLonelyPeers()
if len(peers) == 0 {
continue
}
m.log.Debug("Establishing connection with more peers", "UnconnectedPeers",
len(peers))
for _, p := range peers {
// Do not establish connection with banned peers
if m.pm.IsBanned(p) {
continue
}
m.pm.ConnectToPeer(p.StringID())
}
case <-done:
ticker.Stop()
return
}
}
}
// Listen is called when hosts starts
// listening on an address
func (m *ConnectionManager) Listen(net.Network, ma.Multiaddr) {}
// ListenClose is called when host stops
// listening on an address
func (m *ConnectionManager) ListenClose(net.Network, ma.Multiaddr) {}
// Connected is called when a connection is opened.
// Check inbound and outbound connection count state
// and close connections when limits are reached.
func (m *ConnectionManager) Connected(n net.Network, conn net.Conn) {
// Update inbound/outbound connection count
go func(n net.Network, conn net.Conn) {
curInboundConns, curOutboundConns := m.connsInfo.Info()
if conn.Stat().Direction == net.DirInbound {
m.connsInfo.IncInbound()
if int64(curInboundConns) > m.pm.config.Node.MaxInboundConnections {
m.log.Debug("Closed inbound connection. Max. limit reached",
"MaxAllowed", m.pm.config.Node.MaxInboundConnections)
conn.Close()
}
}
if conn.Stat().Direction == net.DirOutbound {
m.connsInfo.IncOutbound()
if int64(curOutboundConns) > m.pm.config.Node.MaxOutboundConnections {
m.log.Debug("Closed outbound connection. Max. limit reached",
"MaxAllowed", m.pm.config.Node.MaxOutboundConnections)
conn.Close()
}
}
}(n, conn)
// Reset connection failure count
rnAddr := util.RemoteAddrFromConn(conn)
m.pm.ClearConnFailCount(rnAddr)
}
// Disconnected is called when a connection is closed.
// Update the connection count and inform the peer
// manager of the disconnection event.
func (m *ConnectionManager) Disconnected(n net.Network, conn net.Conn) {
if conn.Stat().Direction == net.DirInbound {
m.connsInfo.DecrInbound()
}
if conn.Stat().Direction == net.DirOutbound {
m.connsInfo.DecrOutbound()
}
addr := util.RemoteAddrFromConn(conn)
m.pm.HasDisconnected(addr)
}
// OpenedStream is called when a stream is openned
func (m *ConnectionManager) OpenedStream(n net.Network, s net.Stream) {
// Reset the stream if local node has stopped
if m.pm.localNode.HasStopped() {
s.Reset()
}
rAddr := util.RemoteAddrFromConn(s.Conn())
// Reset connection failure count
m.pm.ClearConnFailCount(rAddr)
}
// ClosedStream is called when a stream is openned
func (m *ConnectionManager) ClosedStream(nt net.Network, s net.Stream) {}