diff options
author | Péter Szilágyi <peterke@gmail.com> | 2015-04-27 19:56:42 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2015-04-27 19:56:42 +0800 |
commit | 706da56f751a6ec98c9f2f29bebc66dffe8d1e2c (patch) | |
tree | 1b3f197f54bc328bc1b61ef4af1e755e5d855419 /p2p | |
parent | 85b4b44235aac41b95c34dbc4de7de91a597ec3c (diff) | |
download | dexon-706da56f751a6ec98c9f2f29bebc66dffe8d1e2c.tar dexon-706da56f751a6ec98c9f2f29bebc66dffe8d1e2c.tar.gz dexon-706da56f751a6ec98c9f2f29bebc66dffe8d1e2c.tar.bz2 dexon-706da56f751a6ec98c9f2f29bebc66dffe8d1e2c.tar.lz dexon-706da56f751a6ec98c9f2f29bebc66dffe8d1e2c.tar.xz dexon-706da56f751a6ec98c9f2f29bebc66dffe8d1e2c.tar.zst dexon-706da56f751a6ec98c9f2f29bebc66dffe8d1e2c.zip |
p2p/discover: wrap the pinger to update the node db too
Diffstat (limited to 'p2p')
-rw-r--r-- | p2p/discover/table.go | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/p2p/discover/table.go b/p2p/discover/table.go index 4058c7bb7..ecfb8d672 100644 --- a/p2p/discover/table.go +++ b/p2p/discover/table.go @@ -288,8 +288,7 @@ func (tab *Table) pingpong(w *bondproc, pinged bool, id NodeID, addr *net.UDPAdd defer func() { tab.bondslots <- struct{}{} }() // Ping the remote side and wait for a pong - tab.db.updateLastPing(id, time.Now()) - if w.err = tab.net.ping(id, addr); w.err != nil { + if w.err = tab.ping(id, addr); w.err != nil { close(w.done) return } @@ -307,14 +306,13 @@ func (tab *Table) pingpong(w *bondproc, pinged bool, id NodeID, addr *net.UDPAdd TCPPort: int(tcpPort), } tab.db.updateNode(w.n) - tab.db.updateLastPong(id, time.Now()) close(w.done) } func (tab *Table) pingreplace(new *Node, b *bucket) { if len(b.entries) == bucketSize { oldest := b.entries[bucketSize-1] - if err := tab.net.ping(oldest.ID, oldest.addr()); err == nil { + if err := tab.ping(oldest.ID, oldest.addr()); err == nil { // The node responded, we don't need to replace it. return } @@ -327,6 +325,19 @@ func (tab *Table) pingreplace(new *Node, b *bucket) { b.entries[0] = new } +// ping a remote endpoint and wait for a reply, also updating the node database +// accordingly. +func (tab *Table) ping(id NodeID, addr *net.UDPAddr) error { + // Update the last ping and send the message + tab.db.updateLastPing(id, time.Now()) + if err := tab.net.ping(id, addr); err != nil { + return err + } + // Pong received, update the database and return + tab.db.updateLastPong(id, time.Now()) + return nil +} + // add puts the entries into the table if their corresponding // bucket is not full. The caller must hold tab.mutex. func (tab *Table) add(entries []*Node) { |