quota

package
v0.2.0 Latest Latest
Warning

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

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

Documentation

Overview

Package quota 是 Quota 领域模型的原型验证(见 docs/plans/2026-07-16-Quota领域模型.md),不进正式代码。

它在 sqlite 共享文件上实现硬配额的核心原子操作: "检查当前窗口余额 + 扣减/预留"一条语句完成,窗口键用介质自己的钟 (sqlite 的共享介质是本机文件,strftime('%s','now') 就是介质钟)算, 不依赖任何应用进程的本地时间——这是模型第 2、4 节的落点。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Store

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

Store 一个进程视角的配额存储。介质是 sqlite 文件,多进程共享同一文件。

func Open

func Open(path string, periodSec int64, limit int, busyTimeoutMS int) (*Store, error)

Open 打开(不存在则建表)共享配额库。busyTimeoutMS 是本进程等锁的上限, 超时即视为"介质不可达",调用方按 fail-closed 处理。

func (*Store) Close

func (s *Store) Close() error

Close 关库。

func (*Store) DB

func (s *Store) DB() *sql.DB

DB 暴露底层连接,只给原型测试模拟"介质不可达"(长持写锁)用。

func (*Store) Release

func (s *Store) Release(ctx context.Context, qkey string, win int64) error

Release 尽力退还一份预留(认领扑空/出错的补偿)。失败不重试——当 leaked,方向保守。

func (*Store) Reserve

func (s *Store) Reserve(ctx context.Context, qkey string) (win int64, ok bool, err error)

Reserve 预留一份额度。返回值三态:

  • ok=true:预留成功,win 是本次预留落在的窗口起点(介质钟);
  • ok=false 且 err=nil:本窗口额度耗尽(不是错误,等下个窗口);
  • err≠nil:介质不可达等故障,调用方必须 fail-closed(零放行)。

Jump to

Keyboard shortcuts

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