From 31e55a29ca44146480eb94f780f83824c460109b Mon Sep 17 00:00:00 2001 From: Brian Greenhill Date: Sat, 7 Mar 2020 13:04:03 +0100 Subject: [PATCH] initial simple rate limiting --- cmd/imageproxy/main.go | 4 +++- go.mod | 2 ++ go.sum | 7 +++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/cmd/imageproxy/main.go b/cmd/imageproxy/main.go index 2b8cc1d59..40fe51714 100644 --- a/cmd/imageproxy/main.go +++ b/cmd/imageproxy/main.go @@ -28,6 +28,7 @@ import ( "time" "github.com/PaulARoy/azurestoragecache" + "github.com/didip/tollbooth" "github.com/die-net/lrucache" "github.com/die-net/lrucache/twotier" "github.com/garyburd/redigo/redis" @@ -56,6 +57,7 @@ var verbose = flag.Bool("verbose", false, "print verbose logging messages") var version = flag.Bool("version", false, "Deprecated: this flag does nothing") var contentTypes = flag.String("contentTypes", "image/*", "comma separated list of allowed content types") var userAgent = flag.String("userAgent", "willnorris/imageproxy", "specify the user-agent used by imageproxy when fetching images from origin website") +var rateLimit = flag.Float64("rateLimit", 10, "set number of requests per second to allow before rate limiting occurs") func init() { flag.Var(&cache, "cache", "location to cache images (see https://github.com/willnorris/imageproxy#cache)") @@ -100,7 +102,7 @@ func main() { } fmt.Printf("imageproxy listening on %s\n", server.Addr) - http.Handle("/", p) + http.Handle("/", tollbooth.LimitFuncHandler(tollbooth.NewLimiter(*rateLimit, nil), p.ServeHTTP)) log.Fatal(http.ListenAndServe(*addr, nil)) } diff --git a/go.mod b/go.mod index c78a7a820..69e48f12c 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/PaulARoy/azurestoragecache v0.0.0-20170906084534-3c249a3ba788 github.com/aws/aws-sdk-go v1.19.0 github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect + github.com/didip/tollbooth v4.0.2+incompatible github.com/die-net/lrucache v0.0.0-20181227122439-19a39ef22a11 github.com/disintegration/imaging v1.6.0 github.com/dnaeon/go-vcr v1.0.1 // indirect @@ -21,6 +22,7 @@ require ( github.com/marstr/guid v0.0.0-20170427235115-8bdf7d1a087c // indirect github.com/muesli/smartcrop v0.2.1-0.20181030220600-548bbf0c0965 github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect + github.com/patrickmn/go-cache v2.1.0+incompatible // indirect github.com/peterbourgon/diskv v0.0.0-20171120014656-2973218375c3 github.com/prometheus/client_golang v1.4.1 github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd diff --git a/go.sum b/go.sum index 879e2d9b8..cda3c824a 100644 --- a/go.sum +++ b/go.sum @@ -40,6 +40,9 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/didip/tollbooth v1.0.0 h1:nVIxVp0Jj75TVxTwUdbRzC0qO0K/LRZudetGemvTCxQ= +github.com/didip/tollbooth v4.0.2+incompatible h1:fVSa33JzSz0hoh2NxpwZtksAzAgd7zjmGO20HCZtF4M= +github.com/didip/tollbooth v4.0.2+incompatible/go.mod h1:A9b0665CE6l1KmzpDws2++elm/CsuWBMa5Jv4WY0PEY= github.com/die-net/lrucache v0.0.0-20181227122439-19a39ef22a11 h1:tFq0KToN9jzAQI8o1eIgIv+wDK6p2v+OD3yuQrAbwmA= github.com/die-net/lrucache v0.0.0-20181227122439-19a39ef22a11/go.mod h1:ew0MSjCVDdtGMjF3kzLK9hwdgF5mOE8SbYVF3Rc7mkU= github.com/disintegration/imaging v1.6.0 h1:nVPXRUUQ36Z7MNf0O77UzgnOb1mkMMor7lmJMJXc/mA= @@ -141,6 +144,9 @@ github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1Cpa github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8= github.com/openzipkin/zipkin-go v0.1.3/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8= github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw= +github.com/patrickmn/go-cache v1.0.0 h1:3gD5McaYs9CxjyK5AXGcq8gdeCARtd/9gJDUvVeaZ0Y= +github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= +github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/peterbourgon/diskv v0.0.0-20171120014656-2973218375c3 h1:ZKRE3mqKoxObHs5oWjLnA1WxXhmlDDAVuE0VsuLIoNk= github.com/peterbourgon/diskv v0.0.0-20171120014656-2973218375c3/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU= github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= @@ -251,6 +257,7 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2 h1:z99zHgr7hKfrUcX/KsoJk5FJfjTceCKIp96+biqP4To= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/time v0.0.0-20181108054448-85acf8d2951c h1:fqgJT0MGcGpPgpWU7VRdRjuArfcOvC4AoJmILihzhDg= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20181219222714-6e267b5cc78e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=