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

Timeouts and retries #180

Open
jdconley opened this issue Sep 28, 2023 · 1 comment
Open

Timeouts and retries #180

jdconley opened this issue Sep 28, 2023 · 1 comment

Comments

@jdconley
Copy link

I've run into issues recently where Polygon is sometimes taking minutes to timeout on requests so was just looking into timeouts and retries. It'd be quite useful if there was a builtin way to specify the request timeout and retry semantics.

If you used something like fetch-retry under the hood and let us supply options that'd be helpful.

And likewise a timeout option that used an AbortController signal with fetch would be great.

@jdconley
Copy link
Author

FWIW I went with something like this as a wrapper, using retry-as-promised.

  private makeRequest<TResult>(
    action: (client: IRestClient) => Promise<TResult>,
  ): Promise<TResult> {
    return retry(
      async () => {
        const timeoutController = new AbortController();
        const client = restClient(
          this.polygonApiKey,
          'https://api.polygon.io',
          {
            signal: timeoutController.signal,
          },
        );
        const timeout = setTimeout(
          () => timeoutController.abort('Timeout calling Polygon API'),
          5000,
        );

        try {
          return await action(client);
        } finally {
          clearTimeout(timeout);
        }
      },
      {
        max: 5,
        name: 'Polygon API',
      },
    );
  }

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