Skip to content
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

Barrier: infinite loop on failure if backend still responding with errors #458

Open
optislav opened this issue Mar 21, 2024 · 0 comments
Open
Labels
scope:core type:enhancement New feature or request
Milestone

Comments

@optislav
Copy link

optislav commented Mar 21, 2024

I have a barrier like this:

export const authBarrier = createBarrier({
  activateOn: {
    failure: isHttpErrorCode(401)
  },
  perform: [getTokenMutation],
})

But I had a bug in a query header where I mistyped AuthoriZation with AuthoriSation:

createJsonQuery({
  request: {
    method: 'GET',
    url: API_URL + '/presets',
    headers: {
      source: $authToken,
      fn: (_, token) => ({
        Authorisation: `Bearer ${token}`
      })
    }
  },
  ...
})

So the backend returned 401 even after updating the token, which led to an infinite loop, freeze, and a lot of requests.

I've written an ugly workaround for this problem:

export const createCircuitBreaker = (maxRetries: number = 3) => {
  let retries = 0; // Circuit breaker. Prevent infinite loop
  const circuitBreaker = () => {
    const pass = ++retries < maxRetries;
    if (!pass) {
      setTimeout(() => { retries = 0; }, 0);
    }
    return pass;
  };
  return circuitBreaker;
};

export const withCircuitBreaker = (condition: (...args: any[]) => boolean) => {
  const circuitBreaker = createCircuitBreaker();
  return (e: { params: unknown, error: unknown }) => condition(e) && circuitBreaker();
};

export const authBarrier = createBarrier({
  activateOn: {
    failure: withCircuitBreaker(isHttpErrorCode(401))
  },
  perform: [getTokenMutation],
});

But I think something like this should be a part of the barrier config.

@igorkamyshev igorkamyshev added type:enhancement New feature or request scope:core labels Apr 2, 2024
@igorkamyshev igorkamyshev added this to the v1.0 milestone Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope:core type:enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants