Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ type ServerConfig struct {
}

type CacheConfig struct {
Enabled bool `toml:"enabled"`
TTL TOMLDuration `toml:"ttl"`
Enabled bool `toml:"enabled"`
UseInmemCache bool `toml:"use_inmem_cache"`
TTL TOMLDuration `toml:"ttl"`
}

type RedisConfig struct {
Expand Down
4 changes: 4 additions & 0 deletions frontend_rate_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ func NewRedisFrontendRateLimiter(r redis.UniversalClient, dur time.Duration, max
}

func (r *RedisFrontendRateLimiter) Take(ctx context.Context, key string) (bool, error) {
start := time.Now()

var incr *redis.IntCmd
truncTS := truncateNow(r.dur)
fullKey := fmt.Sprintf("rate_limit:%s:%s:%d", r.prefix, key, truncTS)
Expand All @@ -122,6 +124,8 @@ func (r *RedisFrontendRateLimiter) Take(ctx context.Context, key string) (bool,
return false, err
}

redisFrontendRateLimiterCacheDurationSumm.WithLabelValues("TAKE").Observe(float64(time.Since(start).Milliseconds()))

return incr.Val()-1 < int64(r.max), nil
}

Expand Down
7 changes: 7 additions & 0 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,13 @@ var (
Buckets: MillisecondDurationBuckets,
}, []string{"command"})

redisFrontendRateLimiterCacheDurationSumm = promauto.NewHistogramVec(prometheus.HistogramOpts{
Namespace: MetricsNamespace,
Name: "redis_frontend_rate_limiter_cache_duration_milliseconds",
Help: "Histogram of Redis Frontend Rate limiter durations, in milliseconds.",
Buckets: MillisecondDurationBuckets,
}, []string{"command"})

tooManyRequestErrorsTotal = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: MetricsNamespace,
Name: "too_many_request_errors_total",
Expand Down
23 changes: 14 additions & 9 deletions proxyd.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,18 +357,23 @@ func Start(config *Config) (*Server, func(), error) {
rpcCache RPCCache
)
if config.Cache.Enabled {
if redisClient == nil {
log.Warn("redis is not configured, using in-memory cache")
if config.Cache.UseInmemCache {
// enforce inmem cache for staticHandler methods
cache = newMemoryCache()
} else {
ttl := defaultCacheTtl
if config.Cache.TTL != 0 {
ttl = time.Duration(config.Cache.TTL)
}
cache = newRedisCache(redisClient, redisReadClient, config.Redis.Namespace, ttl)
if redisClient == nil {
log.Warn("redis is not configured, using in-memory cache")
cache = newMemoryCache()
} else {
ttl := defaultCacheTtl
if config.Cache.TTL != 0 {
ttl = time.Duration(config.Cache.TTL)
}
cache = newRedisCache(redisClient, redisReadClient, config.Redis.Namespace, ttl)

if config.Redis.FallbackToMemory {
cache = newFallbackCache(cache, newMemoryCache())
if config.Redis.FallbackToMemory {
cache = newFallbackCache(cache, newMemoryCache())
}
}
}
rpcCache = newRPCCache(newCacheWithCompression(cache))
Expand Down