-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cherrypick: changes for 4.13.5 (#3711)
* fix: named pipe issues (#3627) * fix: named pipe issues - Remove unnecessary autoReconnect logic - Simplify connection management - Properly support client reconnection to incoming/outgoing servers * fix: PR comments, failing tests * fix: "lock" sockets to mitigate split brain * test: no split brain * fix: reject listening only if server is listening * fix: naming for clarity * fix: clarify onListen doc string * feat: retry helper, use for named pipes * fix: handle retry edge cases * bump: adal-node to 0.2.2 (#3705) Fixes #3704 * fix: ignore node_modules declarative assets (#3709) See microsoft/BotFramework-Composer#7916 for details.
- Loading branch information
1 parent
d2b0197
commit 0ee95d2
Showing
16 changed files
with
329 additions
and
229 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
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,41 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
/** | ||
* Retry a given promise with gradually increasing delay. | ||
* | ||
* @param promise a function that returns a promise to retry | ||
* @param maxRetries the maximum number of times to retry | ||
* @param initialDelay the initial value to delay before retrying (in milliseconds) | ||
* @returns a promise resolving to the result of the promise from the promise generating function, or undefined | ||
*/ | ||
export async function retry<T>( | ||
promise: (n: number) => Promise<T>, | ||
maxRetries: number, | ||
initialDelay = 500 | ||
): Promise<T | undefined> { | ||
let delay = initialDelay, | ||
n = 1, | ||
maybeError: Error | undefined; | ||
|
||
// Take care of negative or zero | ||
maxRetries = Math.max(maxRetries, 1); | ||
|
||
while (n <= maxRetries) { | ||
try { | ||
// Note: return await intentional so we can catch errors | ||
return await promise(n); | ||
} catch (err) { | ||
maybeError = err; | ||
|
||
await new Promise((resolve) => setTimeout(resolve, delay)); | ||
|
||
delay *= n; | ||
n++; | ||
} | ||
} | ||
|
||
if (maybeError) { | ||
throw maybeError; | ||
} | ||
} |
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,41 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
const assert = require('assert'); | ||
const sinon = require('sinon'); | ||
const { retry } = require('../'); | ||
|
||
describe('retry', function () { | ||
it('succeeds on first try', async function () { | ||
const fake = sinon.fake((n) => Promise.resolve(n)); | ||
assert.strictEqual(await retry(fake, 3, 0), 1); | ||
assert.strictEqual(fake.callCount, 1); | ||
}); | ||
|
||
it('handles zero retries', async function () { | ||
const fake = sinon.fake((n) => Promise.resolve(n)); | ||
assert.strictEqual(await retry(fake, 0, 0), 1); | ||
assert.strictEqual(fake.callCount, 1); | ||
}); | ||
|
||
it('handles negative retries', async function () { | ||
const fake = sinon.fake((n) => Promise.resolve(n)); | ||
assert.strictEqual(await retry(fake, -10, 0), 1); | ||
assert.strictEqual(fake.callCount, 1); | ||
}); | ||
|
||
it('succeeds eventually', async function () { | ||
const fake = sinon.fake((n) => (n < 3 ? Promise.reject() : Promise.resolve(10))); | ||
assert.strictEqual(await retry(fake, 3, 0), 10); | ||
assert.strictEqual(fake.callCount, 3); | ||
}); | ||
|
||
it('yields error if never succeeds', async function () { | ||
const fake = sinon.fake(() => Promise.reject(new Error('oh no'))); | ||
await assert.rejects(retry(fake, 3, 0), { | ||
name: 'Error', | ||
message: 'oh no', | ||
}); | ||
assert.strictEqual(fake.callCount, 3); | ||
}); | ||
}); |
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
Oops, something went wrong.