-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Retry and Timeout configuration for upstream requests (#322)
Co-authored-by: Valentin Cocaud <[email protected]> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
3ba3609
commit 23b8987
Showing
29 changed files
with
967 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@graphql-hive/gateway-runtime': patch | ||
--- | ||
|
||
dependencies updates: | ||
|
||
- Added dependency [`@graphql-hive/gateway-abort-signal-any@workspace:^` ↗︎](https://www.npmjs.com/package/@graphql-hive/gateway-abort-signal-any/v/workspace:^) (to `dependencies`) |
9 changes: 9 additions & 0 deletions
9
.changeset/@graphql-mesh_transport-common-322-dependencies.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
'@graphql-mesh/transport-common': patch | ||
--- | ||
|
||
dependencies updates: | ||
|
||
- Added dependency [`@graphql-hive/gateway-abort-signal-any@workspace:^` ↗︎](https://www.npmjs.com/package/@graphql-hive/gateway-abort-signal-any/v/workspace:^) (to `dependencies`) | ||
- Added dependency [`@graphql-tools/executor@^1.3.8` ↗︎](https://www.npmjs.com/package/@graphql-tools/executor/v/1.3.8) (to `dependencies`) | ||
- Removed dependency [`@graphql-tools/delegate@workspace:^` ↗︎](https://www.npmjs.com/package/@graphql-tools/delegate/v/workspace:^) (from `dependencies`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@graphql-tools/delegate': patch | ||
--- | ||
|
||
dependencies updates: | ||
|
||
- Updated dependency [`@graphql-tools/executor@^1.3.8` ↗︎](https://www.npmjs.com/package/@graphql-tools/executor/v/1.3.8) (from `^1.3.6`, in `dependencies`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@graphql-tools/executor-http': patch | ||
--- | ||
|
||
dependencies updates: | ||
|
||
- Added dependency [`@graphql-hive/gateway-abort-signal-any@workspace:^` ↗︎](https://www.npmjs.com/package/@graphql-hive/gateway-abort-signal-any/v/workspace:^) (to `dependencies`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@graphql-hive/gateway-abort-signal-any': patch | ||
--- | ||
|
||
New package |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
--- | ||
'@graphql-hive/gateway-runtime': minor | ||
'@graphql-hive/gateway': minor | ||
--- | ||
|
||
New Retry and Timeout plugins; | ||
|
||
- Retry plugin: Retry a request if it fails | ||
|
||
It respects the `Retry-After` HTTP header, [See more about this HTTP](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After) | ||
|
||
```ts | ||
export const gatewayConfig = defineConfig({ | ||
upstreamRetry: { | ||
// The maximum number of retries to attempt. | ||
maxRetries: 3, // required | ||
// The delay between retries in milliseconds. | ||
retryDelay: 1000, // default | ||
/** | ||
* A function that determines whether a response should be retried. | ||
* If the upstream returns `Retry-After` header, the request will be retried. | ||
*/ | ||
shouldRetry: ({ response }) => response?.status >= 500 || response?.status === 429 | ||
} | ||
// or you can configure it by subgraph name | ||
upstreamRetry({ subgraphName }) { | ||
if (subgraphName === 'my-rate-limited-subgraph') { | ||
return { | ||
maxRetries: 3, | ||
} | ||
} | ||
return { maxRetries: 10 } | ||
} | ||
}) | ||
``` | ||
|
||
- Timeout plugin: Timeout a request if it takes too long | ||
|
||
```ts | ||
export const gatewayConfig = defineConfig({ | ||
// The maximum time in milliseconds to wait for a response from the upstream. | ||
upstreamTimeout: 1000, // required | ||
// or you can configure it by subgraph name | ||
upstreamTimeout({ subgraphName }) { | ||
if (subgraphName === 'my-slow-subgraph') { | ||
return 1000; | ||
} | ||
} | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
"name": "@graphql-hive/gateway-abort-signal-any", | ||
"version": "0.0.0", | ||
"type": "module", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/graphql-hive/gateway.git", | ||
"directory": "packages/abort-signal-any" | ||
}, | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">=18.0.0" | ||
}, | ||
"main": "./dist/index.js", | ||
"exports": { | ||
".": { | ||
"require": { | ||
"types": "./dist/index.d.cts", | ||
"default": "./dist/index.cjs" | ||
}, | ||
"import": { | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.js" | ||
} | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"types": "./dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "pkgroll --clean-dist", | ||
"prepack": "yarn build" | ||
}, | ||
"dependencies": { | ||
"tslib": "^2.8.1" | ||
}, | ||
"devDependencies": { | ||
"pkgroll": "2.5.1" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"sideEffects": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
export type AbortSignalFromAny = AbortSignal & { | ||
signals: Set<AbortSignal>; | ||
addSignals(signals: Iterable<AbortSignal>): void; | ||
}; | ||
|
||
export function isAbortSignalFromAny( | ||
signal?: AbortSignal | null, | ||
): signal is AbortSignalFromAny { | ||
return signal != null && 'signals' in signal && 'addSignals' in signal; | ||
} | ||
|
||
export function abortSignalAny(givenSignals: Iterable<AbortSignal>) { | ||
const signals = new Set<AbortSignal>(); | ||
let singleSignal: AbortSignal | undefined; | ||
for (const signal of givenSignals) { | ||
if (isAbortSignalFromAny(signal)) { | ||
for (const childSignal of signal.signals) { | ||
singleSignal = childSignal; | ||
signals.add(childSignal); | ||
} | ||
} else { | ||
singleSignal = signal; | ||
signals.add(signal); | ||
} | ||
} | ||
if (signals.size < 2) { | ||
return singleSignal; | ||
} | ||
if (signals.size === 0) { | ||
return undefined; | ||
} | ||
const ctrl = new AbortController(); | ||
function onAbort(this: AbortSignal, ev: Event) { | ||
const signal = (ev.target as AbortSignal) || this; | ||
ctrl.abort(signal.reason); | ||
for (const signal of signals) { | ||
signal.removeEventListener('abort', onAbort); | ||
} | ||
} | ||
for (const signal of signals) { | ||
signal.addEventListener('abort', onAbort, { once: true }); | ||
} | ||
Object.defineProperties(ctrl.signal, { | ||
signals: { value: signals }, | ||
addSignals: { | ||
value(newSignals: Iterable<AbortSignal>) { | ||
for (const signal of newSignals) { | ||
if (isAbortSignalFromAny(signal)) { | ||
for (const childSignal of signal.signals) { | ||
if (!signals.has(childSignal)) { | ||
signals.add(childSignal); | ||
childSignal.addEventListener('abort', onAbort, { once: true }); | ||
} | ||
} | ||
} else { | ||
if (!signals.has(signal)) { | ||
signals.add(signal); | ||
signal.addEventListener('abort', onAbort, { once: true }); | ||
} | ||
} | ||
} | ||
}, | ||
}, | ||
}); | ||
return ctrl.signal as AbortSignalFromAny; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.