Skip to content

Commit

Permalink
fix infinite token retries and datastore health check memoization (#3844
Browse files Browse the repository at this point in the history
)
  • Loading branch information
daniel-wer authored Mar 4, 2019
1 parent ec7bcf7 commit be52006
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
7 changes: 5 additions & 2 deletions frontend/javascripts/admin/admin_rest_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function getSharingToken(): ?string {
}

let tokenPromise;
export function doWithToken<T>(fn: (token: string) => Promise<T>): Promise<*> {
export function doWithToken<T>(fn: (token: string) => Promise<T>, tries: number = 1): Promise<*> {
const sharingToken = getSharingToken();
if (sharingToken != null) {
return fn(sharingToken);
Expand All @@ -108,7 +108,10 @@ export function doWithToken<T>(fn: (token: string) => Promise<T>): Promise<*> {
if (error.status === 403) {
console.warn("Token expired. Requesting new token...");
tokenPromise = requestUserToken();
return doWithToken(fn);
// If three new tokens did not fix the 403, abort, otherwise we'll get into an endless loop here
if (tries < 3) {
return doWithToken(fn, tries + 1);
}
}
throw error;
});
Expand Down
9 changes: 6 additions & 3 deletions frontend/javascripts/admin/datastore_health_check.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ const memoizedThrottle = (func, wait = 0, options = {}): Function => {
};
};

export const pingDataStoreIfAppropriate = memoizedThrottle(async (requestedUrl: string): Promise<
*,
> => {
// Do not call this function directly, but call pingMentionedDataStores instead
// which will take care of extracting the hostnames.
// Otherwise, the memoization will not work correctly if the path and query-string are part of the URL
const pingDataStoreIfAppropriate = memoizedThrottle(async (requestedUrl: string): Promise<*> => {
const [datastores, tracingstore, isInMaintenance] = await Promise.all([
RestAPI.getDataStoresCached(),
RestAPI.getTracingStoreCached(),
Expand Down Expand Up @@ -93,3 +94,5 @@ const extractUrls = (str: string): Array<string> => {
export const pingMentionedDataStores = (str: string): void => {
extractUrls(str).map(pingDataStoreIfAppropriate);
};

export default {};
4 changes: 2 additions & 2 deletions frontend/javascripts/libs/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import _ from "lodash";
import urljoin from "url-join";

import { createWorker } from "oxalis/workers/comlink_wrapper";
import { pingDataStoreIfAppropriate, pingMentionedDataStores } from "admin/datastore_health_check";
import { pingMentionedDataStores } from "admin/datastore_health_check";
import CompressWorker from "oxalis/workers/compress.worker";
import FetchBufferWithHeadersWorker from "oxalis/workers/fetch_buffer_with_headers.worker";
import FetchBufferWorker from "oxalis/workers/fetch_buffer.worker";
Expand Down Expand Up @@ -279,7 +279,7 @@ class Request {
): Promise<void> => {
if (doInvestigate) {
// Check whether this request failed due to a problematic datastore
pingDataStoreIfAppropriate(requestedUrl);
pingMentionedDataStores(requestedUrl);
if (error instanceof Response) {
return error.text().then(
text => {
Expand Down

0 comments on commit be52006

Please sign in to comment.