add timeout to requests

This commit is contained in:
julian 2023-05-26 15:34:09 -06:00
parent 0955fdb6e6
commit a2cdc4cdfa

View file

@ -97,6 +97,8 @@ class JsonRPC {
final req = _requestQueue.next;
_socket!.write('${req.jsonRequest}\r\n');
req.initiateTimeout(const Duration(seconds: 10));
// Logging.instance.log(
// "JsonRPC request: wrote request ${req.jsonRequest} "
// "to socket $host:$port",
@ -214,5 +216,17 @@ class _JsonRPCRequest {
}
}
void initiateTimeout(Duration timeout) {
Future<void>.delayed(timeout).then((_) {
if (!isComplete) {
try {
throw Exception("_JsonRPCRequest timed out: $jsonRequest");
} catch (e, s) {
completer.completeError(e, s);
}
}
});
}
bool get isComplete => completer.isCompleted;
}