Skip to content

Commit

Permalink
Merge pull request #322 from mittwald/bugfix/fix-eventual-consistency…
Browse files Browse the repository at this point in the history
…-again

Fix bugs introduced by eventual-consistency handling
  • Loading branch information
martin-helmich authored Mar 13, 2024
2 parents b8354e4 + 7687b5a commit 2449ec1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
19 changes: 15 additions & 4 deletions src/lib/api_consistency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ const d = debug("mw:api-consistency");

export function configureConsistencyHandling(axios: AxiosInstance) {
let lastEventId: string | undefined = undefined;
let mutatedPaths: string[] | undefined = undefined;

axios.interceptors.request.use((config) => {
if (lastEventId !== undefined) {
if (
lastEventId !== undefined &&
mutatedPaths?.some((path) => config.url?.startsWith(path))
) {
d("setting if-event-reached to %o", lastEventId);
config.headers["if-event-reached"] = lastEventId;
}
Expand All @@ -22,11 +26,18 @@ export function configureConsistencyHandling(axios: AxiosInstance) {
const headers = response.headers as AxiosHeaders;

if (headers.has("etag") && isMutatingRequest) {
lastEventId = headers.get("etag") as string;

const mutatedPath = response.config?.url;
if (mutatedPath !== undefined) {
mutatedPaths = [mutatedPath];
}

d(
"setting last event id to %o after mutating request",
headers.get("etag"),
"setting last event id to %o after mutating request for path %o",
headers["etag"],
mutatedPath,
);
lastEventId = headers.get("etag") as string;
}
return response;
});
Expand Down
10 changes: 9 additions & 1 deletion src/lib/api_retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export function configureAxiosRetry(axios: AxiosInstance) {
axiosRetry(axios, {
retries: 10,
retryDelay: axiosRetry.exponentialDelay,
onRetry(count, error) {
onRetry(count, error, config) {
if (error.response?.status === 412 && config.headers !== undefined) {
delete config.headers["if-event-reached"];
}
d("retrying request after %d attempts; error: %o", count, error.message);
},
retryCondition(error) {
Expand All @@ -37,8 +40,13 @@ export function configureAxiosRetry(axios: AxiosInstance) {
}

const isSafeRequest = error.config?.method?.toLowerCase() === "get";
const isPreconditionFailed = error.response?.status === 412;
const isAccessDenied = error.response?.status === 403;

if (isPreconditionFailed) {
return true;
}

return isSafeRequest && isAccessDenied && shouldRetryAccessDenied;
},
});
Expand Down

0 comments on commit 2449ec1

Please sign in to comment.