From 7847863619d8fa36df371db9d33be919493bfc44 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 9 Apr 2020 13:35:11 -0700 Subject: [PATCH] feat: expose WANActive That way, external users can easily determine which DHT they should use. E.g., in go-ipfs, I'd like to call GetClosestPeers in the `ipfs dht` commands based on the "active" DHT. --- dual/dual.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/dual/dual.go b/dual/dual.go index f60e6a92a..64068e125 100644 --- a/dual/dual.go +++ b/dual/dual.go @@ -77,13 +77,14 @@ func (dht *DHT) Close() error { return multierror.Append(dht.WAN.Close(), dht.LAN.Close()).ErrorOrNil() } -func (dht *DHT) activeWAN() bool { +// WANActive returns true when the WAN DHT is active (has peers). +func (dht *DHT) WANActive() bool { return dht.WAN.RoutingTable().Size() > 0 } // Provide adds the given cid to the content routing system. func (dht *DHT) Provide(ctx context.Context, key cid.Cid, announce bool) error { - if dht.activeWAN() { + if dht.WANActive() { return dht.WAN.Provide(ctx, key, announce) } return dht.LAN.Provide(ctx, key, announce) @@ -167,7 +168,7 @@ func (dht *DHT) Bootstrap(ctx context.Context) error { // PutValue adds value corresponding to given Key. func (dht *DHT) PutValue(ctx context.Context, key string, val []byte, opts ...routing.Option) error { - if dht.activeWAN() { + if dht.WANActive() { return dht.WAN.PutValue(ctx, key, val, opts...) } return dht.LAN.PutValue(ctx, key, val, opts...)