Skip to content

Commit

Permalink
dht/query: err return NotFound case
Browse files Browse the repository at this point in the history
When some queries finished, but we got no result, it should
be a simple NotFoundError. Only when every single query ended
in error do we externalize those to the client, in case
something major is going wrong
  • Loading branch information
jbenet committed Jan 19, 2015
1 parent e73245d commit 1db5be1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
10 changes: 10 additions & 0 deletions routing/dht/ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func TestGetFailures(t *testing.T) {
// u.POut("Timout Test\n")
ctx1, _ := context.WithTimeout(context.Background(), 200*time.Millisecond)
if _, err := d.GetValue(ctx1, u.Key("test")); err != nil {
if merr, ok := err.(u.MultiErr); ok && len(merr) > 0 {
err = merr[0]
}

if err != context.DeadlineExceeded {
t.Fatal("Got different error than we expected", err)
}
Expand Down Expand Up @@ -86,6 +90,9 @@ func TestGetFailures(t *testing.T) {
ctx2, _ := context.WithTimeout(context.Background(), 20*time.Second)
_, err = d.GetValue(ctx2, u.Key("test"))
if err != nil {
if merr, ok := err.(u.MultiErr); ok && len(merr) > 0 {
err = merr[0]
}
if err != routing.ErrNotFound {
t.Fatalf("Expected ErrNotFound, got: %s", err)
}
Expand Down Expand Up @@ -202,6 +209,9 @@ func TestNotFound(t *testing.T) {
v, err := d.GetValue(ctx, u.Key("hello"))
log.Debugf("get value got %v", v)
if err != nil {
if merr, ok := err.(u.MultiErr); ok && len(merr) > 0 {
err = merr[0]
}
switch err {
case routing.ErrNotFound:
//Success!
Expand Down
11 changes: 8 additions & 3 deletions routing/dht/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type dhtQueryRunner struct {
peersRemaining todoctr.Counter // peersToQuery + currently processing

result *dhtQueryResult // query result
errs []error // result errors. maybe should be a map[peer.ID]error
errs u.MultiErr // result errors. maybe should be a map[peer.ID]error

rateLimit chan struct{} // processing semaphore
log eventlog.EventLogger
Expand Down Expand Up @@ -122,8 +122,13 @@ func (r *dhtQueryRunner) Run(peers []peer.ID) (*dhtQueryResult, error) {
r.RLock()
defer r.RUnlock()

if len(r.errs) > 0 {
err = r.errs[0] // take the first?
err = routing.ErrNotFound

// if every query to every peer failed, something must be very wrong.
if len(r.errs) > 0 && len(r.errs) == r.peersSeen.Size() {
err = r.errs
} else {
log.Infof("query errs: %s", r.errs)
}

case <-r.cg.Closed():
Expand Down

0 comments on commit 1db5be1

Please sign in to comment.