2019-10-16 17:50:29 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package live
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
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 (
|
|
|
|
// Error is the default error class for live-accounting
|
|
|
|
Error = errs.Class("live-accounting")
|
|
|
|
mon = monkit.Package()
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config contains configurable values for the live accounting service.
|
|
|
|
type Config struct {
|
2019-10-31 17:27:38 +00:00
|
|
|
StorageBackend string `help:"what to use for storing real-time accounting data"`
|
2019-10-16 17:50:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCache creates a new accounting.Cache instance using the type specified backend in
|
|
|
|
// the provided config.
|
|
|
|
func NewCache(log *zap.Logger, config Config) (accounting.Cache, error) {
|
|
|
|
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":
|
|
|
|
return newRedisLiveAccounting(log, config.StorageBackend)
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|