mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
p2p: preserve enode.ID type in the metered connection
This commit is contained in:
parent
4e693ad5a6
commit
67c6281ab2
2 changed files with 16 additions and 8 deletions
|
|
@ -253,3 +253,12 @@ func RandomID(a ID, n int) (b ID) {
|
||||||
}
|
}
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Equal(a, b ID) bool {
|
||||||
|
for i := range a {
|
||||||
|
if a[i] != b[i] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -74,7 +74,7 @@ const (
|
||||||
type MeteredPeerEvent struct {
|
type MeteredPeerEvent struct {
|
||||||
Type MeteredPeerEventType // Type of peer event
|
Type MeteredPeerEventType // Type of peer event
|
||||||
IP net.IP // IP address of the peer
|
IP net.IP // IP address of the peer
|
||||||
ID string // NodeID of the peer
|
ID enode.ID // NodeID of the peer
|
||||||
Elapsed time.Duration // Time elapsed between the connection and the handshake/disconnection
|
Elapsed time.Duration // Time elapsed between the connection and the handshake/disconnection
|
||||||
Ingress uint64 // Ingress count at the moment of the event
|
Ingress uint64 // Ingress count at the moment of the event
|
||||||
Egress uint64 // Egress count at the moment of the event
|
Egress uint64 // Egress count at the moment of the event
|
||||||
|
|
@ -93,7 +93,7 @@ type meteredConn struct {
|
||||||
|
|
||||||
connected time.Time // Connection time of the peer
|
connected time.Time // Connection time of the peer
|
||||||
ip net.IP // IP address of the peer
|
ip net.IP // IP address of the peer
|
||||||
id string // NodeID of the peer
|
id enode.ID // NodeID of the peer
|
||||||
|
|
||||||
// trafficMetered denotes if the peer is registered in the traffic registries.
|
// trafficMetered denotes if the peer is registered in the traffic registries.
|
||||||
// Its value is true if the metered peer count doesn't reach the limit in the
|
// Its value is true if the metered peer count doesn't reach the limit in the
|
||||||
|
|
@ -161,18 +161,17 @@ func (c *meteredConn) Write(b []byte) (n int, err error) {
|
||||||
// the ingress and the egress traffic registries using the peer's IP and node ID,
|
// the ingress and the egress traffic registries using the peer's IP and node ID,
|
||||||
// also emits connect event.
|
// also emits connect event.
|
||||||
func (c *meteredConn) handshakeDone(nodeID enode.ID) {
|
func (c *meteredConn) handshakeDone(nodeID enode.ID) {
|
||||||
id := nodeID.String()
|
|
||||||
if atomic.AddInt32(&meteredPeerCount, 1) >= MeteredPeerLimit {
|
if atomic.AddInt32(&meteredPeerCount, 1) >= MeteredPeerLimit {
|
||||||
// Don't register the peer in the traffic registries.
|
// Don't register the peer in the traffic registries.
|
||||||
atomic.AddInt32(&meteredPeerCount, -1)
|
atomic.AddInt32(&meteredPeerCount, -1)
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
c.id, c.trafficMetered = id, false
|
c.id, c.trafficMetered = nodeID, false
|
||||||
c.lock.Unlock()
|
c.lock.Unlock()
|
||||||
log.Warn("Metered peer count reached the limit")
|
log.Warn("Metered peer count reached the limit")
|
||||||
} else {
|
} else {
|
||||||
key := fmt.Sprintf("%s/%s", c.ip, id)
|
key := fmt.Sprintf("%s/%s", c.ip, nodeID.String())
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
c.id, c.trafficMetered = id, true
|
c.id, c.trafficMetered = nodeID, true
|
||||||
c.ingressMeter = metrics.NewRegisteredMeter(key, PeerIngressRegistry)
|
c.ingressMeter = metrics.NewRegisteredMeter(key, PeerIngressRegistry)
|
||||||
c.egressMeter = metrics.NewRegisteredMeter(key, PeerEgressRegistry)
|
c.egressMeter = metrics.NewRegisteredMeter(key, PeerEgressRegistry)
|
||||||
c.lock.Unlock()
|
c.lock.Unlock()
|
||||||
|
|
@ -180,7 +179,7 @@ func (c *meteredConn) handshakeDone(nodeID enode.ID) {
|
||||||
meteredPeerFeed.Send(MeteredPeerEvent{
|
meteredPeerFeed.Send(MeteredPeerEvent{
|
||||||
Type: PeerConnected,
|
Type: PeerConnected,
|
||||||
IP: c.ip,
|
IP: c.ip,
|
||||||
ID: id,
|
ID: nodeID,
|
||||||
Elapsed: time.Since(c.connected),
|
Elapsed: time.Since(c.connected),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -190,7 +189,7 @@ func (c *meteredConn) handshakeDone(nodeID enode.ID) {
|
||||||
func (c *meteredConn) Close() error {
|
func (c *meteredConn) Close() error {
|
||||||
err := c.Conn.Close()
|
err := c.Conn.Close()
|
||||||
c.lock.RLock()
|
c.lock.RLock()
|
||||||
if c.id == "" {
|
if enode.Equal(c.id, enode.ID{}) {
|
||||||
// If the peer disconnects before the handshake.
|
// If the peer disconnects before the handshake.
|
||||||
c.lock.RUnlock()
|
c.lock.RUnlock()
|
||||||
meteredPeerFeed.Send(MeteredPeerEvent{
|
meteredPeerFeed.Send(MeteredPeerEvent{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue