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