billing

package
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrDailyBillingRebuildRunning = errors.New("daily billing rebuild already running")

Functions

func FindBillingLogRetentionDays added in v0.0.15

func FindBillingLogRetentionDays(ctx context.Context, db *gorm.DB) (int, error)

FindBillingLogRetentionDays reads the retention policy without hiding storage or validation failures. Only an absent row selects the documented default.

Types

type AutoDisableTriggerHandler added in v0.0.15

type AutoDisableTriggerHandler interface {
	DisableFromTriggers(context.Context, []attemptproxy.ChannelAutoDisableTrigger) error
}

type BillingLogRetentionDaysFinder added in v0.0.15

type BillingLogRetentionDaysFinder func(context.Context) (int, error)

BillingLogRetentionDaysFinder returns the validated live retention policy.

type DailyBillingBackfill added in v0.0.15

type DailyBillingBackfill struct {
	// contains filtered or unexported fields
}

func NewDailyBillingBackfill added in v0.0.15

func NewDailyBillingBackfill(
	app dao.AppProvider,
	runner *RebuildRunner,
	logger *zap.Logger,
	onCompleted func(context.Context) error,
) *DailyBillingBackfill

func (*DailyBillingBackfill) Close added in v0.0.15

func (c *DailyBillingBackfill) Close(ctx context.Context) error

func (*DailyBillingBackfill) Done added in v0.0.15

func (c *DailyBillingBackfill) Done() <-chan struct{}

func (*DailyBillingBackfill) ResourceCounts added in v0.0.15

func (c *DailyBillingBackfill) ResourceCounts() app.ResourceCounts

func (*DailyBillingBackfill) SetRebuilder added in v0.0.15

func (c *DailyBillingBackfill) SetRebuilder(rebuilder dao.LogDailyBillingRebuilder)

func (*DailyBillingBackfill) Start added in v0.0.15

func (c *DailyBillingBackfill) Start(ctx context.Context)

type JobStatus added in v0.0.2

type JobStatus string

JobStatus is the lifecycle state of a RebuildJob.

const (
	JobStatusRunning   JobStatus = "running"
	JobStatusSucceeded JobStatus = "succeeded"
	JobStatusFailed    JobStatus = "failed"
	JobStatusCanceled  JobStatus = "canceled"
)

type LimitEvaluator added in v0.0.6

type LimitEvaluator struct {
	App    dao.AppProvider
	Bus    app.EventBus
	Logger *zap.Logger
	// contains filtered or unexported fields
}

LimitEvaluator 周期评估所有配了限额的 admin channel,翻 Status + 写 LimitState。

func NewLimitEvaluator added in v0.0.6

func NewLimitEvaluator(application dao.AppProvider, bus app.EventBus, logger *zap.Logger, interval time.Duration) *LimitEvaluator

func (*LimitEvaluator) Close added in v0.0.13

func (e *LimitEvaluator) Close(ctx context.Context) error

func (*LimitEvaluator) Done added in v0.0.13

func (e *LimitEvaluator) Done() <-chan struct{}

func (*LimitEvaluator) ResourceCounts added in v0.0.13

func (e *LimitEvaluator) ResourceCounts() app.ResourceCounts

func (*LimitEvaluator) Start added in v0.0.6

func (e *LimitEvaluator) Start()

Start 起后台 ticker;每 tick 跑一轮 Tick(now)。

func (*LimitEvaluator) Stop added in v0.0.6

func (e *LimitEvaluator) Stop(ctx context.Context) error

func (*LimitEvaluator) Tick added in v0.0.6

func (e *LimitEvaluator) Tick(now time.Time) error

Tick 跑一轮评估。now 应为 UTC。

func (*LimitEvaluator) TickContext added in v0.0.13

func (e *LimitEvaluator) TickContext(ctx context.Context, now time.Time) error

type LogBatchQueue added in v0.0.14

type LogBatchQueue interface {
	Enqueue(logqueue.LogBatch) deliveryqueue.EnqueueResult
}

type LogRetentionWorker added in v0.0.15

type LogRetentionWorker struct {
	// contains filtered or unexported fields
}

LogRetentionWorker removes expired billing facts in small, cancelable batches.

func NewLogRetentionWorker added in v0.0.15

func NewLogRetentionWorker(db *gorm.DB, findRetentionDays BillingLogRetentionDaysFinder, logger *zap.Logger) *LogRetentionWorker

func (*LogRetentionWorker) Close added in v0.0.15

func (w *LogRetentionWorker) Close(ctx context.Context) error

func (*LogRetentionWorker) Done added in v0.0.15

func (w *LogRetentionWorker) Done() <-chan struct{}

func (*LogRetentionWorker) ResourceCounts added in v0.0.15

func (w *LogRetentionWorker) ResourceCounts() app.ResourceCounts

func (*LogRetentionWorker) RunOnce added in v0.0.15

func (w *LogRetentionWorker) RunOnce(ctx context.Context) error

RunOnce removes eligible billing logs with a forward keyset scan. Backdated rows inserted behind the current (created_at, id) cursor remain for the next RunOnce, which starts from an empty cursor.

func (*LogRetentionWorker) Start added in v0.0.15

func (w *LogRetentionWorker) Start(ctx context.Context)

Start runs one cleanup immediately, then repeats it once per interval.

type QuotaChecker

type QuotaChecker struct {
	App    dao.AppProvider
	Bus    app.EventBus
	Logger *zap.Logger
}

func NewQuotaChecker

func NewQuotaChecker(application dao.AppProvider, bus app.EventBus, logger *zap.Logger) *QuotaChecker

