entitlements

package
v3.1.0 Latest Latest
Warning

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

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

Documentation

Overview

Package entitlements defines provider-neutral contracts and helpers for tenant plan, feature, and quota enforcement.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidCheck reports a malformed tenant, feature, or amount.
	ErrInvalidCheck = errors.New("invalid entitlement check")
	// ErrNilStore reports that no store was configured.
	ErrNilStore = errors.New("entitlement store is required")
)

Functions

func Middleware

func Middleware(service Service, extract TenantExtractor, feature Feature) func(http.Handler) http.Handler

Middleware enforces a feature before invoking the next HTTP handler.

Types

type Decision

type Decision struct {
	Allowed bool
	Feature Feature
	Reason  DecisionReason
	Used    int64
	Limit   int64
}

Decision is safe to send to logs or metrics because it excludes tenant IDs, customer IDs, subscription IDs, and other provider-owned identifiers.

type DecisionReason

type DecisionReason string

DecisionReason is a low-cardinality entitlement decision reason.

const (
	// ReasonAllowed means the feature or quota check passed.
	ReasonAllowed DecisionReason = "allowed"
	// ReasonFeatureDenied means the tenant plan does not include the feature.
	ReasonFeatureDenied DecisionReason = "feature_denied"
	// ReasonQuotaExceeded means usage crossed the configured quota limit.
	ReasonQuotaExceeded DecisionReason = "quota_exceeded"
)

type Feature

type Feature string

Feature names an application-owned capability or quota dimension.

type Observer

type Observer interface {
	ObserveEntitlementDecision(ctx context.Context, decision Decision)
}

Observer receives low-cardinality entitlement decisions.

type Plan

type Plan struct {
	ID       string
	Features map[Feature]bool
	Quotas   map[Feature]Quota
}

Plan describes the features and quotas assigned to a tenant.

func ClonePlan

func ClonePlan(plan Plan) Plan

ClonePlan returns a defensive copy of plan maps.

type Quota

type Quota struct {
	Limit int64
}

Quota describes an optional usage limit for a feature.

type Service

type Service struct {
	Store    Store
	Observer Observer
}

Service enforces plan features and quotas through a Store.

func (Service) Allowed

func (s Service) Allowed(ctx context.Context, tenantID string, feature Feature) (Decision, error)

Allowed checks whether the tenant plan includes feature.

func (Service) Consume

func (s Service) Consume(ctx context.Context, tenantID string, feature Feature, amount int64) (Decision, error)

Consume increments usage for feature and reports whether the tenant remains within the configured quota. Features without a positive quota are allowed when the plan includes the feature.

type Store

type Store interface {
	PlanForTenant(ctx context.Context, tenantID string) (Plan, error)
	IncrementUsage(ctx context.Context, tenantID string, feature Feature, amount int64) (Usage, error)
}

Store loads tenant plans and persists usage counters.

type TenantExtractor

type TenantExtractor func(*http.Request) (string, bool)

TenantExtractor returns the tenant ID for an HTTP request.

type Usage

type Usage struct {
	Used  int64
	Limit int64
}

Usage records usage after a quota increment.

Directories

Path Synopsis
Package entitlementstest provides reusable contract tests for entitlements stores.
Package entitlementstest provides reusable contract tests for entitlements stores.

Jump to

Keyboard shortcuts

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