@metreeca/http - v0.1.1
    Preparing search index...

    Function throttle

    • Creates a middleware pacing exchanges and retrying transient failures.

      Decorates a Fetch implementation with one holding each exchange until an adaptive throttle grants it, so that a client keeps to the rate its target is willing to serve without the call sites having to coordinate. Every exchange routed through the middleware shares a single throttle, whose baseline delay adapts to what the server replies:

      • a successful exchange speeds the client up
      • a failed one slows it down
      • a response asking for a delay through a Retry-After header field, in either the delta seconds or the HTTP date form, sets the delay outright

      Exchanges failing on a transient status, that is 408, 429 or any 5xx, are retried as the throttle directs, up to attempts times; every other unsuccessful exchange, and the last attempt of an exhausted one, is relayed to the caller as it stands, so that status handling stays with the consumer.

      Important

      Retries are driven by the response status, so this middleware is to be declared after any middleware converting responses into rejections, as success does: a failure already reported as a Problem is no longer a response and is relayed to the caller without being retried.

      const client = createFetch(success(), throttle({ minimum: 100, attempts: 3 }));
      
      Warning

      attempts set to 0 retries a transient failure indefinitely, until the exchange either succeeds or fails on a status that isn't retried.

      Parameters

      • options: {} & { attempts?: number } = {}

        The throttling options, as defined by createThrottle, extended with the retry budget

          • Optional Readonlyattempts?: number

            The maximum number of attempts per exchange; 0 means no limit; defaults to 1, that is to a single attempt with no retries

        Returns Middleware

        A Middleware wrapping a Fetch implementation with one pacing every exchange and retrying transient failures