Skip to content

Commit

Permalink
Compile middlewares in a single file. Fix int width for rps param
Browse files Browse the repository at this point in the history
  • Loading branch information
volmedo committed Jun 7, 2019
1 parent 00d6f08 commit 0dc781b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 75 deletions.
32 changes: 0 additions & 32 deletions cmd/server/limiter.go

This file was deleted.

9 changes: 5 additions & 4 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import (

func main() {
var dbHost, dbUser, dbPass, dbName, migrationsPath string
var port, rps, dbPort int
var port, dbPort int
var rps int64
flag.IntVar(&port, "port", 8080, "Port where the server is listening for connections.")
flag.IntVar(&rps, "rps", 100, "Rate limit expressed in requests per second (per client)")
flag.Int64Var(&rps, "rps", 100, "Rate limit expressed in requests per second (per client)")

flag.StringVar(&dbHost, "dbhost", "localhost", "Address of the server that hosts the DB")
flag.IntVar(&dbPort, "dbport", 5432, "Port where the DB server is listening for connections")
Expand Down Expand Up @@ -55,11 +56,11 @@ func main() {
}

apiHandler, prometheusHandler := newMeasuredHandler(apiHandler)
apiHandler, err = newRateLimitedHandler(int64(rps), apiHandler)
apiHandler, err = newRateLimitedHandler(rps, apiHandler)
if err != nil {
log.Panicf("Error creating rate limiter middleware: %v", err)
}
apiHandler = newRecoveredHandler(apiHandler)
apiHandler = newRecoverableHandler(apiHandler)

mux := http.NewServeMux()
mux.Handle("/metrics", prometheusHandler)
Expand Down
25 changes: 0 additions & 25 deletions cmd/server/metrics.go

This file was deleted.

59 changes: 59 additions & 0 deletions cmd/server/middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"fmt"
"net/http"
"time"

"github.com/prometheus/client_golang/prometheus/promhttp"
metrics "github.com/slok/go-http-metrics/metrics/prometheus"
"github.com/slok/go-http-metrics/middleware"
"github.com/ulule/limiter/v3"
"github.com/ulule/limiter/v3/drivers/middleware/stdlib"
"github.com/ulule/limiter/v3/drivers/store/memory"
"github.com/unrolled/recovery"
)

// newMeasuredHandler creates a middleware that take essential metrics about
// the handler being measured, such as number of requests, duration of each request,
// concurrent or in-flight requests and response size.
// This function returns two handlers, the handler being measured and a Prometheus
// handler that exposes the metrics being collected
func newMeasuredHandler(handler http.Handler) (measuredH http.Handler, metricsH http.Handler) {
recorder := metrics.NewRecorder(metrics.Config{
Prefix: "pAPI",
})
mdlw := middleware.New(middleware.Config{
Recorder: recorder,
})

return mdlw.Handler("", handler), promhttp.Handler()
}

// newRateLimitedHandler creates a new middleware based on ulule/limiter package that
// limits the request rate that is sent to the specified handler.
// The returned rate-limited handler will allow up to rps requests per second to
// handler. When the rate exceeds the limit, a "429 Too Many Requests" response will be
// sent back without invoking the wrapped handler.
func newRateLimitedHandler(rps int64, handler http.Handler) (http.Handler, error) {
if rps <= 0 {
return nil, fmt.Errorf("rps cannot be negative (rps = %d)", rps)
}

store := memory.NewStore()
rate := limiter.Rate{
Period: time.Second,
Limit: rps,
}
instance := limiter.New(store, rate)
middleware := stdlib.NewMiddleware(instance)

return middleware.Handler(handler), nil
}

// newRecoveredHandler adds a basic panic recovery middleware so that clients
// get a 500 Internal Server Error when something goes wrong
func newRecoverableHandler(handler http.Handler) http.Handler {
rec := recovery.New()
return rec.Handler(handler)
}
14 changes: 0 additions & 14 deletions cmd/server/recover.go

This file was deleted.

0 comments on commit 0dc781b

Please sign in to comment.