Extend Timeout

How to extend request timeout for long-running requests

Resgate will send requests blindly onto NATS, waiting for a microservice to respond. If no response is received within a set amount of time (defaults to 3 seconds), Resgate will discard the request and send a system.timeout1 error to the client.

In some cases, a microservice may knowingly take longer time to process a request. Fortunately, the RES protocol provides a way for the microservice to extend the timeout for individual requests. Let’s learn how to do this.

Pre-response

Timeouts are extended in something called a pre-response2. The pre-response is sent before the actual response, often as soon as the request has been received.

Tip

To avoid redundant traffic, a service should only send a pre-response when the time it takes to handle the request might exceed the timeout limit of Resgate.

The pre-response message is an ordinary string without any leading white space. The string follows this pattern:

timeout:"<duration>"

Where <duration> is the new timeout in milliseconds.

Example

A call method with delayed response could look like this:

nats.subscribe('call.example.delayed', (msg, reply) => {
	// Increase timeout to 12 seconds
	nats.publish(reply, 'timeout:"12000"');
	// Send response after 10 seconds
	setTimeout(() => {
		nats.publish(reply, JSON.stringify({ result: null }));
	}, 10000);
});

Note

The pre-response is the only type of message that is not JSON encoded.