Problem when endpoint process takes too long

I have some endpoints in Go that work with a large amount of data, so it can take some minutes to get done. When all the process ends, the response is sent correctly but the client doesn’t get anything. I was trying with Postman and I get the same result. This problem only happens when the process takes too much to finish, I even try with another endpoint and I put a sleep for 2 minutes and it’s getting the same result, the process finishes correctly, it sends the 200 code status but Postman says that he couldn’t receive a response. If I put the sleep with only 1 minute it works correctly. I thought that it was a problem with the timeout so I tried adding some configurations like:

s := &http.Server{
Addr: “:8080”,
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}

and also:

http.TimeoutHandler(router, time.Second*150, “Timeout!”)

I’m using the gin framework.

This is called “client timeout”, the browser decides that it took to long and shuts down the connection. Even network devices in-between could cut the connection if there is no traffic for a to long time.

There are a two things you need to take care of:
clients - server connection lifetime timeouts
server handler timeouts

http.Server ReadTimeout and WriteTimeouts are for the connection lifetime.
ReadTimeout indicates the time from first initial request to end of the body read by server
WriteTimeout indicates the time from first initial request to end of response write.

http.TimeoutHandler is for your own handlers deadline, let’s say you don’t want any handlers to take 150 seconds to operate, use this.

According to your configuration, clients can get a timeout after 10 seconds, not 150 seconds. If it gets timeout in less then 10, check it out postman timeouts.

I tried this

router := gin.Default()

s := &http.Server{
	Addr:           ":" + cfg.Port,
	Handler:        router,
	ReadTimeout:    3 * time.Minute,
	WriteTimeout:   3 * time.Minute,
	MaxHeaderBytes: 1 << 20,
}

s.ListenAndServe()

But im having the same result:

Have you tried increasing the timeout of the client, as suggested by the last bullet point in the browser screenshot?

2m for a request is a long time. If there is really nothing on the wire, most clients might cancel the request after about a minute or so.

It is common to set up a background task for actions that are known to take a long time, and then give a “token” to the client. The client can then use that token to ask for the status of the job every now and then.

Yes, I saw that the default value of Postman is 0 which means never timeout, instead i tried putting the value in 180000 ms but it’s the same result.

So can you provide a minimal project to reproduce?

After making some tests, I realized that the problem is on the client, apparently Axios

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.