-
Notifications
You must be signed in to change notification settings - Fork 342
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
Fix/multi endpint delay #2582
Merged
Merged
Fix/multi endpint delay #2582
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fb7248e
fix multi entpoint deploy issues
yoozo 3377d86
unit test
yoozo 06eeb4c
Merge branch 'main' into fix/multi-endpint-delay
yoozo 915e8a4
backoffDelay rule
yoozo 8c2e54f
remove rateLimitDelay
yoozo 02ec4d3
Revert "backoffDelay rule"
yoozo 89cd780
Revert "remove rateLimitDelay"
yoozo 237b754
rateLimited
yoozo 13767b1
fix test
yoozo 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
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 |
---|---|---|
|
@@ -10,10 +10,10 @@ import {getLogger} from '../logger'; | |
import {exitWithError} from '../process'; | ||
import {errorTypeToScoreAdjustment} from './connectionPool.service'; | ||
|
||
const RETRY_DELAY = 60 * 1000; | ||
const MAX_FAILURES = 5; | ||
const RESPONSE_TIME_WEIGHT = 0.7; | ||
const FAILURE_WEIGHT = 0.3; | ||
const RATE_LIMIT_DELAY = 20 * 1000; | ||
|
||
export interface ConnectionPoolItem<T> { | ||
endpoint: string; | ||
|
@@ -191,7 +191,7 @@ export class ConnectionPoolStateManager<T extends IApiConnectionSpecific<any, an | |
} | ||
|
||
//eslint-disable-next-line @typescript-eslint/require-await | ||
async setTimeout(endpoint: string, delay: number): Promise<void> { | ||
async setRecoverTimeout(endpoint: string, delay: number): Promise<void> { | ||
// Make sure there is no existing timeout | ||
await this.clearTimeout(endpoint); | ||
|
||
|
@@ -247,35 +247,49 @@ export class ConnectionPoolStateManager<T extends IApiConnectionSpecific<any, an | |
switch (errorType) { | ||
case ApiErrorType.Connection: { | ||
if (this.pool[endpoint].connected) { | ||
//handleApiDisconnects was already called if this is false | ||
//this.handleApiDisconnects(endpoint); | ||
// The connected status does not provide service. handleApiDisconnects() will be called to handle this. | ||
this.pool[endpoint].connected = false; | ||
} | ||
return; | ||
} | ||
case ApiErrorType.Timeout: | ||
case ApiErrorType.RateLimit: { | ||
// The “rateLimited” status will be selected when no endpoints are available, so we should avoid setting a large delay. | ||
this.pool[endpoint].rateLimited = true; | ||
break; | ||
} | ||
case ApiErrorType.Default: { | ||
const nextDelay = RETRY_DELAY * Math.pow(2, this.pool[endpoint].failureCount - 1); // Exponential backoff using failure count // Start with RETRY_DELAY and double on each failure | ||
this.pool[endpoint].backoffDelay = nextDelay; | ||
|
||
if (ApiErrorType.Timeout || ApiErrorType.RateLimit) { | ||
this.pool[endpoint].rateLimited = true; | ||
} else { | ||
this.pool[endpoint].failed = true; | ||
} | ||
|
||
await this.setTimeout(endpoint, nextDelay); | ||
|
||
logger.warn( | ||
`Endpoint ${this.pool[endpoint].endpoint} experienced an error (${errorType}). Suspending for ${ | ||
nextDelay / 1000 | ||
}s.` | ||
); | ||
return; | ||
// The “failed” status does not provide service. | ||
this.pool[endpoint].failed = true; | ||
break; | ||
} | ||
default: { | ||
throw new Error(`Unknown error type ${errorType}`); | ||
} | ||
} | ||
|
||
const nextDelay = this.calculateNextDelay(this.pool[endpoint]); | ||
this.pool[endpoint].backoffDelay = nextDelay; | ||
await this.setRecoverTimeout(endpoint, nextDelay); | ||
|
||
logger.warn( | ||
`Endpoint ${this.pool[endpoint].endpoint} experienced an error (${errorType}). Suspending for ${ | ||
nextDelay / 1000 | ||
}s.` | ||
); | ||
yoozo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private calculateNextDelay(poolItem: ConnectionPoolItem<T>): number { | ||
const delayRule = [10, 20, 40, 80, 160, 320]; | ||
let delayTime = 0; | ||
if (poolItem.failureCount < 1) { | ||
delayTime = 0; | ||
} else if (poolItem.failureCount >= 1 && poolItem.failureCount <= delayRule.length) { | ||
delayTime = delayRule[poolItem.failureCount - 1]; | ||
} else { | ||
delayTime = delayRule[delayRule.length - 1]; | ||
} | ||
return delayTime * 1000; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can use the old logic, Just change RETRY_DELAY to 10*1000 |
||
} | ||
|
||
private calculatePerformanceScore(responseTime: number, failureCount: number): number { | ||
|
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.
This is still necessary or there will be no throttling