-
Notifications
You must be signed in to change notification settings - Fork 2
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: Improve Lock Manager HTTP layer #15
Merged
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 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,91 @@ | ||
import fetch, { RequestInit, Response } from "node-fetch" | ||
|
||
import { logger } from "../logger.js" | ||
|
||
export type FetchParams = { | ||
endpoint: string, | ||
options: RequestInit | ||
} | ||
|
||
export type RetryOptions = { | ||
retries: number, | ||
retryDelay: number, | ||
fetchParams: FetchParams | ||
} | ||
|
||
export class HttpErrorOptions { | ||
cause?: Error | ||
responseData?: string | unknown | ||
} | ||
|
||
export class HttpError extends Error { | ||
readonly type: string = "HttpError" | ||
responseData?: string | unknown | ||
constructor( | ||
readonly method: string, | ||
readonly endpoint: string, | ||
readonly status: number, | ||
readonly text: string, | ||
private options: HttpErrorOptions | ||
){ | ||
super(text, { cause: options.cause }) | ||
this.responseData = options.responseData | ||
} | ||
|
||
toString(): string { | ||
return JSON.stringify({ method: this.method, endpoint: this.endpoint, status: this.status, text: this.text, responseData: this.responseData, cause: this.cause }) | ||
} | ||
} | ||
|
||
export const invoke = async (options: RetryOptions, apiBody: Object) => { | ||
let attempts = 1 | ||
while (options.retries > 0) { | ||
logger.info("http-client.invoke(): attempt %s of %s invoking %s", attempts, options.retries, JSON.stringify(options.fetchParams)) | ||
try { | ||
return await invokeApi(options.fetchParams, apiBody) | ||
} catch (error: unknown) { | ||
logger.warn("http-client.invoke(): attempt %s of %s invoking %s. Error: %s", attempts, options.retries, JSON.stringify(options.fetchParams), error) | ||
if (attempts < options.retries) { | ||
attempts++ | ||
await sleep(options.retryDelay) | ||
continue | ||
} | ||
|
||
logger.warn("http-client.invoke(): failed to invoke %s. No more attempts left, giving up", JSON.stringify(options.fetchParams)) | ||
throw new Error(`Failed to fetch ${ options.fetchParams.endpoint } after ${ attempts } attempts`, { cause: error }) | ||
} | ||
} | ||
} | ||
|
||
const invokeApi = async (params: FetchParams, apiBody: Object): Promise<unknown | string> => { | ||
const resp = await fetch(params.endpoint, { ...params.options, body: JSON.stringify(apiBody) }) | ||
const readPayload = async (response: Response): Promise<unknown | string> => { | ||
const ctHeader = "content-type" | ||
const hdrs = response.headers | ||
if (hdrs.has(ctHeader) && `${ hdrs.get(ctHeader) }`.toLowerCase().startsWith("application/json")) { | ||
try { | ||
return await response.json() | ||
} catch (e) { | ||
logger.warn("Unable to parse JSON when handling response from '%s', handling will fallback to reading the payload as text. Parsing error: %s", params.endpoint, e) | ||
return await response.text() | ||
} | ||
} else { | ||
return await response.text() | ||
} | ||
} | ||
|
||
if (resp.ok) { | ||
logger.info("invokeApi(): resp was ok, reading payload") | ||
return await readPayload(resp) | ||
} else { | ||
logger.info("invokeApi(): resp was not ok, raising error") | ||
throw new HttpError( | ||
params.options.method, | ||
params.endpoint, | ||
resp.status, | ||
resp.statusText, | ||
{ responseData: await readPayload(resp) }) | ||
} | ||
} | ||
|
||
export const sleep = (time: number) => new Promise(resolve => setTimeout(resolve, time)) |
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
Oops, something went wrong.
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.
Because we have this sleep in the
invoke
method, maybe we should remove this sleep while trying to acquire lock, else we are going to sleep longer duration than necessary.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, makes sense to remove the second one.