aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2014-05-30 19:04:08 +0800
committerobscuren <geffobscura@gmail.com>2014-05-30 19:04:08 +0800
commitf382221b28ab9e886263e37b1eab9c7924a6a0dc (patch)
treebfe979ce01e02438f6bb9cab8260f8954acc0147
parent6c91ffcfbe1a7b469dc6a8a2676959f0000c925a (diff)
downloaddexon-f382221b28ab9e886263e37b1eab9c7924a6a0dc.tar
dexon-f382221b28ab9e886263e37b1eab9c7924a6a0dc.tar.gz
dexon-f382221b28ab9e886263e37b1eab9c7924a6a0dc.tar.bz2
dexon-f382221b28ab9e886263e37b1eab9c7924a6a0dc.tar.lz
dexon-f382221b28ab9e886263e37b1eab9c7924a6a0dc.tar.xz
dexon-f382221b28ab9e886263e37b1eab9c7924a6a0dc.tar.zst
dexon-f382221b28ab9e886263e37b1eab9c7924a6a0dc.zip
Broadcast "peerList" event upon removing or adding peers
-rw-r--r--ethereum.go22
-rw-r--r--peer.go9
2 files changed, 20 insertions, 11 deletions
diff --git a/ethereum.go b/ethereum.go
index 3a7202d53..6be989a10 100644
--- a/ethereum.go
+++ b/ethereum.go
@@ -165,6 +165,8 @@ func (s *Ethereum) AddPeer(conn net.Conn) {
ethutil.Config.Log.Debugf("[SERV] Max connected peers reached. Not adding incoming peer.")
}
}
+
+ s.reactor.Post("peerList", s.peers)
}
func (s *Ethereum) ProcessPeerList(addrs []string) {
@@ -303,12 +305,26 @@ func (s *Ethereum) Peers() *list.List {
}
func (s *Ethereum) reapPeers() {
+ eachPeer(s.peers, func(p *Peer, e *list.Element) {
+ if atomic.LoadInt32(&p.disconnect) == 1 || (p.inbound && (time.Now().Unix()-p.lastPong) > int64(5*time.Minute)) {
+ s.removePeerElement(e)
+ }
+ })
+}
+
+func (s *Ethereum) removePeerElement(e *list.Element) {
s.peerMut.Lock()
defer s.peerMut.Unlock()
- eachPeer(s.peers, func(p *Peer, e *list.Element) {
- if atomic.LoadInt32(&p.disconnect) == 1 || (p.inbound && (time.Now().Unix()-p.lastPong) > int64(5*time.Minute)) {
- s.peers.Remove(e)
+ s.peers.Remove(e)
+
+ s.reactor.Post("peerList", s.peers)
+}
+
+func (s *Ethereum) RemovePeer(p *Peer) {
+ eachPeer(s.peers, func(peer *Peer, e *list.Element) {
+ if peer == p {
+ s.removePeerElement(e)
}
})
}
diff --git a/peer.go b/peer.go
index 60f2de711..6853a949d 100644
--- a/peer.go
+++ b/peer.go
@@ -2,7 +2,6 @@ package eth
import (
"bytes"
- "container/list"
"fmt"
"github.com/ethereum/eth-go/ethchain"
"github.com/ethereum/eth-go/ethutil"
@@ -523,13 +522,7 @@ func (p *Peer) Stop() {
}
// Pre-emptively remove the peer; don't wait for reaping. We already know it's dead if we are here
- p.ethereum.peerMut.Lock()
- defer p.ethereum.peerMut.Unlock()
- eachPeer(p.ethereum.peers, func(peer *Peer, e *list.Element) {
- if peer == p {
- p.ethereum.peers.Remove(e)
- }
- })
+ p.ethereum.RemovePeer(p)
}
func (p *Peer) pushHandshake() error {