Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to increase a Go server timeout for specific endpoints?

Tags:

go

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?

like image 208
annoyed_developer_2022 Avatar asked Sep 14 '25 12:09

annoyed_developer_2022


1 Answers

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.

like image 59
Jasmine Crockett Avatar answered Sep 17 '25 03:09

Jasmine Crockett