mirror of
https://github.com/willnorris/imageproxy.git
synced 2026-05-29 13:55:13 +02:00
add support for per-request timeout
Adds a -timeout flag for specifying the timeout. Currently, this returns a 503 response on timeout, though it should really be a 504, since imageproxy is acting as a gateway. Ref: #75
This commit is contained in:
parent
94dbd77d6e
commit
93166a5b20
2 changed files with 17 additions and 1 deletions
|
|
@ -58,6 +58,11 @@ type Proxy struct {
|
|||
|
||||
// Allow images to scale beyond their original dimensions.
|
||||
ScaleUp bool
|
||||
|
||||
// Timeout specifies a time limit for requests served by this Proxy.
|
||||
// If a call runs for longer than its time limit, a 504 Gateway Timeout
|
||||
// response is returned. A Timeout of zero means no timeout.
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
// NewProxy constructs a new proxy. The provided http RoundTripper will be
|
||||
|
|
@ -87,7 +92,7 @@ func NewProxy(transport http.RoundTripper, cache Cache) *Proxy {
|
|||
return &proxy
|
||||
}
|
||||
|
||||
// ServeHTTP handles image requests.
|
||||
// ServeHTTP handles incoming requests.
|
||||
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/favicon.ico" {
|
||||
return // ignore favicon requests
|
||||
|
|
@ -98,6 +103,15 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
var h http.Handler = http.HandlerFunc(p.serveImage)
|
||||
if p.Timeout > 0 {
|
||||
h = http.TimeoutHandler(h, p.Timeout, "")
|
||||
}
|
||||
h.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
// serveImage handles incoming requests for proxied images.
|
||||
func (p *Proxy) serveImage(w http.ResponseWriter, r *http.Request) {
|
||||
req, err := NewRequest(r, p.DefaultBaseURL)
|
||||
if err != nil {
|
||||
msg := fmt.Sprintf("invalid request URL: %v", err)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue