feat(proxyd): redis namespace

This commit is contained in:
Felipe Andrade 2023-05-13 22:33:09 -07:00
parent 21284bd651
commit 29f7aa88ab
4 changed files with 18 additions and 7 deletions

@ -2,6 +2,7 @@ package proxyd
import ( import (
"context" "context"
"strings"
"time" "time"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
@ -43,16 +44,24 @@ func (c *cache) Put(ctx context.Context, key string, value string) error {
} }
type redisCache struct { type redisCache struct {
rdb *redis.Client rdb *redis.Client
prefix string
} }
func newRedisCache(rdb *redis.Client) *redisCache { func newRedisCache(rdb *redis.Client, prefix string) *redisCache {
return &redisCache{rdb} return &redisCache{rdb, prefix}
}
func (c *redisCache) namespaced(key string) string {
if c.prefix == "" {
return key
}
return strings.Join([]string{c.prefix, key}, ":")
} }
func (c *redisCache) Get(ctx context.Context, key string) (string, error) { func (c *redisCache) Get(ctx context.Context, key string) (string, error) {
start := time.Now() start := time.Now()
val, err := c.rdb.Get(ctx, key).Result() val, err := c.rdb.Get(ctx, c.namespaced(key)).Result()
redisCacheDurationSumm.WithLabelValues("GET").Observe(float64(time.Since(start).Milliseconds())) redisCacheDurationSumm.WithLabelValues("GET").Observe(float64(time.Since(start).Milliseconds()))
if err == redis.Nil { if err == redis.Nil {
@ -66,7 +75,7 @@ func (c *redisCache) Get(ctx context.Context, key string) (string, error) {
func (c *redisCache) Put(ctx context.Context, key string, value string) error { func (c *redisCache) Put(ctx context.Context, key string, value string) error {
start := time.Now() start := time.Now()
err := c.rdb.SetEX(ctx, key, value, redisTTL).Err() err := c.rdb.SetEX(ctx, c.namespaced(key), value, redisTTL).Err()
redisCacheDurationSumm.WithLabelValues("SETEX").Observe(float64(time.Since(start).Milliseconds())) redisCacheDurationSumm.WithLabelValues("SETEX").Observe(float64(time.Since(start).Milliseconds()))
if err != nil { if err != nil {

@ -32,7 +32,8 @@ type CacheConfig struct {
} }
type RedisConfig struct { type RedisConfig struct {
URL string `toml:"url"` URL string `toml:"url"`
Namespace string `toml:"namespace"`
} }
type MetricsConfig struct { type MetricsConfig struct {

@ -6,6 +6,7 @@ response_timeout_seconds = 1
[redis] [redis]
url = "$REDIS_URL" url = "$REDIS_URL"
namespace = "proxyd"
[cache] [cache]
enabled = true enabled = true

@ -236,7 +236,7 @@ func Start(config *Config) (*Server, func(), error) {
log.Warn("redis is not configured, using in-memory cache") log.Warn("redis is not configured, using in-memory cache")
cache = newMemoryCache() cache = newMemoryCache()
} else { } else {
cache = newRedisCache(redisClient) cache = newRedisCache(redisClient, config.Redis.Namespace)
} }
// Ideally, the BlocKSyncRPCURL should be the sequencer or a HA replica that's not far behind // Ideally, the BlocKSyncRPCURL should be the sequencer or a HA replica that's not far behind
ethClient, err := ethclient.Dial(blockSyncRPCURL) ethClient, err := ethclient.Dial(blockSyncRPCURL)