Skip to content

Commit 28e19f4

Browse files
authored
Added rate limiting with httprate (#193)
1 parent 3e88cc1 commit 28e19f4

File tree

5 files changed

+22
-1
lines changed

5 files changed

+22
-1
lines changed

const.go

+13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package convoy
22

3+
import "time"
4+
35
const (
46
HttpPost HttpMethod = "POST"
57
)
@@ -10,3 +12,14 @@ const (
1012
Monthly
1113
Yearly
1214
)
15+
16+
const (
17+
// With this Convoy will not process more than 3000
18+
// concurrent requests per minute. We use github.com/go-chi/httprate
19+
// which uses a sliding window algorithm, so we should be fine :)
20+
// TODO(subomi): We need to configure rate limiting to be per
21+
// client as well as implement distributed limiting backed by
22+
// a distributed key value store.
23+
RATE_LIMIT = 5000
24+
RATE_LIMIT_DURATION = 1 * time.Minute
25+
)

docs/docs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Package docs GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
22
// This file was generated by swaggo/swag at
3-
// 2021-10-26 23:11:32.542301 +0100 WAT m=+26.721756418
3+
// 2021-10-28 18:49:28.999858 +0100 WAT m=+27.284431293
44
package docs
55

66
import (

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/getsentry/sentry-go v0.11.0
1010
github.com/ghodss/yaml v1.0.0
1111
github.com/go-chi/chi/v5 v5.0.3
12+
github.com/go-chi/httprate v0.5.2 // indirect
1213
github.com/go-chi/render v1.0.1
1314
github.com/go-redis/redis/v8 v8.11.4
1415
github.com/gobeam/mongo-go-pagination v0.0.7

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/
105105
github.com/go-check/check v0.0.0-20180628173108-788fd7840127/go.mod h1:9ES+weclKsC9YodN5RgxqK/VD9HM9JsCSh7rNhMZE98=
106106
github.com/go-chi/chi/v5 v5.0.3 h1:khYQBdPivkYG1s1TAzDQG1f6eX4kD2TItYVZexL5rS4=
107107
github.com/go-chi/chi/v5 v5.0.3/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
108+
github.com/go-chi/httprate v0.5.2 h1:pynJZu4jbSSHFRjpbT7EJJf8b9qt2CLZnqqKy0F+jH4=
109+
github.com/go-chi/httprate v0.5.2/go.mod h1:kYR4lorHX3It9tTh4eTdHhcF2bzrYnCrRNlv5+IBm2M=
108110
github.com/go-chi/render v1.0.1 h1:4/5tis2cKaNdnv9zFLfXzcquC9HbeZgCnxGnKrltBS8=
109111
github.com/go-chi/render v1.0.1/go.mod h1:pq4Rr7HbnsdaeHagklXub+p6Wd16Af5l9koip1OvJns=
110112
github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w=

server/route.go

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/frain-dev/convoy/queue"
1515

1616
"github.com/go-chi/chi/v5/middleware"
17+
"github.com/go-chi/httprate"
1718
"github.com/prometheus/client_golang/prometheus"
1819
"github.com/prometheus/client_golang/prometheus/promhttp"
1920
log "github.com/sirupsen/logrus"
@@ -52,6 +53,10 @@ func buildRoutes(app *applicationHandler) http.Handler {
5253

5354
// Public API.
5455
router.Route("/api", func(v1Router chi.Router) {
56+
57+
// rate limit all requests.
58+
v1Router.Use(httprate.LimitAll(convoy.RATE_LIMIT, convoy.RATE_LIMIT_DURATION))
59+
5560
v1Router.Route("/v1", func(r chi.Router) {
5661
r.Use(middleware.AllowContentType("application/json"))
5762
r.Use(jsonResponse)

0 commit comments

Comments
 (0)