Skip to content

Commit

Permalink
feat: add tinder opts
Browse files Browse the repository at this point in the history
Signed-off-by: gfanton <8671905+gfanton@users.noreply.github.com>
  • Loading branch information
gfanton committed Apr 29, 2021
1 parent 787d39d commit 226a8e9
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 27 deletions.
38 changes: 24 additions & 14 deletions go/internal/initutil/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ func (m *Manager) SetupLocalIPFSFlags(fs *flag.FlagSet) {
fs.StringVar(&m.Node.Protocol.Announce, "p2p.swarm-announce", "", "IPFS announce addrs")
fs.StringVar(&m.Node.Protocol.NoAnnounce, "p2p.swarm-no-announce", "", "IPFS exclude announce addrs")
fs.BoolVar(&m.Node.Protocol.LocalDiscovery, "p2p.local-discovery", true, "if true local discovery will be enabled")
fs.BoolVar(&m.Node.Protocol.TinderDHTDriver, "p2p.tinder-dht-driver", true, "if true dht driver will be enable for tinder")
fs.BoolVar(&m.Node.Protocol.TinderRDVPDriver, "p2p.tinder-rdvp-driver", true, "if true rdvp driver will be enable for tinder")
fs.BoolVar(&m.Node.Protocol.UseStaticRelays, "p2p.use-static-relays", true, "if true will use static relays form the config, otherwise it will try to discover relays over the dht")
fs.DurationVar(&m.Node.Protocol.MinBackoff, "p2p.min-backoff", time.Second, "minimum p2p backoff duration")
fs.DurationVar(&m.Node.Protocol.MaxBackoff, "p2p.max-backoff", time.Minute, "maximum p2p backoff duration")
fs.DurationVar(&m.Node.Protocol.MinBackoff, "p2p.min-backoff", time.Minute, "minimum p2p backoff duration")
fs.DurationVar(&m.Node.Protocol.MaxBackoff, "p2p.max-backoff", time.Minute*10, "maximum p2p backoff duration")
fs.DurationVar(&m.Node.Protocol.PollInterval, "p2p.poll-interval", pubsub.DiscoveryPollInterval, "how long the discovery system will waits for more peers")
fs.StringVar(&m.Node.Protocol.RdvpMaddrs, "p2p.rdvp", ":default:", `list of rendezvous point maddr, ":dev:" will add the default devs servers, ":none:" will disable rdvp`)
fs.BoolVar(&m.Node.Protocol.Ble.Enable, "p2p.ble", ble.Supported, "if true Bluetooth Low Energy will be enabled")
Expand Down Expand Up @@ -446,22 +448,29 @@ func (m *Manager) configIPFSRouting(h host.Host, r p2p_routing.Routing) error {

rng := mrand.New(mrand.NewSource(srand.MustSecure())) // nolint:gosec // we need to use math/rand here, but it is seeded from crypto/rand

// rdvp driver
// configure tinder drivers
var drivers []*tinder.Driver
if lenrdvpeers := len(rdvpeers); lenrdvpeers > 0 {
for _, peer := range rdvpeers {
h.Peerstore().AddAddrs(peer.ID, peer.Addrs, peerstore.PermanentAddrTTL)
udisc := tinder.NewRendezvousDiscovery(logger, h, peer.ID, rng)

name := fmt.Sprintf("rdvp#%.6s", peer.ID)
drivers = append(drivers,
tinder.NewDriverFromUnregisterDiscovery(name, udisc, tinder.FilterPublicAddrs))

// rdvp driver
if m.Node.Protocol.TinderRDVPDriver {
if lenrdvpeers := len(rdvpeers); lenrdvpeers > 0 {
for _, peer := range rdvpeers {
h.Peerstore().AddAddrs(peer.ID, peer.Addrs, peerstore.PermanentAddrTTL)
udisc := tinder.NewRendezvousDiscovery(logger, h, peer.ID, rng)

name := fmt.Sprintf("rdvp#%.6s", peer.ID)
drivers = append(drivers,
tinder.NewDriverFromUnregisterDiscovery(name, udisc, tinder.FilterPublicAddrs))
}
}
}

// dht driver
drivers = append(drivers,
tinder.NewDriverFromRouting("dht", r, nil))
if m.Node.Protocol.TinderDHTDriver {
// dht driver
drivers = append(drivers,
tinder.NewDriverFromRouting("dht", r, nil))
}

// localdisc driver
if m.Node.Protocol.LocalDiscovery {
Expand All @@ -480,7 +489,8 @@ func (m *Manager) configIPFSRouting(h host.Host, r p2p_routing.Routing) error {

tinderOpts := &tinder.Opts{
Logger: logger,
AdvertiseResetInterval: time.Minute,
AdvertiseResetInterval: time.Minute * 2,
FindPeerResetInterval: time.Minute * 2,
AdvertiseGracePeriod: time.Minute,
BackoffStrategy: &tinder.BackoffOpts{
StratFactory: backoffstrat,
Expand Down
2 changes: 2 additions & 0 deletions go/internal/initutil/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ type Manager struct {
Announce string `json:"Announce,omitempty"`
NoAnnounce string `json:"NoAnnounce,omitempty"`
LocalDiscovery bool `json:"LocalDiscovery,omitempty"`
TinderDHTDriver bool `json:"TinderDHTDriver,omitempty"`
TinderRDVPDriver bool `json:"TinderRDVPDriver,omitempty"`
UseStaticRelays bool `json:"UseStaticRelays,omitempty"`
Ble struct {
Enable bool `json:"Enable,omitempty"`
Expand Down
8 changes: 2 additions & 6 deletions go/internal/tinder/driver_mocked.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package tinder

import (
"context"
"fmt"
"sync"
"time"

Expand Down Expand Up @@ -112,11 +111,8 @@ func (s *MockDriverServer) HasPeerRecord(ns string, pid p2p_peer.ID) bool {
if peers, ok := s.db[ns]; ok {
if p, ok := peers[pid]; ok {
now := time.Now()
if p.expiration.After(now) {
fmt.Printf("will expire in: %dms\n", p.expiration.Sub(now).Milliseconds())
return true
}
fmt.Printf("expired since: %dms\n", now.Sub(p.expiration).Milliseconds())
return p.expiration.After(now)
// log.Printf("expired since: %dms\n", now.Sub(p.expiration).Milliseconds())
}
}
return false
Expand Down
2 changes: 0 additions & 2 deletions go/internal/tinder/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tinder
import (
"context"
"fmt"
"log"
"math/rand"
"testing"
"time"
Expand Down Expand Up @@ -348,7 +347,6 @@ func TestFindPeers(t *testing.T) {

count := 0
for p := range cc {
log.Printf("peer: %+v\n", p)
links := m.LinksBetweenPeers(hcl.ID(), p.ID)
assert.Len(t, links, 2)

Expand Down
2 changes: 1 addition & 1 deletion go/internal/tinder/watchdogs_advertiser.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ func (wa *watchdogsAdvertiser) Advertise(_ context.Context, ns string, opts ...p
// wa.unregister(ctx, ns)
})
wa.advertises(wctx, ns, opts...)
wa.logger.Debug("advertise started", zap.String("ns", ns))
}

wa.muAdvertiser.Unlock()

wa.logger.Debug("advertise started", zap.String("ns", ns))
return wa.ttl, nil
}

Expand Down
6 changes: 2 additions & 4 deletions go/internal/tinder/watchdogs_discoverer.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ func newWatchdogsDiscoverer(ctx context.Context, l *zap.Logger, h host.Host, res
if backoff != nil && backoff.StratFactory != nil {
// wrap backoff/cache discovery
var err error
disc, err = discovery.NewBackoffDiscovery(disc, backoff.StratFactory, backoff.DiscoveryOptions...)
if err != nil {
if disc, err = discovery.NewBackoffDiscovery(disc, backoff.StratFactory, backoff.DiscoveryOptions...); err != nil {
return nil, err
}
}
Expand All @@ -69,8 +68,6 @@ func (w *watchdogsDiscoverer) FindPeers(_ context.Context, ns string, opts ...p2
w.mufindpeers.Lock()
defer w.mufindpeers.Unlock()

timer := time.Now()

if ft, ok := w.findpeers[ns]; ok {
// already running find peers
if !ft.T.Stop() {
Expand All @@ -81,6 +78,7 @@ func (w *watchdogsDiscoverer) FindPeers(_ context.Context, ns string, opts ...p2
return w.disc.FindPeers(ft.Ctx, ns, opts...)
}

timer := time.Now()
ctx, cancel := context.WithCancel(ctx)

w.logger.Debug("watchdogs looking for peers", zap.String("ns", ns))
Expand Down

0 comments on commit 226a8e9

Please sign in to comment.