Skip to content

Commit

Permalink
Chill the defaults, we don't need that many connections
Browse files Browse the repository at this point in the history
  • Loading branch information
VojtechVitek committed Aug 8, 2024
1 parent 408208f commit c164784
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
6 changes: 3 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Config struct {
// Timeout for each Redis command after which we fall back to a local
// in-memory counter. If Redis does not respond within this duration,
// the system will use the local counter unless it is explicitly disabled.
FallbackTimeout time.Duration `toml:"fallback_timeout"` // default: 50ms
FallbackTimeout time.Duration `toml:"fallback_timeout"` // default: 100ms

// Client if supplied will be used and the below fields will be ignored.
//
Expand All @@ -31,6 +31,6 @@ type Config struct {
Port uint16 `toml:"port"`
Password string `toml:"password"` // optional
DBIndex int `toml:"db_index"` // default: 0
MaxIdle int `toml:"max_idle"` // default: 4
MaxActive int `toml:"max_active"` // default: 8
MaxIdle int `toml:"max_idle"` // default: 5
MaxActive int `toml:"max_active"` // default: 10
}
13 changes: 9 additions & 4 deletions httprateredis.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ func NewRedisLimitCounter(cfg *Config) (*redisCounter, error) {
cfg.PrefixKey = "httprate"
}
if cfg.FallbackTimeout == 0 {
// Activate local in-memory fallback fairly quickly, as this would slow down all requests.
cfg.FallbackTimeout = 100 * time.Millisecond
if cfg.FallbackDisabled {
cfg.FallbackTimeout = time.Second
} else {
// Activate local in-memory fallback fairly quickly,
// so we don't slow down incoming requests too much.
cfg.FallbackTimeout = 100 * time.Millisecond
}
}

rc := &redisCounter{
Expand All @@ -52,10 +57,10 @@ func NewRedisLimitCounter(cfg *Config) (*redisCounter, error) {
if cfg.Client == nil {
maxIdle, maxActive := cfg.MaxIdle, cfg.MaxActive
if maxIdle < 1 {
maxIdle = 20
maxIdle = 5
}
if maxActive < 1 {
maxActive = 50
maxActive = 10
}

rc.client = redis.NewClient(&redis.Options{
Expand Down
2 changes: 0 additions & 2 deletions httprateredis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ func BenchmarkLocalCounter(b *testing.B) {
limitCounter, err := httprateredis.NewRedisLimitCounter(&httprateredis.Config{
Host: "localhost",
Port: 6379,
MaxIdle: 100,
MaxActive: 200,
DBIndex: 0,
ClientName: "httprateredis_test",
PrefixKey: fmt.Sprintf("httprate:test:%v", rand.Int31n(100000)), // Unique key for each test
Expand Down

0 comments on commit c164784

Please sign in to comment.