redis: Rename functions prefixed with New by Open

Rename the functions that are prefixed with 'New' which connect with
Redis by 'Open' to  make clear that they perform network operations.

Change-Id: I1351e89a642e8e2c2586626646315ad0fb2c6242
This commit is contained in:
Ivan Fraixedes 2021-03-24 20:22:50 +01:00 committed by Ivan Fraixedes
parent 903b1b5ffb
commit c5cb4dce4d
12 changed files with 24 additions and 24 deletions

View File

@ -74,7 +74,7 @@ func OpenDB(ctx context.Context, dbURL string, overwrite bool) (*DB, error) {
return nil, ErrDB.Wrap(err)
}
case "redis":
redisClient, err := redis.NewClientFrom(ctx, dbURL)
redisClient, err := redis.OpenClientFrom(ctx, dbURL)
if err != nil {
return nil, ErrDB.Wrap(err)
}

View File

@ -68,7 +68,7 @@ func cmdAPIRun(cmd *cobra.Command, args []string) (err error) {
err = errs.Combine(err, revocationDB.Close())
}()
accountingCache, err := live.NewCache(ctx, log.Named("live-accounting"), runCfg.LiveAccounting)
accountingCache, err := live.OpenCache(ctx, log.Named("live-accounting"), runCfg.LiveAccounting)
if err != nil {
if !accounting.ErrSystemOrNetError.Has(err) || accountingCache == nil {
return errs.New("Error instantiating live accounting cache: %w", err)

View File

@ -400,7 +400,7 @@ func cmdRun(cmd *cobra.Command, args []string) (err error) {
err = errs.Combine(err, revocationDB.Close())
}()
liveAccounting, err := live.NewCache(ctx, log.Named("live-accounting"), runCfg.LiveAccounting)
liveAccounting, err := live.OpenCache(ctx, log.Named("live-accounting"), runCfg.LiveAccounting)
if err != nil {
if !accounting.ErrSystemOrNetError.Has(err) || liveAccounting == nil {
return errs.New("Error instantiating live accounting cache: %w", err)

View File

@ -60,7 +60,7 @@ func openDBBolt(ctx context.Context, path string) (*DB, error) {
// openDBRedis creates a redis-backed DB.
func openDBRedis(ctx context.Context, address string) (*DB, error) {
client, err := redis.NewClientFrom(ctx, address)
client, err := redis.OpenClientFrom(ctx, address)
if err != nil {
return nil, err
}

View File

@ -634,7 +634,7 @@ func (planet *Planet) newSatellite(ctx context.Context, prefix string, index int
planet.databases = append(planet.databases, revocationDB)
liveAccounting, err := live.NewCache(ctx, log.Named("live-accounting"), config.LiveAccounting)
liveAccounting, err := live.OpenCache(ctx, log.Named("live-accounting"), config.LiveAccounting)
if err != nil {
return nil, errs.Wrap(err)
}
@ -765,7 +765,7 @@ func (planet *Planet) newAPI(ctx context.Context, index int, identity *identity.
}
planet.databases = append(planet.databases, revocationDB)
liveAccounting, err := live.NewCache(ctx, log.Named("live-accounting"), config.LiveAccounting)
liveAccounting, err := live.OpenCache(ctx, log.Named("live-accounting"), config.LiveAccounting)
if err != nil {
return nil, errs.Wrap(err)
}

View File

@ -27,7 +27,7 @@ type Config struct {
BandwidthCacheTTL time.Duration `default:"5m" help:"bandwidth cache key time to live"`
}
// NewCache creates a new accounting.Cache instance using the type specified backend in
// OpenCache creates a new accounting.Cache instance using the type specified backend in
// the provided config.
//
// The cache instance may be returned despite of returning the
@ -38,7 +38,7 @@ type Config struct {
// For this reason, the components that uses the cache should operate despite
// the backend is not responding successfully although their service is
// degraded.
func NewCache(ctx context.Context, log *zap.Logger, config Config) (accounting.Cache, error) {
func OpenCache(ctx context.Context, log *zap.Logger, config Config) (accounting.Cache, error) {
parts := strings.SplitN(config.StorageBackend, ":", 2)
var backendType string
if len(parts) == 0 || parts[0] == "" {
@ -48,7 +48,7 @@ func NewCache(ctx context.Context, log *zap.Logger, config Config) (accounting.C
backendType = parts[0]
switch backendType {
case "redis":
return newRedisLiveAccounting(ctx, config.StorageBackend)
return openRedisLiveAccounting(ctx, config.StorageBackend)
default:
return nil, Error.New("unrecognized live accounting backend specifier %q. Currently only redis is supported", backendType)
}

View File

@ -49,7 +49,7 @@ func TestAddGetProjectStorageAndBandwidthUsage(t *testing.T) {
}
}
cache, err := live.NewCache(ctx, zaptest.NewLogger(t).Named("live-accounting"), config)
cache, err := live.OpenCache(ctx, zaptest.NewLogger(t).Named("live-accounting"), config)
require.NoError(t, err)
defer ctx.Check(cache.Close)
@ -123,7 +123,7 @@ func TestGetAllProjectTotals(t *testing.T) {
}
}
cache, err := live.NewCache(ctx, zaptest.NewLogger(t).Named("live-accounting"), config)
cache, err := live.OpenCache(ctx, zaptest.NewLogger(t).Named("live-accounting"), config)
require.NoError(t, err)
defer ctx.Check(cache.Close)
@ -176,7 +176,7 @@ func TestLiveAccountingCache_ProjectBandwidthUsage_expiration(t *testing.T) {
}
}
cache, err := live.NewCache(ctx, zaptest.NewLogger(t).Named("live-accounting"), config)
cache, err := live.OpenCache(ctx, zaptest.NewLogger(t).Named("live-accounting"), config)
require.NoError(t, err)
defer ctx.Check(cache.Close)

View File

@ -22,7 +22,7 @@ type redisLiveAccounting struct {
client *redis.Client
}
// newRedisLiveAccounting returns a redisLiveAccounting cache instance.
// openRedisLiveAccounting returns a redisLiveAccounting cache instance.
//
// It returns accounting.ErrInvalidArgument if the connection address is invalid
// according to Redis.
@ -31,7 +31,7 @@ type redisLiveAccounting struct {
// it fails then it returns an instance and accounting.ErrSystemOrNetError
// because it means that Redis may not be operative at this precise moment but
// it may be in future method calls as it handles automatically reconnects.
func newRedisLiveAccounting(ctx context.Context, address string) (*redisLiveAccounting, error) {
func openRedisLiveAccounting(ctx context.Context, address string) (*redisLiveAccounting, error) {
redisurl, err := url.Parse(address)
if err != nil {
return nil, accounting.ErrInvalidArgument.New("address: invalid URL; %w", err)

View File

@ -63,7 +63,7 @@ func TestGraphqlMutation(t *testing.T) {
require.NoError(t, err)
defer ctx.Check(redis.Close)
cache, err := live.NewCache(ctx, log.Named("cache"), live.Config{StorageBackend: "redis://" + redis.Addr() + "?db=0"})
cache, err := live.OpenCache(ctx, log.Named("cache"), live.Config{StorageBackend: "redis://" + redis.Addr() + "?db=0"})
require.NoError(t, err)
projectLimitCache := accounting.NewProjectLimitCache(db.ProjectAccounting(), 0, 0, accounting.ProjectLimitConfig{CacheCapacity: 100})

View File

@ -47,7 +47,7 @@ func TestGraphqlQuery(t *testing.T) {
require.NoError(t, err)
defer ctx.Check(redis.Close)
cache, err := live.NewCache(ctx, log.Named("cache"), live.Config{StorageBackend: "redis://" + redis.Addr() + "?db=0"})
cache, err := live.OpenCache(ctx, log.Named("cache"), live.Config{StorageBackend: "redis://" + redis.Addr() + "?db=0"})
require.NoError(t, err)
projectLimitCache := accounting.NewProjectLimitCache(db.ProjectAccounting(), 0, 0, accounting.ProjectLimitConfig{CacheCapacity: 100})

View File

@ -38,8 +38,8 @@ type Client struct {
lookupLimit int
}
// NewClient returns a configured Client instance, verifying a successful connection to redis.
func NewClient(ctx context.Context, address, password string, db int) (*Client, error) {
// OpenClient returns a configured Client instance, verifying a successful connection to redis.
func OpenClient(ctx context.Context, address, password string, db int) (*Client, error) {
client := &Client{
db: redis.NewClient(&redis.Options{
Addr: address,
@ -58,8 +58,8 @@ func NewClient(ctx context.Context, address, password string, db int) (*Client,
return client, nil
}
// NewClientFrom returns a configured Client instance from a redis address, verifying a successful connection to redis.
func NewClientFrom(ctx context.Context, address string) (*Client, error) {
// OpenClientFrom returns a configured Client instance from a redis address, verifying a successful connection to redis.
func OpenClientFrom(ctx context.Context, address string) (*Client, error) {
redisurl, err := url.Parse(address)
if err != nil {
return nil, err
@ -76,7 +76,7 @@ func NewClientFrom(ctx context.Context, address string) (*Client, error) {
return nil, err
}
return NewClient(ctx, redisurl.Host, q.Get("password"), db)
return OpenClient(ctx, redisurl.Host, q.Get("password"), db)
}
// SetLookupLimit sets the lookup limit.

View File

@ -24,7 +24,7 @@ func TestSuite(t *testing.T) {
}
defer func() { require.NoError(t, redis.Close()) }()
client, err := NewClient(ctx, redis.Addr(), "", 1)
client, err := OpenClient(ctx, redis.Addr(), "", 1)
if err != nil {
t.Fatal(err)
}
@ -34,7 +34,7 @@ func TestSuite(t *testing.T) {
}
func TestInvalidConnection(t *testing.T) {
_, err := NewClient(context.Background(), "", "", 1)
_, err := OpenClient(context.Background(), "", "", 1)
if err == nil {
t.Fatal("expected connection error")
}
@ -49,7 +49,7 @@ func BenchmarkSuite(b *testing.B) {
}
defer func() { require.NoError(b, redis.Close()) }()
client, err := NewClient(ctx, redis.Addr(), "", 1)
client, err := OpenClient(ctx, redis.Addr(), "", 1)
if err != nil {
b.Fatal(err)
}