Skip to content

Commit 8d38826

Browse files
committed
feat: added timeoutMiddleware spec information to README.md
1 parent caa9bda commit 8d38826

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,45 @@ main();
899899

900900
```
901901
![img.png](images/unaryTest.png)
902+
903+
## Specifications
904+
905+
### Throwing Timeouts
906+
907+
By default, a timeout will not cause an RPC call to automatically throw, this must be manually done by the handler when it receives the abort signal from `ctx.signal`. An example of this is like so:
908+
909+
```ts
910+
class TestMethod extends UnaryHandler {
911+
public handle = async (
912+
input: JSONValue,
913+
cancel: (reason?: any) => void,
914+
meta: Record<string, JSONValue> | undefined,
915+
ctx: ContextTimed,
916+
): Promise<JSONValue> => {
917+
const abortProm = utils.promise<never>();
918+
ctx.signal.addEventListener('abort', () => {
919+
resolveCtxP(ctx);
920+
abortProm.resolveP(ctx.signal.reason);
921+
});
922+
throw await abortProm.p;
923+
};
924+
}
925+
```
926+
927+
### Timeout Middleware
928+
929+
The `timeoutMiddleware` sets an RPCServer's timeout based on the lowest timeout between the Client and the Server. This is so that handlers can eagerly time out and stop processing as soon as it is known that the client has timed out.
930+
931+
This case can be seen in the first diagram, where the server is able to stop the processing of the handler, and close the associated stream of the RPC call based on the shorter timeout sent by the client:
932+
933+
![RPCServer sets timeout based on RPCClient](images/timeoutMiddlewareClientTimeout.svg)
934+
935+
Where the `RPCClient` sends a timeout that is longer than that set on the `RPCServer`, it will be rejected. This is as the timeout of the client should never be expected to exceed that of the server, so that the server's timeout is an absolute limit.
936+
937+
![RPCServer rejects longer timeout sent by RPCClient](images/timeoutMiddlewareServerTimeout.svg)
938+
939+
The `timeoutMiddleware` is enabled by default, and uses the `.metadata.timeout` property on a JSON-RPC request object for the client to send it's timeout.
940+
902941
## Development
903942

904943
Run `nix-shell`, and once you're inside, you can use:

0 commit comments

Comments
 (0)