Skip to content

Commit 7e74bc8

Browse files
authored
Merge pull request #582 from libp2p/fix/more-error-cleanup
fix: improve error handling in dual dht
2 parents b33ccf3 + 04ed71c commit 7e74bc8

File tree

1 file changed

+52
-22
lines changed

1 file changed

+52
-22
lines changed

dual/dual.go

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ import (
66
"context"
77
"sync"
88

9+
dht "github.com/libp2p/go-libp2p-kad-dht"
10+
911
"github.com/ipfs/go-cid"
1012
ci "github.com/libp2p/go-libp2p-core/crypto"
1113
"github.com/libp2p/go-libp2p-core/host"
1214
"github.com/libp2p/go-libp2p-core/peer"
1315
"github.com/libp2p/go-libp2p-core/protocol"
1416
"github.com/libp2p/go-libp2p-core/routing"
15-
dht "github.com/libp2p/go-libp2p-kad-dht"
17+
kb "github.com/libp2p/go-libp2p-kbucket"
1618
helper "github.com/libp2p/go-libp2p-routing-helpers"
17-
1819
ma "github.com/multiformats/go-multiaddr"
1920

2021
"github.com/hashicorp/go-multierror"
@@ -76,7 +77,7 @@ func New(ctx context.Context, h host.Host, options ...dht.Option) (*DHT, error)
7677

7778
// Close closes the DHT context.
7879
func (dht *DHT) Close() error {
79-
return multierror.Append(dht.WAN.Close(), dht.LAN.Close()).ErrorOrNil()
80+
return combineErrors(dht.WAN.Close(), dht.LAN.Close())
8081
}
8182

8283
func (dht *DHT) activeWAN() bool {
@@ -153,31 +154,60 @@ func (dht *DHT) FindPeer(ctx context.Context, pid peer.ID) (peer.AddrInfo, error
153154

154155
wg.Wait()
155156

156-
// combine addresses
157-
deduped := make(map[string]ma.Multiaddr, len(wanInfo.Addrs)+len(lanInfo.Addrs))
158-
for _, addr := range wanInfo.Addrs {
159-
deduped[string(addr.Bytes())] = addr
157+
// Combine addresses. Try to avoid doing unnecessary work while we're at
158+
// it. Note: We're ignoring the errors for now as many of our DHT
159+
// commands can return both a result and an error.
160+
ai := peer.AddrInfo{ID: pid}
161+
if len(wanInfo.Addrs) == 0 {
162+
ai.Addrs = lanInfo.Addrs
163+
} else if len(lanInfo.Addrs) == 0 {
164+
ai.Addrs = wanInfo.Addrs
165+
} else {
166+
// combine addresses
167+
deduped := make(map[string]ma.Multiaddr, len(wanInfo.Addrs)+len(lanInfo.Addrs))
168+
for _, addr := range wanInfo.Addrs {
169+
deduped[string(addr.Bytes())] = addr
170+
}
171+
for _, addr := range lanInfo.Addrs {
172+
deduped[string(addr.Bytes())] = addr
173+
}
174+
ai.Addrs = make([]ma.Multiaddr, 0, len(deduped))
175+
for _, addr := range deduped {
176+
ai.Addrs = append(ai.Addrs, addr)
177+
}
160178
}
161-
for _, addr := range lanInfo.Addrs {
162-
deduped[string(addr.Bytes())] = addr
179+
180+
// If one of the commands succeeded, don't return an error.
181+
if wanErr == nil || lanErr == nil {
182+
return ai, nil
163183
}
164-
addrs := make([]ma.Multiaddr, 0, len(deduped))
165-
for _, addr := range deduped {
166-
addrs = append(addrs, addr)
184+
185+
// Otherwise, return what we have _and_ return the error.
186+
return ai, combineErrors(wanErr, lanErr)
187+
}
188+
189+
func combineErrors(erra, errb error) error {
190+
// if the errors are the same, just return one.
191+
if erra == errb {
192+
return erra
167193
}
168194

169-
return peer.AddrInfo{
170-
ID: pid,
171-
Addrs: addrs,
172-
}, multierror.Append(wanErr, lanErr).ErrorOrNil()
195+
// If one of the errors is a kb lookup failure (no peers in routing
196+
// table), return the other.
197+
if erra == kb.ErrLookupFailure {
198+
return errb
199+
} else if errb == kb.ErrLookupFailure {
200+
return erra
201+
}
202+
return multierror.Append(erra, errb).ErrorOrNil()
173203
}
174204

175205
// Bootstrap allows callers to hint to the routing system to get into a
176206
// Boostrapped state and remain there.
177207
func (dht *DHT) Bootstrap(ctx context.Context) error {
178208
erra := dht.WAN.Bootstrap(ctx)
179209
errb := dht.LAN.Bootstrap(ctx)
180-
return multierror.Append(erra, errb).ErrorOrNil()
210+
return combineErrors(erra, errb)
181211
}
182212

183213
// PutValue adds value corresponding to given Key.
@@ -209,13 +239,13 @@ func (d *DHT) GetValue(ctx context.Context, key string, opts ...routing.Option)
209239
cancelLan()
210240
}
211241
lanWaiter.Wait()
212-
if wanErr != nil {
213-
if lanErr != nil {
214-
return nil, multierror.Append(wanErr, lanErr).ErrorOrNil()
215-
}
242+
if wanErr == nil {
243+
return wanVal, nil
244+
}
245+
if lanErr == nil {
216246
return lanVal, nil
217247
}
218-
return wanVal, nil
248+
return nil, combineErrors(wanErr, lanErr)
219249
}
220250

221251
// SearchValue searches for better values from this value

0 commit comments

Comments
 (0)