于10月8日至10日在纽约市加入我们,了解关于GraphQL联邦和API平台工程的最新技巧、趋势和新闻。参加2024年纽约市GraphQL峰会议
文档
免费开始

重试链

如果由于网络或服务器错误而失败,尝试多次执行操作。


概述

@apollo/client/link/retry可用于在一定次数内重试操作。这在处理不可靠的通信情况时很有用,你可能宁愿等待时间更长,而不是明确定义操作失败。@apollo/client/link/retry默认提供指数退避,并默认在尝试之间抖动延迟。

注意:它目前不处理响应中 GraphQL 错误的重试,只处理网络错误;可以链在 GraphQL 错误后重试操作。有关更多信息,请参阅错误处理文档

一个示例用例是在网络连接离线时保持请求,并在上线后重试。

import { RetryLink } from "@apollo/client/link/retry";
const link = new RetryLink();

选项

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

options.delay

OptionDescription
delay.initialThe number of milliseconds to wait before attempting the first retry.
delay.maxThe maximum number of milliseconds that the link should wait for any retry.
delay.jitterWhether delays between attempts should be randomized.

options.attempts

OptionDescription
attempts.maxThe max number of times to try a single operation before giving up.
attempts.retryIfA 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.

这两个功能结合使用,有助于缓解 thundering herd 问题,通过在主要停电期间分配负载来实现。如果没有这些策略,当服务器恢复正常时,它将一次性被所有客户端击中,这可能导致它再次出现故障。

自定义策略

您可以为delay和/或attempts传递一个函数,这些函数实现了针对每个的定制策略。在两种情况下,函数都提供了相同的countoperationerror)。

attempts函数应返回一个boolean(或一个解析为booleanPromise)来表示是否应该重试响应。如果是,则会调用delay函数,并应返回要延迟的毫秒数。

import { RetryLink } from "@apollo/client/link/retry";
const link = new RetryLink({
attempts: (count, operation, error) => {
return !!error && operation.operationName != 'specialCase';
},
delay: (count, operation, error) => {
return count * 1000 * Math.random();
},
});
上一页
REST
下一页
模式
评分文章评分在GitHub上编辑编辑论坛Discord

©2024Apollo Graph Inc.,简称Apollo GraphQL。

隐私政策

公司