Skip to content

Commit

Permalink
Basic application-level HTTP rate limiting
Browse files Browse the repository at this point in the history
Here set to 10 requests per minute. Note that the current implementation
doesn't use a shared store across instances, so in effect clients can
request on average instances-count * 10 requests per minute.
  • Loading branch information
tombh committed Jun 26, 2018
1 parent c0c6884 commit 16f1917
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion interfacer/src/browsh/raw_text_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"io"
"time"

"github.com/ulule/limiter"
"github.com/ulule/limiter/drivers/store/memory"
"github.com/ulule/limiter/drivers/middleware/stdlib"
"github.com/NYTimes/gziphandler"
)

Expand All @@ -29,12 +32,24 @@ func HTTPServerStart() {
Log("Starting Browsh HTTP server")
serverMux := http.NewServeMux()
uncompressed := http.HandlerFunc(handleHTTPServerRequest)
serverMux.Handle("/", gziphandler.GzipHandler(uncompressed))
limiterMiddleware := setupRateLimiter()
serverMux.Handle("/", limiterMiddleware.Handler(gziphandler.GzipHandler(uncompressed)))
if err := http.ListenAndServe(":" + *HTTPServerPort, &slashFix{serverMux}); err != nil {
Shutdown(err)
}
}

func setupRateLimiter() *stdlib.Middleware {
rate := limiter.Rate{
Period: 1 * time.Minute,
Limit: 10,
}
// TODO: Centralise store amongst instances with Redis
store := memory.NewStore()
middleware := stdlib.NewMiddleware(limiter.New(store, rate), stdlib.WithForwardHeader(true))
return middleware
}

func pseudoUUID() (uuid string) {
b := make([]byte, 16)
_, err := rand.Read(b)
Expand Down

0 comments on commit 16f1917

Please sign in to comment.