Timeout Plugin
Abort requests that exceed a timeout on the client or the server, using a static value or a per-request dynamic timeout.
Client
Use TimeoutLinkPlugin to abort requests that exceed the timeout with an AbortError:
import { TimeoutLinkPlugin } from '@orpc/client/plugins'
const link = new RPCLink({
plugins: [
new TimeoutLinkPlugin({
timeout: 10_000, // 10 seconds
}),
],
})
Server
Use TimeoutHandlerPlugin to abort the request signal with an AbortError when handling exceeds the timeout:
import { TimeoutHandlerPlugin } from '@orpc/server/plugins'
const handler = new RPCHandler(router, {
plugins: [
new TimeoutHandlerPlugin({
timeout: 10_000, // 10 seconds
}),
],
})
Streaming Responses
The timeout option only covers producing the response, so streaming responses can outlive it. Use streamingTimeout, usually higher, to limit the full duration of streaming response bodies (async iterator objects and readable streams):
const handler = new RPCHandler(router, {
plugins: [
new TimeoutHandlerPlugin({
timeout: 10_000, // 10 seconds to produce the response
streamingTimeout: 300_000, // 5 minutes for the full stream
}),
],
})
Dynamic Timeout
The timeout and streamingTimeout options also accept a function, so you can resolve the timeout per request from the interceptor options. On the client these include the procedure path and the client context, on the server the matched procedure and the handler context:
const link = new RPCLink({
plugins: [
new TimeoutLinkPlugin({
timeout: ({ context, path }) => context.timeout ?? 10_000,
}),
],
})const handler = new RPCHandler(router, {
plugins: [
new TimeoutHandlerPlugin({
timeout: ({ path }) => path[0] === 'reports' ? 60_000 : 10_000,
}),
],
})Learn More
For implementation details, see the TimeoutLinkPlugin source code or the TimeoutHandlerPlugin source code.