fix(dialQueue): account for failed dials

fixes #276
This commit is contained in:
Steven Allen 2019-02-26 19:30:59 -07:00
parent eb15b3132e
commit c78d1e6786
3 changed files with 43 additions and 1 deletions

View File

@ -901,6 +901,41 @@ func TestLayeredGet(t *testing.T) {
} }
} }
func TestUnfindablePeer(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
maddrs, peers, dhts := setupDHTS(ctx, 4, t)
defer func() {
for i := 0; i < 4; i++ {
dhts[i].Close()
dhts[i].host.Close()
}
}()
connect(t, ctx, dhts[0], dhts[1])
connect(t, ctx, dhts[1], dhts[2])
connect(t, ctx, dhts[2], dhts[3])
// Give DHT 1 a bad addr for DHT 2.
dhts[1].host.Peerstore().ClearAddrs(peers[2])
dhts[1].host.Peerstore().AddAddr(peers[2], maddrs[0], time.Minute)
ctxT, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
_, err := dhts[0].FindPeer(ctxT, peers[3])
if err == nil {
t.Error("should have failed to find peer")
}
if ctxT.Err() != nil {
t.Error("FindPeer should have failed before context expired")
}
}
func TestFindPeer(t *testing.T) { func TestFindPeer(t *testing.T) {
// t.Skip("skipping test to debug another") // t.Skip("skipping test to debug another")
if testing.Short() { if testing.Short() {

View File

@ -315,7 +315,11 @@ func (dq *dialQueue) worker() {
return return
case <-idleTimer.C: case <-idleTimer.C:
// no new dial requests during our idle period; time to scale down. // no new dial requests during our idle period; time to scale down.
case p := <-dq.in.DeqChan: case p, ok := <-dq.in.DeqChan:
if !ok {
return
}
t := time.Now() t := time.Now()
if err := dq.dialFn(dq.ctx, p); err != nil { if err := dq.dialFn(dq.ctx, p); err != nil {
logger.Debugf("discarding dialled peer because of error: %v", err) logger.Debugf("discarding dialled peer because of error: %v", err)

View File

@ -259,6 +259,9 @@ func (r *dhtQueryRunner) dialPeer(ctx context.Context, p peer.ID) error {
r.Lock() r.Lock()
r.errs = append(r.errs, err) r.errs = append(r.errs, err)
r.Unlock() r.Unlock()
// This peer is dropping out of the race.
r.peersRemaining.Decrement(1)
return err return err
} }
logger.Debugf("connected. dial success.") logger.Debugf("connected. dial success.")