1// Copyright (C) 2019 Storj Labs, Inc. 2// See LICENSE for copying information. 3 4package live 5 6import ( 7 "context" 8 "strings" 9 "time" 10 11 "github.com/spacemonkeygo/monkit/v3" 12 "github.com/zeebo/errs" 13 "go.uber.org/zap" 14 15 "storj.io/storj/satellite/accounting" 16) 17 18var ( 19 // Error is the default error class for live-accounting. 20 Error = errs.Class("live-accounting") 21 mon = monkit.Package() 22) 23 24// Config contains configurable values for the live accounting service. 25type Config struct { 26 StorageBackend string `help:"what to use for storing real-time accounting data"` 27 BandwidthCacheTTL time.Duration `default:"5m" help:"bandwidth cache key time to live"` 28 AsOfSystemInterval time.Duration `default:"-10s" help:"as of system interval"` 29} 30 31// OpenCache creates a new accounting.Cache instance using the type specified backend in 32// the provided config. 33// 34// The cache instance may be returned despite of returning the 35// accounting.ErrSystemOrNetError because some backends allows to reconnect on 36// each operation if the connection was not established or it was disconnected, 37// which is what it could happen at the moment to instance it and the cache will 38// work one the backend system will be reachable later on. 39// For this reason, the components that uses the cache should operate despite 40// the backend is not responding successfully although their service is 41// degraded. 42func OpenCache(ctx context.Context, log *zap.Logger, config Config) (accounting.Cache, error) { 43 parts := strings.SplitN(config.StorageBackend, ":", 2) 44 var backendType string 45 if len(parts) == 0 || parts[0] == "" { 46 return nil, Error.New("please specify a backend for live accounting") 47 } 48 49 backendType = parts[0] 50 switch backendType { 51 case "redis": 52 return openRedisLiveAccounting(ctx, config.StorageBackend) 53 default: 54 return nil, Error.New("unrecognized live accounting backend specifier %q. Currently only redis is supported", backendType) 55 } 56} 57