Specification
After experimenting with PolykeyClient in Polykey Network Status. I've realized that the error handling is really tedious.
The main issues happen when a connection fails, the error you will receive from the PolykeyClient will be inconsistent depending on the point at which the code throws during the the stopping process.
This is because the Stopping signal propagation of WebSocketClient to the PolykeyClient is entirely asynchronous through EventListeners. This makes for a huge pain when attempting connection retries like so:
https://github.com/MatrixAI/Polykey-Network-Status/blob/staging/src/plugins/polykeyClientPlugin.ts
This initially gave me the impression that error handling with RPCClient + WebSocketClient was really messy.
But after playing around with it in React, I managed to come up with a small React context provider component that handles it all really elegantly: https://github.com/MatrixAI/Polykey-Enterprise/blob/feature-ws/src/app/contexts/RPCProvider.tsx
The gist is that instead of the lifecycle of PolykeyClient being flakily tied to WebSocketClient, the PolykeyClient will instead always be started, creating WebSocketConnections lazily when an RPCClient call is being attempted.
This has several benefits:
- Errors from PolykeyClient RPC calls are more predictable, as making an RPC call can no longer error from either
- The connection error from the RPC call
- The connection error from the WebSocketClient
- The WebSocketClientStopped error
- Reconnection + reinitate RPC call logic can be entirely encapsulated within a single loop of the RPC call, rather than 2 independent loops. This is because WebSocketConnection errors will now always occur on the RPC call, rather than possibly receiving three different errors in this circumstance.
Implementation
A lazy param will be simply added to the PolykeyClient constructor. There will be no API changes.
There WILL however be behaviour changes that need to be documented:
- PolykeyClient will never stop unless
.stop is called.
- RPC calls will throw WebSocketConnection errors if the lazily created WebSocketConnection is unsuccessful in connecting
Additional context
Tasks
- Add
lazy option to PolykeyClient
Specification
After experimenting with PolykeyClient in Polykey Network Status. I've realized that the error handling is really tedious.
The main issues happen when a connection fails, the error you will receive from the PolykeyClient will be inconsistent depending on the point at which the code throws during the the stopping process.
This is because the Stopping signal propagation of WebSocketClient to the PolykeyClient is entirely asynchronous through EventListeners. This makes for a huge pain when attempting connection retries like so:
https://github.com/MatrixAI/Polykey-Network-Status/blob/staging/src/plugins/polykeyClientPlugin.ts
This initially gave me the impression that error handling with RPCClient + WebSocketClient was really messy.
But after playing around with it in React, I managed to come up with a small React context provider component that handles it all really elegantly: https://github.com/MatrixAI/Polykey-Enterprise/blob/feature-ws/src/app/contexts/RPCProvider.tsx
The gist is that instead of the lifecycle of PolykeyClient being flakily tied to WebSocketClient, the PolykeyClient will instead always be started, creating WebSocketConnections lazily when an RPCClient call is being attempted.
This has several benefits:
Implementation
A
lazyparam will be simply added to the PolykeyClient constructor. There will be no API changes.There WILL however be behaviour changes that need to be documented:
.stopis called.Additional context
Tasks
lazyoption toPolykeyClient