Skip to content

Commit

Permalink
chore: Migrate redigo to go-redis, upgrade throttled
Browse files Browse the repository at this point in the history
  • Loading branch information
mschfh committed Jan 22, 2025
1 parent 84a3c8e commit 9db4ff3
Show file tree
Hide file tree
Showing 17 changed files with 241 additions and 170 deletions.
5 changes: 3 additions & 2 deletions cmd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ require (
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/getsentry/sentry-go v0.14.0 // indirect
github.com/go-chi/chi v4.1.2+incompatible // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gomodule/redigo v2.0.0+incompatible // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
Expand All @@ -38,6 +38,7 @@ require (
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/redis/go-redis/v9 v9.7.0 // indirect
github.com/rs/xid v1.4.0 // indirect
github.com/satori/go.uuid v1.2.0 // indirect
github.com/shengdoushi/base58 v1.0.0 // indirect
Expand All @@ -46,7 +47,7 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/throttled/throttled v2.2.5+incompatible // indirect
github.com/throttled/throttled/v2 v2.12.0 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
Expand Down
51 changes: 47 additions & 4 deletions cmd/go.sum

Large diffs are not rendered by default.

21 changes: 7 additions & 14 deletions libs/clients/coingecko/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import (
"time"

"github.com/brave-intl/bat-go/libs/clients"
"github.com/brave-intl/bat-go/libs/closers"
appctx "github.com/brave-intl/bat-go/libs/context"
"github.com/gomodule/redigo/redis"
"github.com/google/go-querystring/query"
"github.com/redis/go-redis/v9"
"github.com/shopspring/decimal"
)

Expand All @@ -37,11 +36,11 @@ type Client interface {
type HTTPClient struct {
baseParams
client *clients.SimpleHTTPClient
redis *redis.Pool
redis *redis.Client
}

// NewWithContext returns a new HTTPClient, retrieving the base URL from the context
func NewWithContext(ctx context.Context, redis *redis.Pool) (Client, error) {
func NewWithContext(ctx context.Context, redis *redis.Client) (Client, error) {
// get the server url from context
serverURL, err := appctx.GetStringFromContext(ctx, appctx.CoingeckoServerCTXKey)
if err != nil {
Expand Down Expand Up @@ -174,12 +173,9 @@ func (c *HTTPClient) FetchMarketChart(
return nil, updated, err
}

conn := c.redis.Get()
defer closers.Log(ctx, conn)

var body MarketChartResponse
var entry cacheEntry
entryBytes, err := redis.Bytes(conn.Do("GET", cacheKey))
entryBytes, err := c.redis.Get(ctx, cacheKey).Bytes()
if err == nil {
err = json.Unmarshal(entryBytes, &entry)
if err != nil {
Expand Down Expand Up @@ -222,7 +218,7 @@ func (c *HTTPClient) FetchMarketChart(
if err != nil {
return nil, updated, err
}
_, err = conn.Do("SET", cacheKey, entryBytes)
err = c.redis.Set(ctx, cacheKey, entryBytes, 0).Err()
if err != nil {
return nil, updated, err
}
Expand Down Expand Up @@ -361,14 +357,11 @@ func (c *HTTPClient) FetchCoinMarkets(
return nil, updated, err
}

conn := c.redis.Get()
defer closers.Log(ctx, conn)

var body CoinMarketResponse
var entry cacheEntry

// Check cache first before making request to Coingecko
entryBytes, err := redis.Bytes(conn.Do("GET", cacheKey))
entryBytes, err := c.redis.Get(ctx, cacheKey).Bytes()
if err == nil {
err = json.Unmarshal(entryBytes, &entry)
if err != nil {
Expand Down Expand Up @@ -424,7 +417,7 @@ func (c *HTTPClient) FetchCoinMarkets(
return nil, updated, err
}

_, err = conn.Do("SET", cacheKey, entryBytes)
err = c.redis.Set(ctx, cacheKey, entryBytes, 0).Err()
if err != nil {
return nil, updated, err
}
Expand Down
34 changes: 14 additions & 20 deletions libs/clients/coingecko/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ import (
"github.com/brave-intl/bat-go/libs/clients/coingecko"
appctx "github.com/brave-intl/bat-go/libs/context"
logutils "github.com/brave-intl/bat-go/libs/logging"
"github.com/gomodule/redigo/redis"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

type CoingeckoTestSuite struct {
suite.Suite
redisPool *redis.Pool
client coingecko.Client
ctx context.Context
redis *redis.Client
client coingecko.Client
ctx context.Context
}

func TestCoingeckoTestSuite(t *testing.T) {
Expand Down Expand Up @@ -54,31 +55,24 @@ func (suite *CoingeckoTestSuite) SetupTest() {
// vs-currency limit
suite.ctx = context.WithValue(suite.ctx, appctx.CoingeckoVsCurrencyLimitCTXKey, coingeckoCurrencyLimit)

var redisAddr string = "redis://grant-redis"
var redisAddr string = "redis://grant-redis:6379"
if len(os.Getenv("REDIS_ADDR")) > 0 {
redisAddr = os.Getenv("REDIS_ADDR")
}

suite.redisPool = &redis.Pool{
MaxIdle: 50,
MaxActive: 1000,
Dial: func() (redis.Conn, error) {
conn, err := redis.DialURL(redisAddr)
suite.Require().NoError(err, "failed to connect to redis")
return conn, err
},
opts, err := redis.ParseURL(redisAddr)
if err != nil {
suite.Require().NoError(err, "Must be able to parse redis URL")
}

rConn := suite.redisPool.Get()
defer rConn.Close()
s, err := redis.String(rConn.Do("PING"))
suite.Require().NoError(err, "failed to connect to redis")
suite.Require().True(s == "PONG", "bad response from redis")
suite.redis = redis.NewClient(opts)

err = suite.redis.Ping(suite.ctx).Err()
suite.Require().NoError(err, "Must be able to connect to redis")

// setup the client under test, no redis, will test redis interactions in ratios service
suite.client, err = coingecko.NewWithContext(suite.ctx, suite.redisPool)
suite.client, err = coingecko.NewWithContext(suite.ctx, suite.redis)
suite.Require().NoError(err, "Must be able to correctly initialize the client")

}

func (suite *CoingeckoTestSuite) TestFetchSimplePrice() {
Expand Down
5 changes: 3 additions & 2 deletions libs/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ require (
github.com/go-jose/go-jose/v3 v3.0.0
github.com/golang-migrate/migrate/v4 v4.15.2
github.com/golang/mock v1.6.0
github.com/gomodule/redigo v2.0.0+incompatible
github.com/google/go-querystring v1.1.0
github.com/jmoiron/sqlx v1.3.5
github.com/lib/pq v1.10.7
github.com/linkedin/goavro v2.1.0+incompatible
github.com/mssola/user_agent v0.5.3
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/prometheus/client_golang v1.13.0
github.com/redis/go-redis/v9 v9.7.0
github.com/rs/zerolog v1.28.0
github.com/satori/go.uuid v1.2.0
github.com/segmentio/kafka-go v0.4.35
Expand All @@ -32,7 +32,7 @@ require (
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.1
github.com/superp00t/niceware v0.0.0-20170614015008-16cb30c384b5
github.com/throttled/throttled v2.2.5+incompatible
github.com/throttled/throttled/v2 v2.12.0
github.com/tyler-smith/go-bip39 v1.1.0
golang.org/x/crypto v0.31.0
golang.org/x/net v0.23.0
Expand Down Expand Up @@ -60,6 +60,7 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/containerd/containerd v1.6.18 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-cmp v0.5.9 // indirect
Expand Down
22 changes: 18 additions & 4 deletions libs/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnweb
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/bshuster-repo/logrus-logstash-hook v0.4.1/go.mod h1:zsTqEiSzDgAa/8GZR7E1qaXrhYNDKBYy5/dWPTIflbk=
github.com/bsm/ginkgo/v2 v2.7.0/go.mod h1:AiKlXPm7ItEHNc/2+OkrNG4E0ITzojb9/xWzvQ9XZ9w=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/gomega v1.26.0/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg=
Expand Down Expand Up @@ -409,6 +413,8 @@ github.com/denisenkom/go-mssqldb v0.10.0/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27
github.com/denverdino/aliyungo v0.0.0-20190125010748-a747050bb1ba/go.mod h1:dV8lFg6daOBZbT6/BDGIz6Y3WFGn8juu6G+CQ6LHtl0=
github.com/dgrijalva/jwt-go v0.0.0-20170104182250-a601269ab70c/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/dhui/dktest v0.3.10 h1:0frpeeoM9pHouHjhLeZDuDTJ0PqjDTrycaHaMmkJAo8=
github.com/dhui/dktest v0.3.10/go.mod h1:h5Enh0nG3Qbo9WjNFRrwmKUaePEBhXMOygbz3Ww7Sz0=
Expand Down Expand Up @@ -517,6 +523,8 @@ github.com/go-openapi/swag v0.0.0-20160704191624-1d0bd113de87/go.mod h1:DXUve3Dp
github.com/go-openapi/swag v0.19.2/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/go-openapi/swag v0.19.14/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ=
github.com/go-redis/redis v6.15.8+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-redis/redis/v8 v8.4.2/go.mod h1:A1tbYoHSa1fXwN+//ljcCYYJeLmVrwL9hbQN45Jdy0M=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
Expand Down Expand Up @@ -613,8 +621,8 @@ github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEW
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0=
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
github.com/gomodule/redigo v1.8.9 h1:Sl3u+2BI/kk+VEatbj0scLdrFhjPmbxOc1myhDP41ws=
github.com/gomodule/redigo v1.8.9/go.mod h1:7ArFNvsTjH8GMMzB4uy1snslv2BwmginuMs06a1uzZE=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.1/go.mod h1:xXMiIv4Fb/0kKde4SpL7qlzvu5cMJDRkFDxJfI9uaxA=
Expand Down Expand Up @@ -961,6 +969,7 @@ github.com/onsi/ginkgo v1.12.0/go.mod h1:oUhWkIvk5aDxtKvDDuw8gItl8pKl42LzjC9KZE0
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
github.com/onsi/ginkgo v1.13.0/go.mod h1:+REjRxOmWfHCjfv9TTWB1jD1Frx4XydAD3zm1lskyM0=
github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
github.com/onsi/ginkgo v1.14.2/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
github.com/onsi/gomega v0.0.0-20151007035656-2152b45fa28a/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
Expand Down Expand Up @@ -1074,6 +1083,9 @@ github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1
github.com/prometheus/procfs v0.8.0 h1:ODq8ZFEaYeCaZOJlZZdJA2AbQR98dSHSM1KW/You5mo=
github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0uaxHdg830/4=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/redis/go-redis/v9 v9.0.5/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=
github.com/redis/go-redis/v9 v9.7.0 h1:HhLSs+B6O021gwzl+locl0zEDnyNkxMtf/Z3NNBMa9E=
github.com/redis/go-redis/v9 v9.7.0/go.mod h1:f6zhXITC7JUJIlPEiBOTXxJgPLdZcA93GewI7inzyWw=
github.com/remyoudompheng/bigfft v0.0.0-20190728182440-6a916e37a237/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
Expand Down Expand Up @@ -1174,8 +1186,8 @@ github.com/syndtr/gocapability v0.0.0-20170704070218-db04d3cc01c8/go.mod h1:hkRG
github.com/syndtr/gocapability v0.0.0-20180916011248-d98352740cb2/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
github.com/tchap/go-patricia v2.2.6+incompatible/go.mod h1:bmLyhP68RS6kStMGxByiQ23RP/odRBOTVjwp2cDyi6I=
github.com/throttled/throttled v2.2.5+incompatible h1:65UB52X0qNTYiT0Sohp8qLYVFwZQPDw85uSa65OljjQ=
github.com/throttled/throttled v2.2.5+incompatible/go.mod h1:0BjlrEGQmvxps+HuXLsyRdqpSRvJpq0PNIsOtqP9Nos=
github.com/throttled/throttled/v2 v2.12.0 h1:IezKE1uHlYC/0Al05oZV6Ar+uN/znw3cy9J8banxhEY=
github.com/throttled/throttled/v2 v2.12.0/go.mod h1:+EAvrG2hZAQTx8oMpBu8fq6Xmm+d1P2luKK7fIY1Esc=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
Expand Down Expand Up @@ -1250,6 +1262,7 @@ go.opentelemetry.io/contrib v0.20.0/go.mod h1:G/EtFaa6qaN7+LxqfIAT3GiZa7Wv5DTBUz
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.20.0/go.mod h1:oVGt1LRbBOBq1A5BQLlUg9UaU/54aiHw8cgjV3aWZ/E=
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.28.0/go.mod h1:vEhqr0m4eTc+DWxfsXoXue2GBgV2uUwVznkGIHW/e5w=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.20.0/go.mod h1:2AboqHi0CiIZU0qwhtUfCYD1GeUzvvIXWNkhDt7ZMG4=
go.opentelemetry.io/otel v0.14.0/go.mod h1:vH5xEuwy7Rts0GNtsCW3HYQoZDY+OmBJ6t1bFGGlxgw=
go.opentelemetry.io/otel v0.20.0/go.mod h1:Y3ugLH2oa81t5QO+Lty+zXf8zC9L26ax4Nzoxm/dooo=
go.opentelemetry.io/otel v1.3.0/go.mod h1:PWIKzi6JCp7sM0k9yZ43VX+T345uNbAkDKwHVjb2PTs=
go.opentelemetry.io/otel/exporters/otlp v0.20.0/go.mod h1:YIieizyaN77rtLJra0buKiNBOm9XQfkPEKBeuhoMwAM=
Expand Down Expand Up @@ -1611,6 +1624,7 @@ golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
Expand Down
23 changes: 11 additions & 12 deletions libs/middleware/rate_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (

appctx "github.com/brave-intl/bat-go/libs/context"
"github.com/brave-intl/bat-go/libs/logging"
"github.com/gomodule/redigo/redis"
"github.com/throttled/throttled"
"github.com/throttled/throttled/store/memstore"
"github.com/throttled/throttled/store/redigostore"
"github.com/redis/go-redis/v9"
"github.com/throttled/throttled/v2"
"github.com/throttled/throttled/v2/store/goredisstore.v9"
"github.com/throttled/throttled/v2/store/memstore"
)

// IPRateLimiterWithStore rate limits based on IP using
Expand All @@ -21,7 +21,7 @@ func IPRateLimiterWithStore(
ctx context.Context,
perMin int,
burst int,
store throttled.GCRAStore,
store throttled.GCRAStoreCtx,
) func(next http.Handler) http.Handler {
logger := logging.Logger(ctx, "middleware.IPRateLimiterWithStore")

Expand All @@ -30,12 +30,12 @@ func IPRateLimiterWithStore(
MaxRate: throttled.PerMin(perMin),
MaxBurst: burst,
}
rateLimiter, err := throttled.NewGCRARateLimiter(store, quota)
rateLimiter, err := throttled.NewGCRARateLimiterCtx(store, quota)
if err != nil {
logger.Fatal().Err(err)
}

httpRateLimiter := throttled.HTTPRateLimiter{
httpRateLimiter := throttled.HTTPRateLimiterCtx{
RateLimiter: rateLimiter,
VaryBy: &throttled.VaryBy{
RemoteAddr: true,
Expand Down Expand Up @@ -65,7 +65,7 @@ func IPRateLimiterWithStore(
// in-memory store that will not synchronize across instances.
func RateLimiter(ctx context.Context, perMin int) func(next http.Handler) http.Handler {
logger := logging.Logger(ctx, "middleware.RateLimiter")
store, err := memstore.New(65536)
store, err := memstore.NewCtx(65536)
if err != nil {
logger.Fatal().Err(err)
}
Expand All @@ -87,18 +87,17 @@ func RateLimiterRedisStore(
ctx context.Context,
perMin int,
burst int,
redis *redis.Pool,
rdb *redis.Client,
keyPrefix string,
db int,
) func(next http.Handler) http.Handler {
logger, err := appctx.GetLogger(ctx)
if err != nil {
_, logger = logging.SetupLogger(ctx)
}
store, err := redigostore.New(redis, keyPrefix, db)
store, err := goredisstore.NewCtx(rdb, keyPrefix)
if err != nil {
logger.Fatal().Err(err)
}

return IPRateLimiterWithStore(ctx, perMin, burst, store)
}

19 changes: 8 additions & 11 deletions libs/middleware/rate_limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package middleware

import (
"context"
"github.com/alicebob/miniredis/v2"
"github.com/gomodule/redigo/redis"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/alicebob/miniredis/v2"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
)

func TestRateLimiterMemoryMiddleware(t *testing.T) {
Expand Down Expand Up @@ -44,16 +45,12 @@ func TestRateLimiterRedisMiddleware(t *testing.T) {
limit := 60
burst := 2
mr, _ := miniredis.Run()
pool := &redis.Pool{
MaxIdle: 1,
IdleTimeout: 5000,
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", mr.Addr())
},
}
rdb := redis.NewClient(&redis.Options{
Addr: mr.Addr(),
})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
wrappedHandler := RateLimiterRedisStore(ctx, limit, burst, pool, "", 1)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
wrappedHandler := RateLimiterRedisStore(ctx, limit, burst, rdb, "")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
server := httptest.NewServer(wrappedHandler)
defer server.Close()

Expand Down
Loading

0 comments on commit 9db4ff3

Please sign in to comment.