func (*QuotaChecker) Start

func (q *QuotaChecker) Start()

type RebuildJob added in v0.0.2

type RebuildJob struct {
	ID           string
	Filter       dao.BillingRebuildFilter
	Status       JobStatus
	DoneSlices   int64
	TotalSlices  int64
	ReplayedLogs int64
	StartedAt    int64
	FinishedAt   int64
	Error        string
	// contains filtered or unexported fields
}

RebuildJob is an in-memory record of one async rebuild request. Terminal jobs (succeeded/failed/canceled) are GC'd after retain duration.

func (*RebuildJob) Snapshot added in v0.0.2

func (j *RebuildJob) Snapshot() RebuildJob

Snapshot returns a synchronized copy of the job's mutable fields. Callers outside RebuildRunner must use this rather than reading j.Status directly (race-detector flags direct reads, since run() updates Status under j.mu and counters via sync/atomic).

type RebuildRunner added in v0.0.2

type RebuildRunner struct {
	// contains filtered or unexported fields
}

RebuildRunner schedules log-owned daily billing rebuilds in the background. Status lives in memory only — master restart drops all jobs (clients re-poll → 404 → retrigger). Terminal jobs are retained for `retain` duration before gc removes them.

func NewRebuildRunner added in v0.0.2

func NewRebuildRunner(app dao.AppProvider, logger *zap.Logger, retain time.Duration) *RebuildRunner

NewRebuildRunner constructs the runner and spawns a gc goroutine for terminal-job cleanup. Tests with a nil app must inject a daily rebuilder. retain controls how long terminal jobs stay visible via Get/List.

func (*RebuildRunner) Close added in v0.0.13

func (r *RebuildRunner) Close(ctx context.Context) error

Stop signals the runner to exit, cancels every in-flight job's context, and stops the gc loop. Idempotent + concurrent-safe via sync.Once. Returns after issuing cancel signals; in-flight goroutines mark themselves canceled at their next ctx check.

func (*RebuildRunner) Done added in v0.0.13

func (r *RebuildRunner) Done() <-chan struct{}

func (*RebuildRunner) Get added in v0.0.2

func (r *RebuildRunner) Get(id string) (*RebuildJob, bool)

Get returns the in-memory job by ID; ok=false if unknown or gc'd.

func (*RebuildRunner) List added in v0.0.2

func (r *RebuildRunner) List() []*RebuildJob

List returns a snapshot of all currently-tracked jobs (running + retained terminals).

func (*RebuildRunner) ResourceCounts added in v0.0.13

func (r *RebuildRunner) ResourceCounts() app.ResourceCounts

func (*RebuildRunner) SetDailyRebuilder added in v0.0.15

func (r *RebuildRunner) SetDailyRebuilder(rebuilder dao.LogDailyBillingRebuilder)

func (*RebuildRunner) SetSliceSleep added in v0.0.14

func (r *RebuildRunner) SetSliceSleep(d time.Duration)

SetSliceSleep configures an inter-slice pause applied after each successful date slice inside run(). Zero (the default) disables sleeping, so existing tests that don't call this keep running at full speed. Production wiring sets this from the admin-configurable rebuild_slice_sleep_ms setting so background rebuild replay trickles rather than competing with peak DB I/O. The sleep is implemented as a ctx-cancelable select in run(), not a bare time.Sleep, so job cancellation still takes effect promptly mid-sleep.

func (*RebuildRunner) Start added in v0.0.13

func (r *RebuildRunner) Start(ctx context.Context)

func (*RebuildRunner) Stop added in v0.0.2

func (r *RebuildRunner) Stop(ctx context.Context) error

func (*RebuildRunner) Submit added in v0.0.2

func (r *RebuildRunner) Submit(filter dao.BillingRebuildFilter) (*RebuildJob, error)

Submit discovers the actual request-log dates, fixes TotalSlices, then registers the job and starts its worker. It validates the date range and returns an error for empty, inverted, or unparseable input.

type Settler

type Settler struct {
	App          dao.AppProvider
	Bus          app.EventBus
	Logger       *zap.Logger
	LogQueue     LogBatchQueue
	AutoDisabler AutoDisableTriggerHandler
	// contains filtered or unexported fields
}

func NewCoreFactSettler added in v0.0.14

func NewCoreFactSettler(application dao.AppProvider, bus app.EventBus, logger *zap.Logger, queue LogBatchQueue) *Settler

NewCoreFactSettler is the split-schema settler activated together with the log worker and strict dual-database startup in Task 3B.

func NewSettler

func NewSettler(application dao.AppProvider, bus app.EventBus, logger *zap.Logger) *Settler

func (*Settler) Settle

func (s *Settler) Settle(ctx context.Context, agentID string, logs []protocol.UsageLogEntry)

Settle is the void, fire-and-forget wrapper used by the legacy ws usage.reported subscriber: per-entry failures are logged inside SettleBatch and swallowed here, matching the async publish semantics (agent already got its ack before this runs).

func (*Settler) SettleBatch added in v0.0.13

func (s *Settler) SettleBatch(ctx context.Context, agentID string, logs []protocol.UsageLogEntry) error

SettleBatch 逐条结算并返回聚合错误;成功条目照常落库/扣费,失败条目计入返回值。 供同步摄取路径(HTTP ingest)用:调用方以非 nil 返回决定 5xx,让 agent 重试整批; 已成功条目靠 request_id 去重天然幂等,重试不会双计。

func (*Settler) Start

func (s *Settler) Start()

Start subscribes to usage.reported events

Jump to

Keyboard shortcuts

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