Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move go-libp2p-discovery here #1291

Merged
merged 65 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
257cea0
interface and utility functions
vyzo Oct 12, 2018
d0766bb
discovery implementation using ContentRouting
vyzo Oct 12, 2018
6e5e928
niceties and best practices
vyzo Oct 18, 2018
0cd9c1b
test and gx package update
vyzo Oct 18, 2018
25577e9
document the use of SHA256 as the cid hash function
vyzo Oct 18, 2018
96c2f75
use 6hrs as ttl for routing based advertisements
vyzo Feb 12, 2019
ac2df56
Merge pull request #8 from libp2p/fix/ttl
vyzo Feb 12, 2019
5f26fd8
correctly encode ns to CID
vyzo Feb 14, 2019
6e64956
Merge pull request #11 from libp2p/fix/issue-10
vyzo Feb 14, 2019
250048d
add a timeout to Provide in routing.Advertise
vyzo Feb 14, 2019
2662fe1
clarify comment.
raulk Feb 14, 2019
3352420
Merge pull request #12 from libp2p/fix/provide-timeout
vyzo Feb 14, 2019
cfdf417
use 3hrs as routing advertisement ttl
vyzo Apr 13, 2019
d6e566a
Merge pull request #23 from libp2p/fix/routing-advertisement-period
Stebalien Apr 16, 2019
a5bbace
allow user-spceified TTL in routing advertisements
vyzo May 16, 2019
c36fd0f
add options to utility interface
vyzo May 16, 2019
383cb2a
fix test
vyzo May 16, 2019
7628197
Merge pull request #25 from libp2p/feat/options
vyzo May 17, 2019
bf6bb15
Merge pull request #25 from libp2p/feat/options
vyzo May 17, 2019
83826f4
Consolidate abstractions and core types into go-libp2p-core (#24)
raulk May 26, 2019
47d54ff
Consolidate abstractions and core types into go-libp2p-core (#24)
raulk May 26, 2019
adb766d
added initial backoff cache discovery
aschmahmann Sep 6, 2019
df6fbe1
added discovery based content routing
aschmahmann Sep 6, 2019
702512d
added discovery options to discovery based content routing
aschmahmann Sep 6, 2019
7f58d8f
refactored backoffcache and increased an internal channel size.
aschmahmann Sep 8, 2019
54366d0
refactor constructor functions for BackoffFactory implementations. re…
aschmahmann Sep 8, 2019
be38134
polynomialbackoff: added checks for small degree polynomials
aschmahmann Oct 11, 2019
66d546e
add backoff connector
aschmahmann Oct 11, 2019
1601c60
Merge branch 'feat/backoff' of github.com:libp2p/go-libp2p-discovery …
aschmahmann Oct 11, 2019
d5a0348
fix and tests for backoff connector
aschmahmann Oct 13, 2019
204664a
added configurable channel buffer sizes to the backoff cache.
aschmahmann Oct 13, 2019
77e85c3
Merge pull request #27 from libp2p/feat/content-routing
aschmahmann Oct 16, 2019
50b20ff
added backoff docstrings
aschmahmann Oct 25, 2019
b151fef
merge in discovery based content routing changes. moved mock discover…
aschmahmann Oct 25, 2019
bd1a26b
merge in discovery based content routing changes. moved mock discover…
aschmahmann Oct 25, 2019
fa543cf
cleanup some function signatures
aschmahmann Oct 29, 2019
c635b33
cleanup backoffcache event dispatching logic
aschmahmann Oct 29, 2019
d4ccc21
more documentation and function argument coalescing
aschmahmann Oct 29, 2019
1a259ff
Merge pull request #26 from libp2p/feat/backoff
Stebalien Oct 30, 2019
5c4f2c7
add tests for backoff factory reuse
aschmahmann Apr 22, 2020
412b885
backoff strategy factory constructors take random seeds instead of ra…
aschmahmann Apr 22, 2020
a618d46
backoff strategy factory constructors take sources instead of seeds f…
aschmahmann Apr 22, 2020
e4f2331
Merge pull request #54 from libp2p/fix/53
Stebalien Apr 23, 2020
4c3a313
Put period at end of sentence
edwargix Jun 5, 2020
9c9ee54
Merge pull request #65 from edwargix/master
vyzo Jun 5, 2020
dd7188b
Fix hang in BackoffDiscovery.FindPeers when requesting limit lower th…
iand May 4, 2021
6841689
fix staticcheck
marten-seemann May 19, 2021
39a7c78
Merge pull request #70 from libp2p/fix-staticcheck
marten-seemann May 19, 2021
90403a4
Merge pull request #69 from iand/issue-67
marten-seemann Jun 14, 2021
4e1ef58
remove deprecated types
marten-seemann Jul 22, 2021
8a221ef
remove deprecated types
marten-seemann Jul 22, 2021
9a48259
Merge pull request #73 from libp2p/remove-deprecated-types
Stebalien Jul 22, 2021
b29843f
Merge pull request #73 from libp2p/remove-deprecated-types
Stebalien Jul 22, 2021
0af1a4b
feat: plumb peerstore contexts changes through (#75)
guseggert Nov 10, 2021
6443c8b
feat: plumb peerstore contexts changes through (#75)
guseggert Nov 10, 2021
73bd078
chore: update go-log to v2
marten-seemann Dec 7, 2021
4d687a9
fix flaky TestBackoffDiscoveryMultipleBackoff test on CI (#80)
marten-seemann Jan 3, 2022
8a63d30
remove dependency on the go-libp2p-peerstore/addr package (#82)
marten-seemann Jan 3, 2022
d3243b5
Merge remote-tracking branch 'p2p/discovery/backoff/master' into disc…
marten-seemann Jan 9, 2022
7c0be24
rename p2p/discovery/backoff package, run go mod tidy
marten-seemann Jan 9, 2022
1da627d
Merge remote-tracking branch 'p2p/discovery/util/master' into discove…
marten-seemann Jan 9, 2022
5039dc0
rename p2p/discovery/util package
marten-seemann Jan 9, 2022
b5056a8
Merge remote-tracking branch 'p2p/discovery/routing/master' into disc…
marten-seemann Jan 9, 2022
d5f4910
rename p2p/discovery/routing package, run go mod tidy
marten-seemann Jan 9, 2022
4269c5f
stop using go-libp2p-discovery
marten-seemann Jan 9, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/libp2p/go-libp2p-core/transport"
"github.com/libp2p/go-libp2p-peerstore/pstoremem"

drouting "github.com/libp2p/go-libp2p/p2p/discovery/routing"
"github.com/libp2p/go-libp2p/p2p/host/autonat"
"github.com/libp2p/go-libp2p/p2p/host/autorelay"
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
Expand All @@ -27,7 +28,6 @@ import (
"github.com/libp2p/go-libp2p/p2p/protocol/holepunch"

blankhost "github.com/libp2p/go-libp2p-blankhost"
discovery "github.com/libp2p/go-libp2p-discovery"
swarm "github.com/libp2p/go-libp2p-swarm"
tptu "github.com/libp2p/go-libp2p-transport-upgrader"

Expand Down Expand Up @@ -285,7 +285,7 @@ func (cfg *Config) NewNode() (host.Host, error) {
h.Close()
return nil, fmt.Errorf("cannot enable autorelay; no suitable routing for discovery")
}
opts = append(opts, autorelay.WithDiscoverer(discovery.NewRoutingDiscovery(crouter)))
opts = append(opts, autorelay.WithDiscoverer(drouting.NewRoutingDiscovery(crouter)))
}
ar, err = autorelay.NewAutoRelay(h, router, opts...)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/gogo/protobuf v1.3.2
github.com/golang/mock v1.6.0
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/golang-lru v0.5.4
github.com/huin/goupnp v1.0.2 // indirect
github.com/ipfs/go-cid v0.0.7
github.com/ipfs/go-datastore v0.5.0
Expand All @@ -22,7 +23,6 @@ require (
github.com/libp2p/go-libp2p-blankhost v0.3.0
github.com/libp2p/go-libp2p-circuit v0.4.1-0.20220104091935-28fb8d25f785
github.com/libp2p/go-libp2p-core v0.13.1-0.20220104083644-a3dd401efe36
github.com/libp2p/go-libp2p-discovery v0.6.0
github.com/libp2p/go-libp2p-mplex v0.4.1
github.com/libp2p/go-libp2p-nat v0.1.0
github.com/libp2p/go-libp2p-netutil v0.1.0
Expand All @@ -42,6 +42,7 @@ require (
github.com/libp2p/zeroconf/v2 v2.1.1
github.com/multiformats/go-multiaddr v0.5.0
github.com/multiformats/go-multiaddr-dns v0.3.1
github.com/multiformats/go-multihash v0.0.15
github.com/multiformats/go-multistream v0.2.2
github.com/multiformats/go-varint v0.0.6
github.com/prometheus/common v0.30.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,6 @@ github.com/libp2p/go-libp2p-core v0.11.0/go.mod h1:ECdxehoYosLYHgDDFa2N4yE8Y7aQR
github.com/libp2p/go-libp2p-core v0.12.0/go.mod h1:ECdxehoYosLYHgDDFa2N4yE8Y7aQRAMf0sX9mf2sbGg=
github.com/libp2p/go-libp2p-core v0.13.1-0.20220104083644-a3dd401efe36 h1:b/pMmgc5EV+dqSc+MjkX5xPa1nV6EKiOb0L0XT03Lic=
github.com/libp2p/go-libp2p-core v0.13.1-0.20220104083644-a3dd401efe36/go.mod h1:KlkHsZ0nKerWsXLZJm3LfFQwusI5k3iN4BgtYTE4IYE=
github.com/libp2p/go-libp2p-discovery v0.6.0 h1:1XdPmhMJr8Tmj/yUfkJMIi8mgwWrLUsCB3bMxdT+DSo=
github.com/libp2p/go-libp2p-discovery v0.6.0/go.mod h1:/u1voHt0tKIe5oIA1RHBKQLVCWPna2dXmPNHc2zR9S8=
github.com/libp2p/go-libp2p-mplex v0.4.1 h1:/pyhkP1nLwjG3OM+VuaNJkQT/Pqq73WzB3aDN3Fx1sc=
github.com/libp2p/go-libp2p-mplex v0.4.1/go.mod h1:cmy+3GfqfM1PceHTLL7zQzAAYaryDu6iPSC+CIb094g=
github.com/libp2p/go-libp2p-nat v0.1.0 h1:vigUi2MEN+fwghe5ijpScxtbbDz+L/6y8XwlzYOJgSY=
Expand Down
232 changes: 232 additions & 0 deletions p2p/discovery/backoff/backoff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
package backoff

import (
"math"
"math/rand"
"sync"
"time"

logging "github.com/ipfs/go-log/v2"
)

var log = logging.Logger("discovery-backoff")

type BackoffFactory func() BackoffStrategy

// BackoffStrategy describes how backoff will be implemented. BackoffStratgies are stateful.
type BackoffStrategy interface {
// Delay calculates how long the next backoff duration should be, given the prior calls to Delay
Delay() time.Duration
// Reset clears the internal state of the BackoffStrategy
Reset()
}

// Jitter implementations taken roughly from https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/

// Jitter must return a duration between min and max. Min must be lower than, or equal to, max.
type Jitter func(duration, min, max time.Duration, rng *rand.Rand) time.Duration

// FullJitter returns a random number uniformly chose from the range [min, boundedDur].
// boundedDur is the duration bounded between min and max.
func FullJitter(duration, min, max time.Duration, rng *rand.Rand) time.Duration {
if duration <= min {
return min
}

normalizedDur := boundedDuration(duration, min, max) - min

return boundedDuration(time.Duration(rng.Int63n(int64(normalizedDur)))+min, min, max)
}

// NoJitter returns the duration bounded between min and max
func NoJitter(duration, min, max time.Duration, rng *rand.Rand) time.Duration {
return boundedDuration(duration, min, max)
}

type randomizedBackoff struct {
min time.Duration
max time.Duration
rng *rand.Rand
}

func (b *randomizedBackoff) BoundedDelay(duration time.Duration) time.Duration {
return boundedDuration(duration, b.min, b.max)
}

func boundedDuration(d, min, max time.Duration) time.Duration {
if d < min {
return min
}
if d > max {
return max
}
return d
}

type attemptBackoff struct {
attempt int
jitter Jitter
randomizedBackoff
}

func (b *attemptBackoff) Reset() {
b.attempt = 0
}

// NewFixedBackoff creates a BackoffFactory with a constant backoff duration
func NewFixedBackoff(delay time.Duration) BackoffFactory {
return func() BackoffStrategy {
return &fixedBackoff{delay: delay}
}
}

type fixedBackoff struct {
delay time.Duration
}

func (b *fixedBackoff) Delay() time.Duration {
return b.delay
}

func (b *fixedBackoff) Reset() {}

// NewPolynomialBackoff creates a BackoffFactory with backoff of the form c0*x^0, c1*x^1, ...cn*x^n where x is the attempt number
// jitter is the function for adding randomness around the backoff
// timeUnits are the units of time the polynomial is evaluated in
// polyCoefs is the array of polynomial coefficients from [c0, c1, ... cn]
func NewPolynomialBackoff(min, max time.Duration, jitter Jitter,
timeUnits time.Duration, polyCoefs []float64, rngSrc rand.Source) BackoffFactory {
rng := rand.New(&lockedSource{src: rngSrc})
return func() BackoffStrategy {
return &polynomialBackoff{
attemptBackoff: attemptBackoff{
randomizedBackoff: randomizedBackoff{
min: min,
max: max,
rng: rng,
},
jitter: jitter,
},
timeUnits: timeUnits,
poly: polyCoefs,
}
}
}

type polynomialBackoff struct {
attemptBackoff
timeUnits time.Duration
poly []float64
}

func (b *polynomialBackoff) Delay() time.Duration {
var polySum float64
switch len(b.poly) {
case 0:
return 0
case 1:
polySum = b.poly[0]
default:
polySum = b.poly[0]
exp := 1
attempt := b.attempt
b.attempt++

for _, c := range b.poly[1:] {
exp *= attempt
polySum += float64(exp) * c
}
}
return b.jitter(time.Duration(float64(b.timeUnits)*polySum), b.min, b.max, b.rng)
}

// NewExponentialBackoff creates a BackoffFactory with backoff of the form base^x + offset where x is the attempt number
// jitter is the function for adding randomness around the backoff
// timeUnits are the units of time the base^x is evaluated in
func NewExponentialBackoff(min, max time.Duration, jitter Jitter,
timeUnits time.Duration, base float64, offset time.Duration, rngSrc rand.Source) BackoffFactory {
rng := rand.New(&lockedSource{src: rngSrc})
return func() BackoffStrategy {
return &exponentialBackoff{
attemptBackoff: attemptBackoff{
randomizedBackoff: randomizedBackoff{
min: min,
max: max,
rng: rng,
},
jitter: jitter,
},
timeUnits: timeUnits,
base: base,
offset: offset,
}
}
}

type exponentialBackoff struct {
attemptBackoff
timeUnits time.Duration
base float64
offset time.Duration
}

func (b *exponentialBackoff) Delay() time.Duration {
attempt := b.attempt
b.attempt++
return b.jitter(
time.Duration(math.Pow(b.base, float64(attempt))*float64(b.timeUnits))+b.offset, b.min, b.max, b.rng)
}

// NewExponentialDecorrelatedJitter creates a BackoffFactory with backoff of the roughly of the form base^x where x is the attempt number.
// Delays start at the minimum duration and after each attempt delay = rand(min, delay * base), bounded by the max
// See https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/ for more information
func NewExponentialDecorrelatedJitter(min, max time.Duration, base float64, rngSrc rand.Source) BackoffFactory {
rng := rand.New(&lockedSource{src: rngSrc})
return func() BackoffStrategy {
return &exponentialDecorrelatedJitter{
randomizedBackoff: randomizedBackoff{
min: min,
max: max,
rng: rng,
},
base: base,
}
}
}

type exponentialDecorrelatedJitter struct {
randomizedBackoff
base float64
lastDelay time.Duration
}

func (b *exponentialDecorrelatedJitter) Delay() time.Duration {
if b.lastDelay < b.min {
b.lastDelay = b.min
return b.lastDelay
}

nextMax := int64(float64(b.lastDelay) * b.base)
b.lastDelay = boundedDuration(time.Duration(b.rng.Int63n(nextMax-int64(b.min)))+b.min, b.min, b.max)
return b.lastDelay
}

func (b *exponentialDecorrelatedJitter) Reset() { b.lastDelay = 0 }

type lockedSource struct {
lk sync.Mutex
src rand.Source
}

func (r *lockedSource) Int63() (n int64) {
r.lk.Lock()
n = r.src.Int63()
r.lk.Unlock()
return
}

func (r *lockedSource) Seed(seed int64) {
r.lk.Lock()
r.src.Seed(seed)
r.lk.Unlock()
}
Loading