quota

package
v0.44.2 Latest Latest
Warning

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

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

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

Constants

This section is empty.

Variables

View Source
var ErrQuotaExceeded = errors.New("quota: exceeded")

ErrQuotaExceeded is returned by CheckCanWrite when the user is out of room.

View Source
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 (olivov H5, 2026-08-05).

Functions

This section is empty.

Types

type Service

type Service struct {
	Store db.Store
}

Service is the quota façade exposed to the rest of the codebase.

func New

func New(store db.Store) *Service

New constructs a Service.

func (*Service) AddUsage

func (s *Service) AddUsage(ctx context.Context, userID int64, bytes int64) error

AddUsage atomically grows usage_bytes by `bytes`.

func (*Service) CheckCanWrite

func (s *Service) CheckCanWrite(ctx context.Context, userID int64, addBytes int64) error

CheckCanWrite returns ErrQuotaExceeded when (used + addBytes) > quota. quota == 0 (unlimited) and userID <= 0 (anonymous / system) always pass.

func (*Service) Get

func (s *Service) Get(ctx context.Context, userID int64) (Snapshot, error)

Get returns the current snapshot. Returns ErrUserNotFound for an id that isn't a user.

func (*Service) Recompute

func (s *Service) Recompute(ctx context.Context, userID int64) (int64, error)

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.

func (*Service) SetQuota

func (s *Service) SetQuota(ctx context.Context, userID int64, bytes int64) error

SetQuota writes a new quota_bytes value (admin only — caller enforces). Returns ErrUserNotFound for an id that isn't a user, so an admin doesn't get a 200 for a write that landed nowhere.

func (*Service) SubUsage

func (s *Service) SubUsage(ctx context.Context, userID int64, bytes int64) error

SubUsage atomically shrinks usage_bytes by `bytes`. The DB layer clamps the result at zero.

type Snapshot

type Snapshot struct {
	UsedBytes   int64   `json:"used_bytes"`
	QuotaBytes  int64   `json:"quota_bytes"` // 0 == unlimited
	PercentUsed float64 `json:"percent_used"`
	Unlimited   bool    `json:"unlimited"`
}

Snapshot is the value returned by Get — used by the /me/quota handler.

Jump to

Keyboard shortcuts

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