Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrinux committed Dec 12, 2023
1 parent 943f9ab commit 88d9616
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 81 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ This section does not describe permanent API contracts; it just describes limita

## Backend Configuration

Prebid Cache requires a backend data store which enforces TTL expiration. The following storage options are supported: Aerospike, Cassandra, Memcache, and Redis. You're welcomed to contribute a new backend adapter if needed.
Prebid Cache requires a backend data store which enforces TTL expiration. The following storage options are supported: Aerospike, Cassandra, Memcache, and Redis/Redis sentinel. You're welcomed to contribute a new backend adapter if needed.

There is also an option (enabled by default) for a basic in-memory data store intended only for development. This backend does not support TTL expiration and is not built for production use.

Expand Down Expand Up @@ -383,4 +383,4 @@ docker run -p 8000:8000 -t prebid-cache

### Profiling

[pprof stats](http://artem.krylysov.com/blog/2017/03/13/profiling-and-optimizing-go-web-applications/) can be accessed from a running app on the admin port `localhost:2525`.
[pprof stats](http://artem.krylysov.com/blog/2017/03/13/profiling-and-optimizing-go-web-applications/) can be accessed from a running app on the admin port `localhost:2525`.
2 changes: 2 additions & 0 deletions backends/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ func TestNewBaseBackend(t *testing.T) {
desc: "Redis",
inConfig: config.Backend{Type: config.BackendRedis},
expectedLogEntries: []logEntry{
{msg: "Error creating Redis backend: At least one Host[s] is required.", lvl: logrus.FatalLevel},
{msg: "Creating Redis backend", lvl: logrus.InfoLevel},
{msg: "Error creating Redis backend: ", lvl: logrus.FatalLevel},
},
},
Expand Down
16 changes: 11 additions & 5 deletions backends/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,17 @@ type RedisBackend struct {
func NewRedisBackend(cfg config.Redis, ctx context.Context) *RedisBackend {

if len(cfg.Hosts) < 1 || cfg.Host == "" {
log.Fatalf("At least one Host[s] is required.")
log.Fatalf("Error creating Redis backend: At least one Host[s] is required.")
}

var redisClient RedisDBClient

sentinel := false

if cfg.MasterName != "" {
log.Info("Creating Redis sentinel backend")
sentinel = true
// Mode failover (sentinel)
log.Info("Creating Redis sentinel backend")
options := &redis.FailoverOptions{
MasterName: cfg.MasterName,
SentinelAddrs: cfg.Hosts,
Expand All @@ -70,10 +73,9 @@ func NewRedisBackend(cfg config.Redis, ctx context.Context) *RedisBackend {
}

redisClient = RedisDBClient{client: redis.NewFailoverClient(options)}

} else {
log.Info("Creating Redis backend")
// Mode single
log.Info("Creating Redis backend")
constr := cfg.Host + ":" + strconv.Itoa(cfg.Port)
options := &redis.Options{
Addr: constr,
Expand All @@ -96,7 +98,11 @@ func NewRedisBackend(cfg config.Redis, ctx context.Context) *RedisBackend {
panic("RedisBackend failure. This shouldn't happen.")
}

log.Infof("Connected to Redis at %s:%d", cfg.Host, cfg.Port)
if sentinel {
log.Infof("Connected to Redis: %v", cfg.Hosts)
} else {
log.Infof("Connected to Redis: %s:%v", cfg.Host, cfg.Port)
}

return &RedisBackend{
cfg: cfg,
Expand Down
13 changes: 9 additions & 4 deletions config/backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ func (cfg *Memcache) validateAndLog() error {

type Redis struct {
Host string `mapstructure:"host"`
Hosts []string `mapstructure:"hosts"`
MasterName string `mapstructure:"mastername"`
Port int `mapstructure:"port"`
Password string `mapstructure:"password"`
Db int `mapstructure:"db"`
ExpirationMinutes int `mapstructure:"expiration"`
TLS RedisTLS `mapstructure:"tls"`
Hosts []string `mapstructure:"hosts"`
MasterName string `mapstructure:"mastername"`
}

type RedisTLS struct {
Expand All @@ -167,8 +167,13 @@ type RedisTLS struct {
}

func (cfg *Redis) validateAndLog() error {
log.Infof("config.backend.redis.host: %s", cfg.Host)
log.Infof("config.backend.redis.port: %d", cfg.Port)
if cfg.Host != "" && len(cfg.Hosts) > 0 {
log.Infof("config.backend.redis.hosts: %s. Note that redis host will be ignore if 'hosts' is define", cfg.Hosts)
log.Infof("config.backend.redis.mastername: %s.", cfg.MasterName)
} else {
log.Infof("config.backend.redis.host: %s", cfg.Host)
log.Infof("config.backend.redis.port: %d", cfg.Port)
}
log.Infof("config.backend.redis.db: %d", cfg.Db)
if cfg.ExpirationMinutes > 0 {
log.Infof("config.backend.redis.expiration: %d. Note that this configuration option is being deprecated in favor of config.request_limits.max_ttl_seconds", cfg.ExpirationMinutes)
Expand Down
9 changes: 7 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,7 @@ func getExpectedDefaultConfig() Configuration {
DefaultTTL: utils.CASSANDRA_DEFAULT_TTL_SECONDS,
},
Redis: Redis{
Hosts: []string{},
ExpirationMinutes: utils.REDIS_DEFAULT_EXPIRATION_MINUTES,
},
Ignite: Ignite{
Expand Down Expand Up @@ -1267,8 +1268,12 @@ func getExpectedFullConfigForTestFile() Configuration {
Hosts: []string{"10.0.0.1:11211", "127.0.0.1"},
},
Redis: Redis{
Host: "127.0.0.1",
Port: 6379,
Host: "127.0.0.1",
Port: 6379,
Hosts: []string{
"10.0.0.1:26379", "127.0.0.1",
},
MasterName: "mymaster",
Password: "redis-password",
Db: 1,
ExpirationMinutes: 1,
Expand Down
6 changes: 5 additions & 1 deletion config/configtest/sample_full_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ backend:
keyspace: "prebid"
default_ttl_seconds: 60
memcache:
hosts: ["10.0.0.1:11211","127.0.0.1"]
hosts: ["10.0.0.1:11211", "127.0.0.1"]
redis:
hosts:
- "10.0.0.1:26379"
- "127.0.0.1"
mastername: "mymaster"
host: "127.0.0.1"
port: 6379
password: "redis-password"
Expand Down
67 changes: 0 additions & 67 deletions config/configtest/sample_full_redis_sentinel_config.yaml

This file was deleted.

0 comments on commit 88d9616

Please sign in to comment.