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

Retry with different server #52

Open
david-hollifield opened this issue Aug 16, 2022 · 0 comments
Open

Retry with different server #52

david-hollifield opened this issue Aug 16, 2022 · 0 comments

Comments

@david-hollifield
Copy link

I know I'm a little late to the game here, but would it be possible to retry a request using a different server for each retry?

For example, I have a bunch of load-balanced API servers available to handle requests. If I get an error, like a 503, I'd like to retry the same request, but to a different server (maybe adding a retry=X query argument). How would I be able to do something like that using this operator?

I have a non-rxjs implementation in an AngularJs app that I'm converting to Angular and would like to provide the same functionality using an HttpInterceptor and your backoff operator. Here's the current (stripped-down) implementation used within an AngularJs error interceptor:

      // should we retry?
      if (_.indexOf(this.retryErrors, response.status) != -1) {
        // ruh-roh!!!
        let attempt = 0;

        new Backoff({
          attempts: 1,
          maxAttempts: retryServers.length
        }).run((retry) => {
          attempt++;

          // retry the last request (with an updated server randomly selected from the available servers list)
          let url = new URL(response.config.url, {});
          let serverIndex = Math.floor(Math.random() * retryServers.length);
          let retryServer = retryServers[serverIndex];
          url.set("host", retryServer);
          response.config.url = url.toString();

          // add a retry parameter that could be used by api if needed
          response.config.params = Object.assign({}, response.config.params, { retry: attempt })

          this.$http(response.config).then(retrySuccess).catch(() => {
            if (!retry()) {
              retryError();
            }
          });
        });

        return false;
      } else {
        ...
      }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant