Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(network): Add backoff delay logic
- Loading branch information
Showing
14 changed files
with
177 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package host | ||
|
||
import ( | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
const ( | ||
Factor = 1.6 | ||
|
||
// Randomize backoff delays so that if requests start at | ||
// the same time, they won't operate in lockstep. | ||
Jitter = 0.2 | ||
) | ||
|
||
var rsource rand.Source | ||
|
||
func init() { | ||
rsource = rand.NewSource(time.Now().UnixNano()) | ||
} | ||
|
||
type BackoffDelay struct { | ||
rand *rand.Rand | ||
baseDelay time.Duration | ||
maxDelay time.Duration | ||
} | ||
|
||
func NewBackoffDelay(baseDelay, maxDelay time.Duration) *BackoffDelay { | ||
return &BackoffDelay{ | ||
rand: rand.New(rsource), | ||
baseDelay: baseDelay, | ||
maxDelay: maxDelay, | ||
} | ||
} | ||
|
||
func (b *BackoffDelay) Backoff(retries int) time.Duration { | ||
if retries == 0 { | ||
return b.baseDelay | ||
} | ||
|
||
backoff, max := float64(b.baseDelay), float64(b.maxDelay) | ||
for backoff < max && retries > 0 { | ||
backoff *= Factor | ||
retries-- | ||
} | ||
|
||
backoff *= 1 + Jitter*(b.rand.Float64()*2-1) | ||
if backoff < 0 { | ||
return 0 | ||
} | ||
|
||
return time.Duration(backoff) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<<<<<<< HEAD | ||
// this file was generated by protoc-gen-gotemplate | ||
|
||
package metric | ||
||||||| merged common ancestors | ||
======= | ||
// this file was generated by protoc-gen-gotemplate | ||
|
||
<<<<<<< HEAD:core/entity/kind.validate.gen.go | ||
package entity | ||
||||||| merged common ancestors:core/network/p2p/protocol/provider/pubsub/pubsub.validate.gen.go | ||
package pubsub | ||
======= | ||
package metric | ||
>>>>>>> refactor(network): new archi with libp2p:core/network/metric/peer.validate.gen.go | ||
>>>>>>> refactor(network): new archi with libp2p |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<<<<<<< HEAD | ||
// this file was generated by protoc-gen-gotemplate | ||
|
||
package metric | ||
||||||| merged common ancestors | ||
======= | ||
// this file was generated by protoc-gen-gotemplate | ||
|
||
<<<<<<< HEAD:core/entity/kind.validate.gen.go | ||
package entity | ||
||||||| merged common ancestors:core/network/p2p/protocol/provider/pubsub/pubsub.validate.gen.go | ||
package pubsub | ||
======= | ||
package metric | ||
>>>>>>> feat(p2p): Custom host with the capability to choose a specific conn on NewStream:core/network/metric/peer.validate.gen.go | ||
>>>>>>> feat(p2p): Custom host with the capability to choose a specific conn on NewStream |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<<<<<<< HEAD | ||
// this file was generated by protoc-gen-gotemplate | ||
|
||
package metric | ||
||||||| merged common ancestors | ||
======= | ||
// this file was generated by protoc-gen-gotemplate | ||
|
||
<<<<<<< HEAD:core/entity/kind.validate.gen.go | ||
package entity | ||
||||||| merged common ancestors:core/network/p2p/protocol/provider/pubsub/pubsub.validate.gen.go | ||
package pubsub | ||
======= | ||
package metric | ||
>>>>>>> refactor(network): new archi with libp2p:core/network/metric/peer.validate.gen.go | ||
>>>>>>> refactor(network): new archi with libp2p |
Oops, something went wrong.