forked from Qortal/qortal
Timeouts are specified by action, rather than using 10 second for every request. This allows certain requests to wait for longer before timing out, such as ones that create transactions.
This commit is contained in:
parent
8beffd4dae
commit
4d9cece9fa
@ -28,7 +28,7 @@ myfunction();
|
||||
|
||||
## Timeouts
|
||||
|
||||
By default, all requests will timeout after 10 seconds, and will throw an error - `The request timed out`. If you need a longer timeout - e.g. when fetching large QDN resources that may take a long time to be retrieved, you can use `qortalRequestWithTimeout(request, timeout)` as an alternative to `qortalRequest(request)`.
|
||||
By default, all requests will timeout after a certain amount of time (default 10 seconds, but some actions use a higher value), and will throw an error - `The request timed out`. If you need a longer timeout - e.g. when fetching large QDN resources that may take a long time to be retrieved, you can use `qortalRequestWithTimeout(request, timeout)` as an alternative to `qortalRequest(request)`.
|
||||
|
||||
```
|
||||
async function myfunction() {
|
||||
|
@ -358,6 +358,36 @@ const awaitTimeout = (timeout, reason) =>
|
||||
)
|
||||
);
|
||||
|
||||
function getDefaultTimeout(action) {
|
||||
if (action != null) {
|
||||
// Some actions need longer default timeouts, especially those that create transactions
|
||||
switch (action) {
|
||||
case "FETCH_QDN_RESOURCE":
|
||||
// Fetching data can take a while, especially if the status hasn't been checked first
|
||||
return 60 * 1000;
|
||||
|
||||
case "PUBLISH_QDN_RESOURCE":
|
||||
// Publishing could take a very long time on slow system, due to the proof-of-work computation
|
||||
// It's best not to timeout
|
||||
return 60 * 60 * 1000;
|
||||
|
||||
case "SEND_CHAT_MESSAGE":
|
||||
// Chat messages rely on PoW computations, so allow extra time
|
||||
return 60 * 1000;
|
||||
|
||||
case "JOIN_GROUP":
|
||||
case "DEPLOY_AT":
|
||||
case "SEND_COIN":
|
||||
// Allow extra time for other actions that create transactions, even if there is no PoW
|
||||
return 30 * 1000;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 10 * 1000;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a Qortal (Q-Apps) request with no timeout
|
||||
*/
|
||||
@ -381,7 +411,7 @@ const qortalRequestWithNoTimeout = (request) => new Promise((res, rej) => {
|
||||
* Make a Qortal (Q-Apps) request with the default timeout (10 seconds)
|
||||
*/
|
||||
const qortalRequest = (request) =>
|
||||
Promise.race([qortalRequestWithNoTimeout(request), awaitTimeout(10000, "The request timed out")]);
|
||||
Promise.race([qortalRequestWithNoTimeout(request), awaitTimeout(getDefaultTimeout(request.action), "The request timed out")]);
|
||||
|
||||
/**
|
||||
* Make a Qortal (Q-Apps) request with a custom timeout, specified in milliseconds
|
||||
|
Loading…
Reference in New Issue
Block a user