p2p/discover: wrap the pinger to update the node db too

This commit is contained in:
Péter Szilágyi 2015-04-27 14:56:42 +03:00
parent 85b4b44235
commit 706da56f75

View File

@ -288,8 +288,7 @@ func (tab *Table) pingpong(w *bondproc, pinged bool, id NodeID, addr *net.UDPAdd
defer func() { tab.bondslots <- struct{}{} }() defer func() { tab.bondslots <- struct{}{} }()
// Ping the remote side and wait for a pong // Ping the remote side and wait for a pong
tab.db.updateLastPing(id, time.Now()) if w.err = tab.ping(id, addr); w.err != nil {
if w.err = tab.net.ping(id, addr); w.err != nil {
close(w.done) close(w.done)
return return
} }
@ -307,14 +306,13 @@ func (tab *Table) pingpong(w *bondproc, pinged bool, id NodeID, addr *net.UDPAdd
TCPPort: int(tcpPort), TCPPort: int(tcpPort),
} }
tab.db.updateNode(w.n) tab.db.updateNode(w.n)
tab.db.updateLastPong(id, time.Now())
close(w.done) close(w.done)
} }
func (tab *Table) pingreplace(new *Node, b *bucket) { func (tab *Table) pingreplace(new *Node, b *bucket) {
if len(b.entries) == bucketSize { if len(b.entries) == bucketSize {
oldest := b.entries[bucketSize-1] 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. // The node responded, we don't need to replace it.
return return
} }
@ -327,6 +325,19 @@ func (tab *Table) pingreplace(new *Node, b *bucket) {
b.entries[0] = new 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 // add puts the entries into the table if their corresponding
// bucket is not full. The caller must hold tab.mutex. // bucket is not full. The caller must hold tab.mutex.
func (tab *Table) add(entries []*Node) { func (tab *Table) add(entries []*Node) {