Ver código fonte

Ass wait timeout

DarthSim 7 anos atrás
pai
commit
1deb2b9c54
2 arquivos alterados com 11 adições e 2 exclusões
  1. 7 0
      config.go
  2. 4 2
      server.go

+ 7 - 0
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)
 	}

+ 4 - 2
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")) {