Documentation
¶
Index ¶
- Variables
- type BillingProvider
- type Handler
- type InMemoryMeter
- func (m *InMemoryMeter) CheckLimit(_ context.Context, tenantID string) (bool, int64, error)
- func (m *InMemoryMeter) GetUsage(_ context.Context, tenantID string, period time.Time) (*UsageReport, error)
- func (m *InMemoryMeter) RecordExecution(_ context.Context, tenantID, pipelineName string) error
- func (m *InMemoryMeter) SetPlan(tenantID, planID string)
- type MockBillingProvider
- func (m *MockBillingProvider) CancelSubscription(_ context.Context, subscriptionID string) error
- func (m *MockBillingProvider) CreateCustomer(_ context.Context, tenantID, _ string) (string, error)
- func (m *MockBillingProvider) CreateSubscription(_ context.Context, customerID, planID string) (string, error)
- func (m *MockBillingProvider) HandleWebhook(_ context.Context, payload []byte, _ string) error
- func (m *MockBillingProvider) ReportUsage(_ context.Context, subscriptionID string, quantity int64) error
- type Plan
- type SQLiteMeter
- func (m *SQLiteMeter) CheckLimit(ctx context.Context, tenantID string) (bool, int64, error)
- func (m *SQLiteMeter) GetUsage(ctx context.Context, tenantID string, period time.Time) (*UsageReport, error)
- func (m *SQLiteMeter) RecordExecution(ctx context.Context, tenantID, pipelineName string) error
- func (m *SQLiteMeter) SetPlan(tenantID, planID string)
- type UsageEntry
- type UsageMeter
- type UsageReport
Constants ¶
This section is empty.
Variables ¶
var ( PlanFree = Plan{ ID: "free", Name: "Free", PriceMonthly: 0, ExecutionsPerMonth: 1000, MaxPipelines: 5, MaxStepsPerPipeline: 20, RetentionDays: 7, MaxWorkers: 2, } PlanStarter = Plan{ ID: "starter", Name: "Starter", PriceMonthly: 4900, ExecutionsPerMonth: 50_000, MaxPipelines: 25, MaxStepsPerPipeline: 50, RetentionDays: 30, MaxWorkers: 8, Features: []string{"email-support", "custom-domains"}, } PlanProfessional = Plan{ ID: "professional", Name: "Professional", PriceMonthly: 19900, ExecutionsPerMonth: 500_000, MaxPipelines: 0, MaxStepsPerPipeline: 0, RetentionDays: 90, MaxWorkers: 32, Features: []string{"email-support", "custom-domains", "priority-builds", "advanced-analytics"}, } PlanEnterprise = Plan{ ID: "enterprise", Name: "Enterprise", PriceMonthly: 0, ExecutionsPerMonth: 0, MaxPipelines: 0, MaxStepsPerPipeline: 0, RetentionDays: 365, MaxWorkers: 0, Features: []string{ "sso", "multi-region", "dedicated-infrastructure", "sla-guarantee", "priority-support", "custom-domains", "advanced-analytics", "audit-log-export", }, } // AllPlans is the ordered list of available plans. AllPlans = []Plan{PlanFree, PlanStarter, PlanProfessional, PlanEnterprise} )
Predefined billing plans.
Functions ¶
This section is empty.
Types ¶
type BillingProvider ¶
type BillingProvider interface {
// CreateCustomer registers a new billing customer for the given tenant.
CreateCustomer(ctx context.Context, tenantID, email string) (customerID string, err error)
// CreateSubscription starts a subscription for the customer on the given plan.
CreateSubscription(ctx context.Context, customerID, planID string) (subscriptionID string, err error)
// CancelSubscription cancels an active subscription.
CancelSubscription(ctx context.Context, subscriptionID string) error
// ReportUsage reports metered usage for a subscription.
ReportUsage(ctx context.Context, subscriptionID string, quantity int64) error
// HandleWebhook processes an incoming webhook payload from the payment provider.
HandleWebhook(ctx context.Context, payload []byte, signature string) error
}
BillingProvider abstracts payment and subscription management.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler exposes billing endpoints over HTTP.
func NewHandler ¶
func NewHandler(meter UsageMeter, provider BillingProvider) *Handler
NewHandler creates a new billing HTTP handler.
func (*Handler) RegisterRoutes ¶
RegisterRoutes registers billing endpoints on the given mux.
type InMemoryMeter ¶
type InMemoryMeter struct {
// contains filtered or unexported fields
}
InMemoryMeter is a thread-safe in-memory UsageMeter suitable for tests.
func NewInMemoryMeter ¶
func NewInMemoryMeter() *InMemoryMeter
NewInMemoryMeter creates an InMemoryMeter.
func (*InMemoryMeter) CheckLimit ¶
CheckLimit checks whether the tenant may run another execution.
func (*InMemoryMeter) GetUsage ¶
func (m *InMemoryMeter) GetUsage(_ context.Context, tenantID string, period time.Time) (*UsageReport, error)
GetUsage returns the usage report for the given period.
func (*InMemoryMeter) RecordExecution ¶
func (m *InMemoryMeter) RecordExecution(_ context.Context, tenantID, pipelineName string) error
RecordExecution records a single execution for the tenant.
func (*InMemoryMeter) SetPlan ¶
func (m *InMemoryMeter) SetPlan(tenantID, planID string)
SetPlan associates a tenant with a billing plan.
type MockBillingProvider ¶
type MockBillingProvider struct {
// Customers maps tenantID -> customerID.
Customers map[string]string
// Subscriptions maps subscriptionID -> planID.
Subscriptions map[string]string
// UsageReports collects (subscriptionID, quantity) pairs.
UsageReports []UsageEntry
// WebhookPayloads collects raw webhook bodies.
WebhookPayloads [][]byte
// Error fields allow tests to inject failures.
CreateCustomerErr error
CreateSubscriptionErr error
CancelSubscriptionErr error
ReportUsageErr error
HandleWebhookErr error
// contains filtered or unexported fields
}
MockBillingProvider is a test double that records calls and returns configurable results.
func NewMockBillingProvider ¶
func NewMockBillingProvider() *MockBillingProvider
NewMockBillingProvider creates a MockBillingProvider ready for use.
func (*MockBillingProvider) CancelSubscription ¶
func (m *MockBillingProvider) CancelSubscription(_ context.Context, subscriptionID string) error
CancelSubscription cancels a mock subscription.
func (*MockBillingProvider) CreateCustomer ¶
CreateCustomer creates a mock customer.
func (*MockBillingProvider) CreateSubscription ¶
func (m *MockBillingProvider) CreateSubscription(_ context.Context, customerID, planID string) (string, error)
CreateSubscription creates a mock subscription.
func (*MockBillingProvider) HandleWebhook ¶
HandleWebhook records the webhook payload.
func (*MockBillingProvider) ReportUsage ¶
func (m *MockBillingProvider) ReportUsage(_ context.Context, subscriptionID string, quantity int64) error
ReportUsage records a usage report.
type Plan ¶
type Plan struct {
ID string `json:"id"`
Name string `json:"name"`
PriceMonthly int `json:"price_monthly"` // cents
ExecutionsPerMonth int64 `json:"executions_per_month"` // 0 = unlimited
MaxPipelines int `json:"max_pipelines"` // 0 = unlimited
MaxStepsPerPipeline int `json:"max_steps_per_pipeline"` // 0 = unlimited
RetentionDays int `json:"retention_days"`
MaxWorkers int `json:"max_workers"`
Features []string `json:"features,omitempty"`
}
Plan represents a billing plan with usage limits.
func (Plan) IsUnlimited ¶
IsUnlimited reports whether the plan has no execution limit.
type SQLiteMeter ¶
type SQLiteMeter struct {
// contains filtered or unexported fields
}
SQLiteMeter is a UsageMeter backed by a SQLite database.
func NewSQLiteMeter ¶
func NewSQLiteMeter(db *sql.DB) (*SQLiteMeter, error)
NewSQLiteMeter creates a new SQLiteMeter and initialises the schema.
func (*SQLiteMeter) CheckLimit ¶
CheckLimit checks whether the tenant may run another execution.
func (*SQLiteMeter) GetUsage ¶
func (m *SQLiteMeter) GetUsage(ctx context.Context, tenantID string, period time.Time) (*UsageReport, error)
GetUsage returns the usage report for the given period from SQLite.
func (*SQLiteMeter) RecordExecution ¶
func (m *SQLiteMeter) RecordExecution(ctx context.Context, tenantID, pipelineName string) error
RecordExecution records an execution in SQLite.
func (*SQLiteMeter) SetPlan ¶
func (m *SQLiteMeter) SetPlan(tenantID, planID string)
SetPlan associates a tenant with a billing plan.
type UsageEntry ¶
UsageEntry records a single usage report.
type UsageMeter ¶
type UsageMeter interface {
// RecordExecution records a single pipeline execution for the tenant.
RecordExecution(ctx context.Context, tenantID, pipelineName string) error
// GetUsage returns the usage report for the given billing period.
GetUsage(ctx context.Context, tenantID string, period time.Time) (*UsageReport, error)
// CheckLimit checks whether the tenant is allowed to run another execution
// and returns the remaining executions in the current period.
CheckLimit(ctx context.Context, tenantID string) (allowed bool, remaining int64, err error)
}
UsageMeter tracks and queries resource consumption per tenant.
type UsageReport ¶
type UsageReport struct {
TenantID string `json:"tenant_id"`
Period time.Time `json:"period"` // first day of billing period
ExecutionCount int64 `json:"execution_count"`
PipelineCount int `json:"pipeline_count"`
StepCount int `json:"step_count"`
WorkerPeak int `json:"worker_peak"`
}
UsageReport summarizes resource usage for a tenant during a billing period.