mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-24 03:05:49 +03:00
fileserver: When out of filedescriptors, spread retry attempts
This commit is contained in:
parent
4a6121f989
commit
3513b6f2f7
1 changed files with 4 additions and 1 deletions
|
@ -2,10 +2,12 @@ package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -65,7 +67,8 @@ func (fh *fileHandler) serveFile(w http.ResponseWriter, r *http.Request, name st
|
||||||
return http.StatusForbidden, err
|
return http.StatusForbidden, err
|
||||||
}
|
}
|
||||||
// Likely the server is under load and ran out of file descriptors
|
// Likely the server is under load and ran out of file descriptors
|
||||||
w.Header().Set("Retry-After", "5") // TODO: 5 seconds enough delay? Or too much?
|
backoff := int(3 + rand.Int31()%3) // 3–5 seconds to prevent a stampede
|
||||||
|
w.Header().Set("Retry-After", strconv.Itoa(backoff))
|
||||||
return http.StatusServiceUnavailable, err
|
return http.StatusServiceUnavailable, err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
Loading…
Reference in a new issue