summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2024-03-21 19:01:44 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2024-03-21 19:18:38 +0900
commit05453881c308b88ed6991e2ff9d6ac322dfac7c8 (patch)
tree7288801c7e744ca2827e5cb95fe7a3c239cb754a
parent5e47ab94318a0a43b253bd09b5a18ef61cd7de1b (diff)
Set a 2-second timeout for POST requests
Close #3685
-rw-r--r--src/server.go9
1 files changed, 7 insertions, 2 deletions
diff --git a/src/server.go b/src/server.go
index c3954d8d..082d9278 100644
--- a/src/server.go
+++ b/src/server.go
@@ -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
}