Set a 2-second timeout for POST requests

Close #3685
This commit is contained in:
Junegunn Choi 2024-03-21 19:01:44 +09:00
parent 5e47ab9431
commit 05453881c3
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
1 changed files with 7 additions and 2 deletions

View File

@ -32,6 +32,7 @@ const (
httpUnauthorized = "HTTP/1.1 401 Unauthorized" + crlf
httpUnavailable = "HTTP/1.1 503 Service Unavailable" + crlf
httpReadTimeout = 10 * time.Second
channelTimeout = 2 * time.Second
jsonContentType = "Content-Type: application/json" + crlf
maxContentLength = 1024 * 1024
)
@ -170,7 +171,7 @@ func (server *httpServer) handleHttpRequest(conn net.Conn) string {
select {
case response := <-server.responseChannel:
return good(response)
case <-time.After(2 * time.Second):
case <-time.After(channelTimeout):
go func() {
// Drain the channel
<-server.responseChannel
@ -227,7 +228,11 @@ func (server *httpServer) handleHttpRequest(conn net.Conn) string {
return bad("no action specified")
}
server.actionChannel <- actions
select {
case server.actionChannel <- actions:
case <-time.After(channelTimeout):
return httpUnavailable + crlf
}
return httpOk + crlf
}