Add Redis rate limiter

This commit is contained in:
longquan0104 2025-03-10 17:02:05 +07:00 committed by GitHub
parent c166a41c99
commit 550d96ea67
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
26 changed files with 2268 additions and 69 deletions

View file

@ -1,6 +1,7 @@
package integration
import (
"net"
"net/http"
"testing"
"time"
@ -12,7 +13,8 @@ import (
type RateLimitSuite struct {
BaseSuite
ServerIP string
ServerIP string
RedisEndpoint string
}
func TestRateLimitSuite(t *testing.T) {
@ -26,6 +28,7 @@ func (s *RateLimitSuite) SetupSuite() {
s.composeUp()
s.ServerIP = s.getComposeServiceIP("whoami1")
s.RedisEndpoint = net.JoinHostPort(s.getComposeServiceIP("redis"), "6379")
}
func (s *RateLimitSuite) TearDownSuite() {
@ -58,3 +61,34 @@ func (s *RateLimitSuite) TestSimpleConfiguration() {
s.T().Fatalf("requests throughput was too fast wrt to rate limiting: 100 requests in %v", elapsed)
}
}
func (s *RateLimitSuite) TestRedisRateLimitSimpleConfiguration() {
file := s.adaptFile("fixtures/ratelimit/simple_redis.toml", struct {
Server1 string
RedisEndpoint string
}{
Server1: s.ServerIP,
RedisEndpoint: s.RedisEndpoint,
})
s.traefikCmd(withConfigFile(file))
err := try.GetRequest("http://127.0.0.1:8080/api/rawdata", 1*time.Second, try.BodyContains("ratelimit", "redis"))
require.NoError(s.T(), err)
start := time.Now()
count := 0
for {
err = try.GetRequest("http://127.0.0.1:8081/", 500*time.Millisecond, try.StatusCodeIs(http.StatusOK))
require.NoError(s.T(), err)
count++
if count > 100 {
break
}
}
stop := time.Now()
elapsed := stop.Sub(start)
if elapsed < time.Second*99/100 {
s.T().Fatalf("requests throughput was too fast wrt to rate limiting: 100 requests in %v", elapsed)
}
}