-
Notifications
You must be signed in to change notification settings - Fork 123
Add exponential backoff #134
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
Merged
f2prateek
merged 3 commits into
segmentio:master
from
rohitpaulk:add-exponential-backoff
Dec 5, 2017
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,49 @@ | ||
| require 'segment/analytics/defaults' | ||
|
|
||
| module Segment | ||
| class Analytics | ||
| class BackoffPolicy | ||
| include Segment::Analytics::Defaults::BackoffPolicy | ||
|
|
||
| # @param [Hash] opts | ||
| # @option opts [Numeric] :min_timeout_ms The minimum backoff timeout | ||
| # @option opts [Numeric] :max_timeout_ms The maximum backoff timeout | ||
| # @option opts [Numeric] :multiplier The value to multiply the current | ||
| # interval with for each retry attempt | ||
| # @option opts [Numeric] :randomization_factor The randomization factor | ||
| # to use to create a range around the retry interval | ||
| def initialize(opts = {}) | ||
| @min_timeout_ms = opts[:min_timeout_ms] || MIN_TIMEOUT_MS | ||
| @max_timeout_ms = opts[:max_timeout_ms] || MAX_TIMEOUT_MS | ||
| @multiplier = opts[:multiplier] || MULTIPLIER | ||
| @randomization_factor = opts[:randomization_factor] || RANDOMIZATION_FACTOR | ||
|
|
||
| @attempts = 0 | ||
| end | ||
|
|
||
| # @return [Numeric] the next backoff interval, in milliseconds. | ||
| def next_interval | ||
| interval = @min_timeout_ms * (@multiplier**@attempts) | ||
| interval = add_jitter(interval, @randomization_factor) | ||
|
|
||
| @attempts += 1 | ||
|
|
||
| [interval, @max_timeout_ms].min | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def add_jitter(base, randomization_factor) | ||
| random_number = rand | ||
| max_deviation = base * randomization_factor | ||
| deviation = random_number * max_deviation | ||
|
|
||
| if random_number < 0.5 | ||
| base - deviation | ||
| else | ||
| base + deviation | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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 hidden or 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,92 @@ | ||
| require 'spec_helper' | ||
|
|
||
| module Segment | ||
| class Analytics | ||
| describe BackoffPolicy do | ||
| describe '#initialize' do | ||
| context 'no options are given' do | ||
| it 'sets default min_timeout_ms' do | ||
| actual = subject.instance_variable_get(:@min_timeout_ms) | ||
| expect(actual).to eq(described_class::MIN_TIMEOUT_MS) | ||
| end | ||
|
|
||
| it 'sets default max_timeout_ms' do | ||
| actual = subject.instance_variable_get(:@max_timeout_ms) | ||
| expect(actual).to eq(described_class::MAX_TIMEOUT_MS) | ||
| end | ||
|
|
||
| it 'sets default multiplier' do | ||
| actual = subject.instance_variable_get(:@multiplier) | ||
| expect(actual).to eq(described_class::MULTIPLIER) | ||
| end | ||
|
|
||
| it 'sets default randomization factor' do | ||
| actual = subject.instance_variable_get(:@randomization_factor) | ||
| expect(actual).to eq(described_class::RANDOMIZATION_FACTOR) | ||
| end | ||
| end | ||
|
|
||
| context 'options are given' do | ||
| let(:min_timeout_ms) { 1234 } | ||
| let(:max_timeout_ms) { 5678 } | ||
| let(:multiplier) { 24 } | ||
| let(:randomization_factor) { 0.4 } | ||
|
|
||
| let(:options) do | ||
| { | ||
| min_timeout_ms: min_timeout_ms, | ||
| max_timeout_ms: max_timeout_ms, | ||
| multiplier: multiplier, | ||
| randomization_factor: randomization_factor | ||
| } | ||
| end | ||
|
|
||
| subject { described_class.new(options) } | ||
|
|
||
| it 'sets passed in min_timeout_ms' do | ||
| actual = subject.instance_variable_get(:@min_timeout_ms) | ||
| expect(actual).to eq(min_timeout_ms) | ||
| end | ||
|
|
||
| it 'sets passed in max_timeout_ms' do | ||
| actual = subject.instance_variable_get(:@max_timeout_ms) | ||
| expect(actual).to eq(max_timeout_ms) | ||
| end | ||
|
|
||
| it 'sets passed in multiplier' do | ||
| actual = subject.instance_variable_get(:@multiplier) | ||
| expect(actual).to eq(multiplier) | ||
| end | ||
|
|
||
| it 'sets passed in randomization_factor' do | ||
| actual = subject.instance_variable_get(:@randomization_factor) | ||
| expect(actual).to eq(randomization_factor) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe '#next_interval' do | ||
| subject { | ||
| described_class.new( | ||
| min_timeout_ms: 1000, | ||
| max_timeout_ms: 10000, | ||
| multiplier: 2, | ||
| randomization_factor: 0.5 | ||
| ) | ||
| } | ||
|
|
||
| it 'returns exponentially increasing durations' do | ||
| expect(subject.next_interval).to be_within(500).of(1000) | ||
| expect(subject.next_interval).to be_within(1000).of(2000) | ||
| expect(subject.next_interval).to be_within(2000).of(4000) | ||
| expect(subject.next_interval).to be_within(4000).of(8000) | ||
| end | ||
|
|
||
| it 'caps maximum duration at max_timeout_secs' do | ||
| 10.times { subject.next_interval } | ||
| expect(subject.next_interval).to eq(10000) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was this previously unused?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. It was being assigned to a variable, but the actual headers used were hardcoded.
(The diff for this individual commit includes all the relevant files).