Skip to content

Commit

Permalink
Update Dependencies - Part 3 + Final (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
SyntaxNode authored May 3, 2022
1 parent 93f018f commit 87d895a
Show file tree
Hide file tree
Showing 10 changed files with 426 additions and 117 deletions.
6 changes: 3 additions & 3 deletions backends/aerospike_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func TestAerospikeClientGet(t *testing.T) {
aerospikeBackend.client = tt.inAerospikeClient

// Run test
actualValue, actualErr := aerospikeBackend.Get(context.TODO(), "defaultKey")
actualValue, actualErr := aerospikeBackend.Get(context.Background(), "defaultKey")

// Assertions
assert.Equal(t, tt.expectedValue, actualValue, tt.desc)
Expand Down Expand Up @@ -269,7 +269,7 @@ func TestClientPut(t *testing.T) {
aerospikeBackend.client = tt.inAerospikeClient

// Run test
actualErr := aerospikeBackend.Put(context.TODO(), tt.inKey, tt.inValueToStore, 0)
actualErr := aerospikeBackend.Put(context.Background(), tt.inKey, tt.inValueToStore, 0)

// Assert Put error
if tt.expectedErrorMsg != "" {
Expand All @@ -278,7 +278,7 @@ func TestClientPut(t *testing.T) {
assert.Nil(t, actualErr, tt.desc)

// Assert Put() sucessfully logged "not default value" under "testKey":
storedValue, getErr := aerospikeBackend.Get(context.TODO(), tt.inKey)
storedValue, getErr := aerospikeBackend.Get(context.Background(), tt.inKey)

assert.Nil(t, getErr, tt.desc)
assert.Equal(t, tt.inValueToStore, storedValue, tt.desc)
Expand Down
6 changes: 3 additions & 3 deletions backends/cassandra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestCassandraClientGet(t *testing.T) {
cassandraBackend.client = tt.in.cassandraClient

// Run test
actualValue, actualErr := cassandraBackend.Get(context.TODO(), tt.in.key)
actualValue, actualErr := cassandraBackend.Get(context.Background(), tt.in.key)

// Assertions
assert.Equal(t, tt.expected.value, actualValue, tt.desc)
Expand Down Expand Up @@ -155,14 +155,14 @@ func TestCassandraClientPut(t *testing.T) {
cassandraBackend.client = tt.in.cassandraClient

// Run test
actualErr := cassandraBackend.Put(context.TODO(), tt.in.key, tt.in.valueToStore, tt.in.ttl)
actualErr := cassandraBackend.Put(context.Background(), tt.in.key, tt.in.valueToStore, tt.in.ttl)

// Assert Put error
assert.Equal(t, tt.expected.err, actualErr, tt.desc)

// Assert value
if tt.expected.err == nil {
storedValue, getErr := cassandraBackend.Get(context.TODO(), tt.in.key)
storedValue, getErr := cassandraBackend.Get(context.Background(), tt.in.key)

assert.NoError(t, getErr, tt.desc)
assert.Equal(t, tt.expected.value, storedValue, tt.desc)
Expand Down
8 changes: 7 additions & 1 deletion backends/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package config

import (
"context"
"time"

log "github.com/sirupsen/logrus"

"github.com/prebid/prebid-cache/backends"
Expand Down Expand Up @@ -39,6 +42,9 @@ func applyCompression(cfg config.Compression, backend backends.Backend) backends
}

func newBaseBackend(cfg config.Backend, appMetrics *metrics.Metrics) backends.Backend {
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()

switch cfg.Type {
case config.BackendCassandra:
return backends.NewCassandraBackend(cfg.Cassandra)
Expand All @@ -49,7 +55,7 @@ func newBaseBackend(cfg config.Backend, appMetrics *metrics.Metrics) backends.Ba
case config.BackendAerospike:
return backends.NewAerospikeBackend(cfg.Aerospike, appMetrics)
case config.BackendRedis:
return backends.NewRedisBackend(cfg.Redis)
return backends.NewRedisBackend(cfg.Redis, ctx)
default:
log.Fatalf("Unknown backend type: %s", cfg.Type)
}
Expand Down
6 changes: 3 additions & 3 deletions backends/memcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestMemcacheGet(t *testing.T) {
mcBackend.memcache = tt.in.memcacheClient

// Run test
actualValue, actualErr := mcBackend.Get(context.TODO(), tt.in.key)
actualValue, actualErr := mcBackend.Get(context.Background(), tt.in.key)

// Assertions
assert.Equal(t, tt.expected.value, actualValue, tt.desc)
Expand Down Expand Up @@ -143,14 +143,14 @@ func TestMemcachePut(t *testing.T) {
mcBackend.memcache = tt.in.memcacheClient

// Run test
actualErr := mcBackend.Put(context.TODO(), tt.in.key, tt.in.valueToStore, tt.in.ttl)
actualErr := mcBackend.Put(context.Background(), tt.in.key, tt.in.valueToStore, tt.in.ttl)

// Assert Put error
assert.Equal(t, tt.expected.err, actualErr, tt.desc)

// Assert value
if tt.expected.err == nil {
storedValue, getErr := mcBackend.Get(context.TODO(), tt.in.key)
storedValue, getErr := mcBackend.Get(context.Background(), tt.in.key)

assert.NoError(t, getErr, tt.desc)
assert.Equal(t, tt.expected.value, storedValue, tt.desc)
Expand Down
14 changes: 7 additions & 7 deletions backends/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestMemoryBackend(t *testing.T) {
backend: NewMemoryBackend(),
setup: func(b *MemoryBackend) {},
run: func(b *MemoryBackend) (string, error) {
err := b.Put(context.TODO(), "someKey", "someValye", 0)
err := b.Put(context.Background(), "someKey", "someValye", 0)
return "", err
},
expected: testExpectedValues{err: nil},
Expand All @@ -43,10 +43,10 @@ func TestMemoryBackend(t *testing.T) {
desc: "Put returns a RecordExistsError",
backend: NewMemoryBackend(),
setup: func(b *MemoryBackend) {
b.Put(context.TODO(), "someKey", "someValue", 0)
b.Put(context.Background(), "someKey", "someValue", 0)
},
run: func(b *MemoryBackend) (string, error) {
err := b.Put(context.TODO(), "someKey", "someValye", 0)
err := b.Put(context.Background(), "someKey", "someValye", 0)
return "", err
},
expected: testExpectedValues{"", utils.NewPBCError(utils.RECORD_EXISTS)},
Expand All @@ -60,21 +60,21 @@ func TestMemoryBackend(t *testing.T) {
desc: "succesful get",
backend: NewMemoryBackend(),
setup: func(b *MemoryBackend) {
b.Put(context.TODO(), "someKey", "someValue", 0)
b.Put(context.Background(), "someKey", "someValue", 0)
},
run: func(b *MemoryBackend) (string, error) {
return b.Get(context.TODO(), "someKey")
return b.Get(context.Background(), "someKey")
},
expected: testExpectedValues{"someValue", nil},
},
{
desc: "Get returns a Key not found error",
backend: NewMemoryBackend(),
setup: func(b *MemoryBackend) {
b.Put(context.TODO(), "someKey", "someValue", 0)
b.Put(context.Background(), "someKey", "someValue", 0)
},
run: func(b *MemoryBackend) (string, error) {
return b.Get(context.TODO(), "anotherKey")
return b.Get(context.Background(), "anotherKey")
},
expected: testExpectedValues{"", utils.NewPBCError(utils.KEY_NOT_FOUND)},
},
Expand Down
22 changes: 11 additions & 11 deletions backends/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strconv"
"time"

"github.com/go-redis/redis"
"github.com/go-redis/redis/v8"
"github.com/prebid/prebid-cache/config"
"github.com/prebid/prebid-cache/utils"
log "github.com/sirupsen/logrus"
Expand All @@ -16,8 +16,8 @@ import (
// Redis database. Its implementation is intended to use the "github.com/go-redis/redis"
// client
type RedisDB interface {
Get(key string) (string, error)
Put(key string, value string, ttlSeconds int) (bool, error)
Get(ctx context.Context, key string) (string, error)
Put(ctx context.Context, key string, value string, ttlSeconds int) (bool, error)
}

// RedisDBClient is a wrapper for the Redis client that implements
Expand All @@ -27,15 +27,15 @@ type RedisDBClient struct {
}

// Get returns the value associated with the provided `key` parameter
func (db RedisDBClient) Get(key string) (string, error) {
return db.client.Get(key).Result()
func (db RedisDBClient) Get(ctx context.Context, key string) (string, error) {
return db.client.Get(ctx, key).Result()
}

// Put will set 'key' to hold string 'value' if 'key' does not exist in the redis storage.
// When key already holds a value, no operation is performed. That's the reason this adapter
// uses the 'github.com/go-redis/redis's library SetNX. SetNX is short for "SET if Not eXists".
func (db RedisDBClient) Put(key string, value string, ttlSeconds int) (bool, error) {
return db.client.SetNX(key, value, time.Duration(ttlSeconds)*time.Second).Result()
func (db RedisDBClient) Put(ctx context.Context, key, value string, ttlSeconds int) (bool, error) {
return db.client.SetNX(ctx, key, value, time.Duration(ttlSeconds)*time.Second).Result()
}

// RedisBackend when initialized will instantiate and configure the Redis client. It implements
Expand All @@ -46,7 +46,7 @@ type RedisBackend struct {
}

// NewRedisBackend initializes the redis client and pings to make sure connection was successful
func NewRedisBackend(cfg config.Redis) *RedisBackend {
func NewRedisBackend(cfg config.Redis, ctx context.Context) *RedisBackend {
constr := cfg.Host + ":" + strconv.Itoa(cfg.Port)

options := &redis.Options{
Expand All @@ -68,7 +68,7 @@ func NewRedisBackend(cfg config.Redis) *RedisBackend {

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

_, err := redisClient.client.Ping().Result()
_, err := redisClient.client.Ping(ctx).Result()

if err != nil {
log.Fatalf("Error creating Redis backend: %v", err)
Expand All @@ -87,7 +87,7 @@ func NewRedisBackend(cfg config.Redis) *RedisBackend {
// parameter and interprets its response. A `Nil` error reply of the Redis client means
// the `key` does not exist.
func (b *RedisBackend) Get(ctx context.Context, key string) (string, error) {
res, err := b.client.Get(key)
res, err := b.client.Get(ctx, key)

if err == redis.Nil {
err = utils.NewPBCError(utils.KEY_NOT_FOUND)
Expand All @@ -101,7 +101,7 @@ func (b *RedisBackend) Get(ctx context.Context, key string) (string, error) {
// not being written because the `key` already holds a value, and a RecordExistsError is returned
func (b *RedisBackend) Put(ctx context.Context, key string, value string, ttlSeconds int) error {

success, err := b.client.Put(key, value, ttlSeconds)
success, err := b.client.Put(ctx, key, value, ttlSeconds)
if !success {
return utils.NewPBCError(utils.RECORD_EXISTS)
}
Expand Down
20 changes: 10 additions & 10 deletions backends/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"
"testing"

"github.com/go-redis/redis"
"github.com/go-redis/redis/v8"
"github.com/prebid/prebid-cache/utils"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -67,7 +67,7 @@ func TestRedisClientGet(t *testing.T) {
redisBackend.client = tt.in.redisClient

// Run test
actualValue, actualErr := redisBackend.Get(context.TODO(), tt.in.key)
actualValue, actualErr := redisBackend.Get(context.Background(), tt.in.key)

// Assertions
assert.Equal(t, tt.expected.value, actualValue, tt.desc)
Expand Down Expand Up @@ -154,49 +154,49 @@ func TestRedisClientPut(t *testing.T) {
redisBackend.client = tt.in.redisClient

// Run test
actualErr := redisBackend.Put(context.TODO(), tt.in.key, tt.in.valueToStore, tt.in.ttl)
actualErr := redisBackend.Put(context.Background(), tt.in.key, tt.in.valueToStore, tt.in.ttl)

// Assert Put error
assert.Equal(t, tt.expected.err, actualErr, tt.desc)

// Assert value
if tt.expected.err == nil {
storedValue, getErr := redisBackend.Get(context.TODO(), tt.in.key)
storedValue, getErr := redisBackend.Get(context.Background(), tt.in.key)

assert.NoError(t, getErr, tt.desc)
assert.Equal(t, tt.expected.value, storedValue, tt.desc)
}
}
}

// Redis client that always throws an error
// errorProneRedisClient always throws an error
type errorProneRedisClient struct {
success bool
errorToThrow error
}

func (ec *errorProneRedisClient) Get(key string) (string, error) {
func (ec *errorProneRedisClient) Get(ctx context.Context, key string) (string, error) {
return "", ec.errorToThrow
}

func (ec *errorProneRedisClient) Put(key string, value string, ttlSeconds int) (bool, error) {
func (ec *errorProneRedisClient) Put(ctx context.Context, key string, value string, ttlSeconds int) (bool, error) {
return ec.success, ec.errorToThrow
}

// Redis client client that does not throw errors
// goodRedisClient does not throw errors
type goodRedisClient struct {
key string
value string
}

func (gc *goodRedisClient) Get(key string) (string, error) {
func (gc *goodRedisClient) Get(ctx context.Context, key string) (string, error) {
if key == gc.key {
return gc.value, nil
}
return "", utils.NewPBCError(utils.KEY_NOT_FOUND)
}

func (gc *goodRedisClient) Put(key string, value string, ttlSeconds int) (bool, error) {
func (gc *goodRedisClient) Put(ctx context.Context, key string, value string, ttlSeconds int) (bool, error) {
if gc.key != key {
gc.key = key
}
Expand Down
6 changes: 3 additions & 3 deletions endpoints/put_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1041,9 +1041,9 @@ func benchmarkPutHandler(b *testing.B, testCase string) {
func newMockBackend() *backends.MemoryBackend {
backend := backends.NewMemoryBackend()

backend.Put(context.TODO(), "non-36-char-key-maps-to-json", `json{"field":"value"}`, 0)
backend.Put(context.TODO(), "36-char-key-maps-to-non-xml-nor-json", `#@!*{"desc":"data got malformed and is not prefixed with 'xml' nor 'json' substring"}`, 0)
backend.Put(context.TODO(), "36-char-key-maps-to-actual-xml-value", "xml<tag>xml data here</tag>", 0)
backend.Put(context.Background(), "non-36-char-key-maps-to-json", `json{"field":"value"}`, 0)
backend.Put(context.Background(), "36-char-key-maps-to-non-xml-nor-json", `#@!*{"desc":"data got malformed and is not prefixed with 'xml' nor 'json' substring"}`, 0)
backend.Put(context.Background(), "36-char-key-maps-to-actual-xml-value", "xml<tag>xml data here</tag>", 0)

return backend
}
Expand Down
18 changes: 3 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,21 @@ module github.com/prebid/prebid-cache
go 1.16

require (
github.com/BurntSushi/toml v1.0.0 // indirect
github.com/aerospike/aerospike-client-go v4.0.0+incompatible
github.com/bitly/go-hostpool v0.1.0 // indirect
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/didip/tollbooth/v6 v6.1.2
github.com/go-redis/redis v6.12.1-0.20180718122851-ee41b9092371+incompatible
github.com/gocql/gocql v0.0.0-20180617115710-e06f8c1bcd78
github.com/go-redis/redis/v8 v8.11.5
github.com/gocql/gocql v1.0.0
github.com/gofrs/uuid v4.2.0+incompatible
github.com/golang/snappy v0.0.4
github.com/google/gomemcache v0.0.0-20210709172713-c1c93e4523ee
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce // indirect
github.com/julienschmidt/httprouter v1.3.0
github.com/magiconair/properties v1.8.0 // indirect
github.com/mitchellh/mapstructure v0.0.0-20180715050151-f15292f7a699 // indirect
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.18.1 // indirect
github.com/pelletier/go-toml v1.2.0 // indirect
github.com/prometheus/client_golang v1.5.1
github.com/prometheus/client_model v0.2.0
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475
github.com/rs/cors v1.8.2
github.com/sirupsen/logrus v1.4.2
github.com/spf13/afero v1.1.1 // indirect
github.com/spf13/cast v1.2.0 // indirect
github.com/spf13/jwalterweatherman v0.0.0-20180109140146-7c0cea34c8ec // indirect
github.com/spf13/pflag v1.0.1 // indirect
github.com/spf13/viper v1.0.2
github.com/spf13/viper v1.11.0
github.com/stretchr/testify v1.7.1
github.com/vrischmann/go-metrics-influxdb v0.1.1
github.com/yuin/gopher-lua v0.0.0-20180630135845-46796da1b0b4 // indirect
Expand Down
Loading

0 comments on commit 87d895a

Please sign in to comment.