2020-09-09 20:20:44 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package accounting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2021-08-12 17:26:43 +01:00
|
|
|
"storj.io/common/lrucache"
|
2020-09-09 20:20:44 +01:00
|
|
|
"storj.io/common/memory"
|
|
|
|
"storj.io/common/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrProjectLimitType error for project limit type.
|
2021-04-28 09:06:17 +01:00
|
|
|
ErrProjectLimitType = errs.Class("project limit type")
|
2020-09-09 20:20:44 +01:00
|
|
|
// ErrGetProjectLimit error for getting project limits from database.
|
2021-04-28 09:06:17 +01:00
|
|
|
ErrGetProjectLimit = errs.Class("get project limits")
|
2020-09-09 20:20:44 +01:00
|
|
|
// ErrGetProjectLimitCache error for getting project limits from cache.
|
2021-04-28 09:06:17 +01:00
|
|
|
ErrGetProjectLimitCache = errs.Class("get project limits from cache")
|
2020-09-09 20:20:44 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// ProjectLimitDB stores information about projects limits for storage and bandwidth limits.
|
|
|
|
//
|
|
|
|
// architecture: Database
|
|
|
|
type ProjectLimitDB interface {
|
|
|
|
// GetProjectLimits returns current project limit for both storage and bandwidth.
|
|
|
|
GetProjectLimits(ctx context.Context, projectID uuid.UUID) (ProjectLimits, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProjectLimitConfig is a configuration struct for project limit.
|
|
|
|
type ProjectLimitConfig struct {
|
|
|
|
CacheCapacity int `help:"number of projects to cache." releaseDefault:"10000" devDefault:"100"`
|
|
|
|
CacheExpiration time.Duration `help:"how long to cache the project limits." releaseDefault:"10m" devDefault:"30s"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProjectLimitCache stores the values for both storage usage limit and bandwidth limit for
|
|
|
|
// each project ID if they differ from the default limits.
|
|
|
|
type ProjectLimitCache struct {
|
|
|
|
projectLimitDB ProjectLimitDB
|
|
|
|
defaultMaxUsage memory.Size
|
|
|
|
defaultMaxBandwidth memory.Size
|
2021-12-03 15:06:20 +00:00
|
|
|
defaultMaxSegments int64
|
2020-09-09 20:20:44 +01:00
|
|
|
|
|
|
|
state *lrucache.ExpiringLRU
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewProjectLimitCache creates a new project limit cache to store the project limits for each project ID.
|
2021-12-03 15:06:20 +00:00
|
|
|
func NewProjectLimitCache(db ProjectLimitDB, defaultMaxUsage, defaultMaxBandwidth memory.Size, defaultMaxSegments int64, config ProjectLimitConfig) *ProjectLimitCache {
|
2020-09-09 20:20:44 +01:00
|
|
|
return &ProjectLimitCache{
|
|
|
|
projectLimitDB: db,
|
|
|
|
defaultMaxUsage: defaultMaxUsage,
|
|
|
|
defaultMaxBandwidth: defaultMaxBandwidth,
|
2021-12-03 15:06:20 +00:00
|
|
|
defaultMaxSegments: defaultMaxSegments,
|
2020-09-09 20:20:44 +01:00
|
|
|
state: lrucache.New(lrucache.Options{
|
|
|
|
Capacity: config.CacheCapacity,
|
|
|
|
Expiration: config.CacheExpiration,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-10 09:37:50 +00:00
|
|
|
// GetLimits returns the project limits from cache.
|
|
|
|
func (c *ProjectLimitCache) GetLimits(ctx context.Context, projectID uuid.UUID) (ProjectLimits, error) {
|
|
|
|
fn := func() (interface{}, error) {
|
|
|
|
return c.getProjectLimits(ctx, projectID)
|
|
|
|
}
|
|
|
|
projectLimits, err := c.state.Get(projectID.String(), fn)
|
|
|
|
if err != nil {
|
|
|
|
return ProjectLimits{}, ErrGetProjectLimitCache.Wrap(err)
|
|
|
|
}
|
|
|
|
limits, ok := projectLimits.(ProjectLimits)
|
|
|
|
if !ok {
|
|
|
|
return ProjectLimits{}, ErrProjectLimitType.New("cache Get error")
|
|
|
|
}
|
|
|
|
return limits, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBandwidthLimit return the bandwidth usage limit for a project ID.
|
|
|
|
func (c *ProjectLimitCache) GetBandwidthLimit(ctx context.Context, projectID uuid.UUID) (_ memory.Size, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
projectLimits, err := c.GetLimits(ctx, projectID)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if projectLimits.Bandwidth == nil {
|
|
|
|
return c.defaultMaxBandwidth, nil
|
|
|
|
}
|
|
|
|
return memory.Size(*projectLimits.Bandwidth), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getProjectLimits returns project limits from DB.
|
|
|
|
func (c *ProjectLimitCache) getProjectLimits(ctx context.Context, projectID uuid.UUID) (_ ProjectLimits, err error) {
|
2020-09-09 20:20:44 +01:00
|
|
|
defer mon.Task()(&ctx, projectID)(&err)
|
|
|
|
|
|
|
|
projectLimits, err := c.projectLimitDB.GetProjectLimits(ctx, projectID)
|
|
|
|
if err != nil {
|
|
|
|
return ProjectLimits{}, ErrGetProjectLimit.Wrap(err)
|
|
|
|
}
|
|
|
|
if projectLimits.Bandwidth == nil {
|
|
|
|
defaultBandwidth := c.defaultMaxBandwidth.Int64()
|
|
|
|
projectLimits.Bandwidth = &defaultBandwidth
|
|
|
|
}
|
|
|
|
if projectLimits.Usage == nil {
|
|
|
|
defaultUsage := c.defaultMaxUsage.Int64()
|
|
|
|
projectLimits.Usage = &defaultUsage
|
|
|
|
}
|
2023-02-15 17:54:22 +00:00
|
|
|
if projectLimits.Segments == nil {
|
|
|
|
defaultSegments := c.defaultMaxSegments
|
|
|
|
projectLimits.Segments = &defaultSegments
|
|
|
|
}
|
|
|
|
if projectLimits.Segments == nil {
|
|
|
|
defaultSegments := c.defaultMaxSegments
|
|
|
|
projectLimits.Segments = &defaultSegments
|
|
|
|
}
|
2020-09-09 20:20:44 +01:00
|
|
|
|
|
|
|
return projectLimits, nil
|
|
|
|
}
|