credit

package
v1.0.0-beta.228 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 19, 2026 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const ErrCodeEffectiveAtMustBeSet models.ErrorCode = "grant_effective_at_must_be_set"
View Source
const ErrCodeGrantAmountMustBePositive models.ErrorCode = "grant_amount_must_be_positive"

Variables

View Source
var ErrGrantEffectiveAtMustBeSet = models.NewValidationIssue(
	ErrCodeEffectiveAtMustBeSet,
	"effective at must be set",
	models.WithFieldString("effectiveAt"),
	commonhttp.WithHTTPStatusCodeAttribute(http.StatusBadRequest),
)

Functions

func SortedPeriodsFromDedupedTimes

func SortedPeriodsFromDedupedTimes(ts []time.Time) []timeutil.ClosedPeriod

Returns a list of non-overlapping periods between the sorted times.

Types

type BalanceConnector

type BalanceConnector interface {
	// GetBalanceAt returns the result of the engine.Run at a given time.
	// It tries to minimize execution cost by calculating from the latest valid snapshot, thus the length of the returned history WILL NOT be deterministic.
	GetBalanceAt(ctx context.Context, ownerID models.NamespacedID, at time.Time) (engine.RunResult, error)
	// GetBalanceForPeriod returns the result of the engine.Run for the provided period.
	// The returned history will exactly match the provided period.
	GetBalanceForPeriod(ctx context.Context, ownerID models.NamespacedID, period timeutil.ClosedPeriod) (engine.RunResult, error)
	// ResetUsageForOwner resets the usage for an owner at a given time.
	ResetUsageForOwner(ctx context.Context, ownerID models.NamespacedID, params ResetUsageForOwnerParams) (balanceAfterReset *balance.Snapshot, err error)
	// GetLastValidSnapshotAt fetches the last valid snapshot for an owner.
	GetLastValidSnapshotAt(ctx context.Context, owner models.NamespacedID, at time.Time) (balance.Snapshot, error)
}

Generic connector for balance related operations.

type CreateGrantInput

type CreateGrantInput struct {
	Amount           float64
	Priority         uint8
	EffectiveAt      time.Time
	Expiration       *grant.ExpirationPeriod
	Metadata         map[string]string
	Annotations      models.Annotations
	ResetMaxRollover float64
	ResetMinRollover float64
	Recurrence       *timeutil.Recurrence
}

func (CreateGrantInput) Validate

func (i CreateGrantInput) Validate() error

type CreditConnector

type CreditConnector interface {
	BalanceConnector
	GrantConnector
}

func NewCreditConnector

func NewCreditConnector(
	cfg CreditConnectorConfig,
) CreditConnector

type CreditConnectorConfig

type CreditConnectorConfig struct {
	// services
	GrantRepo              grant.Repo
	BalanceSnapshotService balance.SnapshotService
	OwnerConnector         grant.OwnerConnector
	StreamingConnector     streaming.Connector
	Logger                 *slog.Logger
	Tracer                 trace.Tracer
	Publisher              eventbus.Publisher
	TransactionManager     transaction.Creator
	// configuration
	Granularity         time.Duration
	SnapshotGracePeriod datetime.ISODuration
}

type GrantConnector

type GrantConnector interface {
	CreateGrant(ctx context.Context, owner models.NamespacedID, grant CreateGrantInput) (*grant.Grant, error)
	VoidGrant(ctx context.Context, grantID models.NamespacedID, at *time.Time) error
}

type GrantNotFoundError

type GrantNotFoundError struct {
	GrantID string
}

func (*GrantNotFoundError) Error

func (e *GrantNotFoundError) Error() string

type ResetUsageForOwnerParams

type ResetUsageForOwnerParams struct {
	At              time.Time
	RetainAnchor    bool
	PreserveOverage bool
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL