npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

apollo-link-retry

v2.2.16

Published

Retry Apollo Link for GraphQL Network Stack

Downloads

362,617

Readme


title: apollo-link-retry description: Attempt an operation multiple times if it fails due to network or server errors.

Sometimes, you're in an unreliable situation but you would rather wait longer than explicitly fail an operation. apollo-link-retry provides exponential backoff, and jitters delays between attempts by default. It does not (currently) handle retries for GraphQL errors in the response, only for network errors.

One such use case is to hold on to a request while a network connection is offline and retry until it comes back online.

import { RetryLink } from "apollo-link-retry";

const link = new RetryLink();

Options

The standard retry strategy provides exponential backoff with jittering, and takes the following options, grouped into delay and attempt strategies:

options.delay

  • delay.initial: The number of milliseconds to wait before attempting the first retry.

  • delay.max: The maximum number of milliseconds that the link should wait for any retry.

  • delay.jitter: Whether delays between attempts should be randomized.

options.attempts

  • attempts.max: The max number of times to try a single operation before giving up.

  • attempts.retryIf: A predicate function that can determine whether a particular response should be retried.

Default configuration

The default configuration is equivalent to:

new RetryLink({
  delay: {
    initial: 300,
    max: Infinity,
    jitter: true
  },
  attempts: {
    max: 5,
    retryIf: (error, _operation) => !!error
  }
});

Avoiding thundering herd

Starting with initialDelay, the delay of each subsequent retry is increased exponentially, meaning it's multiplied by 2 each time. For example, if initialDelay is 100, additional retries will occur after delays of 200, 400, 800, etc.

With the jitter option enabled, delays are randomized anywhere between 0ms (instant), and 2x the configured delay. This way you get the same result on average, but with random delays.

These two features combined help alleviate the thundering herd problem, by distributing load during major outages. Without these strategies, when your server comes back up it will be hit by all of your clients at once, possibly causing it to go down again.

Custom Strategies

Instead of the options object, you may pass a function for delay and/or attempts, which implement custom strategies for each. In both cases the function is given the same arguments (count, operation, error).

The attempts function should return a boolean indicating whether the response should be retried. If yes, the delay function is then called, and should return the number of milliseconds to delay by.

import { RetryLink } from "apollo-link-retry";

const link = new RetryLink({
  attempts: (count, operation, error) => {
    return !!error && operation.operationName != 'specialCase';
  },
  delay: (count, operation, error) => {
    return count * 1000 * Math.random();
  },
});