From 1deb2b9c54a8808147b855c3115b7b31a1d4906d Mon Sep 17 00:00:00 2001 From: DarthSim Date: Thu, 15 Mar 2018 20:53:39 +0600 Subject: [PATCH] Ass wait timeout --- config.go | 7 +++++++ server.go | 6 ++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/config.go b/config.go index 571c2d65..4d296ca0 100644 --- a/config.go +++ b/config.go @@ -77,6 +77,7 @@ func hexFileConfig(b *[]byte, filepath string) { type config struct { Bind string ReadTimeout int + WaitTimeout int WriteTimeout int DownloadTimeout int Concurrency int @@ -103,6 +104,7 @@ type config struct { var conf = config{ Bind: ":8080", ReadTimeout: 10, + WaitTimeout: 10, WriteTimeout: 10, DownloadTimeout: 5, Concurrency: runtime.NumCPU() * 2, @@ -125,6 +127,7 @@ func init() { strEnvConfig(&conf.Bind, "IMGPROXY_BIND") intEnvConfig(&conf.ReadTimeout, "IMGPROXY_READ_TIMEOUT") + intEnvConfig(&conf.WaitTimeout, "IMGPROXY_WAIT_TIMEOUT") intEnvConfig(&conf.WriteTimeout, "IMGPROXY_WRITE_TIMEOUT") intEnvConfig(&conf.DownloadTimeout, "IMGPROXY_DOWNLOAD_TIMEOUT") intEnvConfig(&conf.Concurrency, "IMGPROXY_CONCURRENCY") @@ -165,6 +168,10 @@ func init() { log.Fatalf("Read timeout should be greater than 0, now - %d\n", conf.ReadTimeout) } + if conf.WaitTimeout <= 0 { + log.Fatalf("Wait timeout should be greater than 0, now - %d\n", conf.WaitTimeout) + } + if conf.WriteTimeout <= 0 { log.Fatalf("Write timeout should be greater than 0, now - %d\n", conf.WriteTimeout) } diff --git a/server.go b/server.go index 121c678b..230df2f8 100644 --- a/server.go +++ b/server.go @@ -141,7 +141,9 @@ func checkSecret(s string) bool { return strings.HasPrefix(s, "Bearer ") && subtle.ConstantTimeCompare([]byte(strings.TrimPrefix(s, "Bearer ")), []byte(conf.Secret)) == 1 } -func (h *httpHandler) lock(t *timer) { +func (h *httpHandler) lock() { + t := startTimer(time.Duration(conf.WaitTimeout) * time.Second) + select { case h.sem <- struct{}{}: // Go ahead @@ -169,7 +171,7 @@ func (h *httpHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { t := startTimer(time.Duration(conf.WriteTimeout) * time.Second) - h.lock(t) + h.lock() defer h.unlock() if !checkSecret(r.Header.Get("Authorization")) {