Documentation
¶
Overview ¶
Package quota tracks per-user storage usage and enforces the users.quota_bytes ceiling at upload time.
quota_bytes == 0 means "unlimited". usage_bytes is incremented atomically in the DB on successful uploads and decremented on deletes; a periodic Recompute() job rebuilds it from authoritative node sizes.
Index ¶
- Variables
- type Service
- func (s *Service) AddUsage(ctx context.Context, userID int64, bytes int64) error
- func (s *Service) CheckCanWrite(ctx context.Context, userID int64, addBytes int64) error
- func (s *Service) CheckFile(ctx context.Context, userID int64, fileSize, addBytes int64) error
- func (s *Service) CheckFileSize(ctx context.Context, userID int64, size int64) error
- func (s *Service) Get(ctx context.Context, userID int64) (Snapshot, error)
- func (s *Service) Recompute(ctx context.Context, userID int64) (int64, error)
- func (s *Service) SetQuota(ctx context.Context, userID int64, bytes int64) error
- func (s *Service) SubUsage(ctx context.Context, userID int64, bytes int64) error
- type Snapshot
Constants ¶
This section is empty.
Variables ¶
var ErrFileTooLarge = fmt.Errorf("%w: the file is larger than your per-file upload limit", ErrQuotaExceeded)
ErrFileTooLarge is returned by CheckFile when one file is larger than the per-file limit a permission rule sets (package perm, max_upload_bytes). It wraps ErrQuotaExceeded on purpose: every write surface already turns that into its protocol's "over the limit" answer (507, QuotaExceeded, ENOSPC…), and that is the right class of answer — only the sentence differs.
var ErrQuotaExceeded = errors.New("quota: exceeded")
ErrQuotaExceeded is returned by CheckCanWrite when the user is out of room.
var ErrUserNotFound = errors.New("quota: user not found")
ErrUserNotFound means the id doesn't name a user.
usage_bytes and quota_bytes are COALESCE'd columns on `users`, so a user that exists always reads back (0, 0) = "unlimited, nothing used". A no-rows result therefore says nothing about quotas — it says there is no such user, and the caller should answer 404 rather than leak `sql: no rows in result set` as a 500 (a production report, 2026-08-05).
Functions ¶
This section is empty.
Types ¶
type Service ¶
Service is the quota façade exposed to the rest of the codebase.
func (*Service) CheckCanWrite ¶
CheckCanWrite returns ErrQuotaExceeded when (used + addBytes) > quota. quota == 0 (unlimited) and userID <= 0 (anonymous / system) always pass.
func (*Service) CheckFile ¶ added in v0.49.0
CheckFile is the check every single-file write makes: fileSize against the per-file upload limit of the rules binding userID, then addBytes (the bytes the write adds — usually the same number) against the quota.
func (*Service) CheckFileSize ¶ added in v0.49.0
CheckFileSize returns ErrFileTooLarge when size exceeds the per-file limit of the permission rules binding userID. No limit, anonymous and system writes, and administrators (bound by no rule) pass.
func (*Service) Get ¶
Get returns the current snapshot. Returns ErrUserNotFound for an id that isn't a user.
func (*Service) Recompute ¶
Recompute rebuilds usage_bytes from the SUM(size) of nodes owned by the user. Returns ErrUserNotFound for an id that isn't a user — the underlying UPDATE would otherwise touch no rows and report success.