alex-okrushko/backoff-rxjs

Retry with different server

david-hollifield opened this issue · 0 comments

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 {
        ...
      }