From 3dbe05669103403a6d32b68c62f52879d3150bb0 Mon Sep 17 00:00:00 2001 From: Akmal Date: Sat, 15 Jun 2024 11:09:35 +0700 Subject: [PATCH] Add rate limiter to 20rps --- go.mod | 2 +- go.sum | 4 ++-- handlers/data/data.go | 5 +++++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index d111b9d..a77208b 100644 --- a/go.mod +++ b/go.mod @@ -24,7 +24,7 @@ require ( github.com/DataDog/zstd v1.5.5 // indirect github.com/andybalholm/brotli v1.1.0 // indirect github.com/andybalholm/cascadia v1.3.2 // indirect - github.com/benbjohnson/clock v1.3.0 // indirect + github.com/benbjohnson/clock v1.3.5 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.2.0 // indirect github.com/cockroachdb/errors v1.11.1 // indirect diff --git a/go.sum b/go.sum index 270e11a..3719b7e 100644 --- a/go.sum +++ b/go.sum @@ -12,8 +12,8 @@ github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsVi github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU= github.com/ansrivas/fiberprometheus/v2 v2.6.1 h1:wac3pXaE6BYYTF04AC6K0ktk6vCD+MnDOJZ3SK66kXM= github.com/ansrivas/fiberprometheus/v2 v2.6.1/go.mod h1:MloIKvy4yN6hVqlRpJ/jDiR244YnWJaQC0FIqS8A+MY= -github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A= -github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= +github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o= +github.com/benbjohnson/clock v1.3.5/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= diff --git a/handlers/data/data.go b/handlers/data/data.go index 7c07022..b9c3dd9 100644 --- a/handlers/data/data.go +++ b/handlers/data/data.go @@ -19,6 +19,7 @@ import ( "github.com/tidwall/gjson" "github.com/valyala/fasthttp" "github.com/valyala/fasthttp/fasthttpproxy" + "go.uber.org/ratelimit" "golang.org/x/net/html" ) @@ -36,6 +37,8 @@ var ( ErrNotFound = errors.New("post not found") ) +var rl = ratelimit.New(20) // 20 rps + type Media struct { TypeName []byte URL []byte @@ -142,6 +145,8 @@ func (i *InstaData) GetData(postID string) error { } func getData(postID string) (gjson.Result, error) { + rl.Take() // Rate limit to 20 rps + req, res := fasthttp.AcquireRequest(), fasthttp.AcquireResponse() defer func() { fasthttp.ReleaseRequest(req)