0
Fork 0
mirror of https://github.com/willnorris/imageproxy.git synced 2024-12-16 21:56:43 -05: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:
Will Norris 2016-11-29 13:50:19 -08:00
parent 94dbd77d6e
commit 93166a5b20
2 changed files with 17 additions and 1 deletions

View file

@ -49,6 +49,7 @@ var cacheDir = flag.String("cacheDir", "", "(Deprecated; use 'cache' instead) di
var cacheSize = flag.Uint64("cacheSize", 0, "Deprecated: this flag does nothing")
var signatureKey = flag.String("signatureKey", "", "HMAC key used in calculating request signatures")
var scaleUp = flag.Bool("scaleUp", false, "allow images to scale beyond their original dimensions")
var timeout = flag.Duration("timeout", 0, "time limit for requests served by this proxy")
var version = flag.Bool("version", false, "print version information")
func main() {
@ -91,6 +92,7 @@ func main() {
}
}
p.Timeout = *timeout
p.ScaleUp = *scaleUp
server := &http.Server{

View file

@ -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)