-
-
Notifications
You must be signed in to change notification settings - Fork 657
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
makeBackoffMachine: Improve backoff sleep logic by making it local. #3841
Merged
rk-for-zulip
merged 6 commits into
zulip:master
from
chrisbobbe:improve-progressive-timeout
Apr 10, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c36e2bb
eslint: Disable no-await-in-loop.
577ac4c
backoff machine: Add, for replacement of progressiveTimeout.
a301dda
backoff machine: Add jitter.
eb20721
tryUntilSuccessful [nfc]: Make iterative, not recursive.
9c1131f
backoff machine: Replace all uses of progressiveTimeout.
a3a442d
progressiveTimeout: Delete; was entirely replaced by new BackoffMachi…
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 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,60 @@ | ||
/* @flow strict-local */ | ||
import { BackoffMachine } from '../async'; | ||
import { Lolex } from '../../__tests__/aux/lolex'; | ||
|
||
// Since BackoffMachine is in async.js, these tests *should* be in | ||
// async-test.js. But doing that introduces some interference between these | ||
// tests and the other Lolex-based tests, since Jest is running both of them in | ||
// the same environment in parallel. This may be resolved out of the box in Jest | ||
// 26, and it might even be safe in Jest 25.1.0 with a custom environment | ||
// (https://github.com/facebook/jest/pull/8897). But as of 2020-03, putting them | ||
// in a separate file is our workaround. | ||
|
||
describe('BackoffMachine', () => { | ||
const lolex: Lolex = new Lolex(); | ||
|
||
afterEach(() => { | ||
lolex.clearAllTimers(); | ||
}); | ||
|
||
afterAll(() => { | ||
lolex.dispose(); | ||
}); | ||
|
||
const measureWait = async (promise: Promise<void>) => { | ||
const start = Date.now(); | ||
lolex.runOnlyPendingTimers(); | ||
await promise; | ||
return Date.now() - start; | ||
}; | ||
|
||
test('timeouts are random from zero to 100ms, 200ms, 400ms, 800ms...', async () => { | ||
// This is a randomized test. NUM_TRIALS is chosen so that the failure | ||
// probability < 1e-9. There are 2 * 11 assertions, and each one has a | ||
// failure probability < 1e-12; see below. | ||
const NUM_TRIALS = 100; | ||
const expectedMaxDurations = [100, 200, 400, 800, 1600, 3200, 6400, 10000, 10000, 10000, 10000]; | ||
|
||
const trialResults: Array<number[]> = []; | ||
|
||
for (let i = 0; i < NUM_TRIALS; i++) { | ||
const resultsForThisTrial = []; | ||
const backoffMachine = new BackoffMachine(); | ||
for (let j = 0; j < expectedMaxDurations.length; j++) { | ||
const duration = await measureWait(backoffMachine.wait()); | ||
resultsForThisTrial.push(duration); | ||
} | ||
trialResults.push(resultsForThisTrial); | ||
} | ||
|
||
expectedMaxDurations.forEach((expectedMax, i) => { | ||
const maxFromAllTrials = Math.max(...trialResults.map(r => r[i])); | ||
const minFromAllTrials = Math.min(...trialResults.map(r => r[i])); | ||
|
||
// Each of these assertions has a failure probability of: | ||
// 0.75 ** NUM_TRIALS = 0.75 ** 100 < 1e-12 | ||
expect(minFromAllTrials).toBeLessThan(expectedMax * 0.25); | ||
expect(maxFromAllTrials).toBeGreaterThan(expectedMax * 0.75); | ||
}); | ||
}); | ||
}); |
This file was deleted.
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 was deleted.
Oops, something went wrong.
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.
I'm not sure how turning this off will prevent – or even make less likely – the failure state you describe in the commit message. Our existing improper uses of
array.forEach
seem more likely to have been induced by AirBnB's anti-loop position, which we've since thoughtfully revoked. (See the bottom of this file.)Which is not to say that I'm against removing this lint! I suspect we almost invariably do want sequential
await
whenever we have multiple Awaitables, simply because we rarely – if ever – want to fire off a hundred network requests at once.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.
Well, with this rule, if you want to
await
sequentially in a loop, you have to choose between aneslint-disable
and the failure of using a forEach. I think it's reasonable to assume that someone will choose wrong because they trust our choice of rules and they're not looking closely at the implementation of forEach. I'll see if I can make this clearer in the commit message.That may be true, but it seems anecdotal; you're talking about the outbox issue, right, which I didn't know about when I committed this; in fact, my reasoning doesn't depend on any improper uses I saw. It sounds like you're not opposed to repealing
no-await-in-loop
, anyway, but I hope the reason I'd like to do so is clear.