We have a monolithic API that (in addition to other endpoints) has a number of reporting endpoints under the /reports
prefix. These reporting endpoints all take a while (>2 minutes) to return a result, and the results from these /reports/*
endpoints are all cached by our CDN. Each /reports/*
endpoint has to be called once per day, and the problem is that the HTTP calls may time out if the reporting takes too long.
In the long term, the plan is to refactor this monolithic API to break it down into smaller microservices, and to completely remove the need to call an HTTP endpoint in order to trigger the building of these reports (i.e. by having a cron job that directly runs the reporting before populating the CDN with the results).
In the short term, however, these reports consistently fail. As an immedate workaround, we have increased the WriteTimeout
and IdleTimeout
on the HTTP server to 3 minutes, but the problem with this workaround is that sometimes the reports take even longer than 3 minutes (meaning that they still timeout). The other problem is that since this is a monolithic API, increasing the WriteTimeout
and IdleTimeout
on the server because of the /reporting/*
endpoints means that these timeouts are increased for all endpoints attached to the server - which means that we are effectively increasing the risk of a DOS attack.
Is there a way to increase the timeouts for a specific subset of endpoints?
Set the write deadline in the /reports/*
handlers using a response controller.
For example, add this line to the handler to set the timeout to 10 minutes:
http.NewResponseController(rw).SetWriteDeadline(time.Now().Add(10 * time.Minute))
The idle timeout is not relevant to your problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With