policy

package
v2.22.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package policy provides storage retention and cleanup policy management.

Index

Constants

View Source
const RetentionRule = "retention"

RetentionRule is the Rules key that sets a policy's retention: a Go duration string such as "90m" or "720h". A policy that carries it may have any name; one without it must have a name ParsePolicyName understands.

Variables

This section is empty.

Functions

func ParsePolicyName

func ParsePolicyName(name string) (string, time.Duration, error)

ParsePolicyName extracts policy type and parameters from a policy name. Supported formats:

  • "delete-after-Xmin" - Delete after X minutes
  • "retain-Xdays" - Retain for X days
  • "retain-Xhours" - Retain for X hours

Returns (policyType, duration, error)

Types

type Config

type Config struct {
	// Name is a unique identifier for this policy (e.g., "delete-after-5min", "retain-30days")
	Name string `json:"name" yaml:"name"`

	// Description provides human-readable documentation for this policy
	Description string `json:"description,omitempty" yaml:"description,omitempty"`

	// Rules contains policy-specific configuration (e.g., retention duration)
	Rules map[string]interface{} `json:"rules,omitempty" yaml:"rules,omitempty"`
}

Config defines a retention policy for media storage. Policies control how long media should be retained and when it should be deleted.

func (*Config) Retention added in v2.19.0

func (p *Config) Retention() (time.Duration, error)

Retention returns how long media under this policy is kept: the RetentionRule when the policy sets one, otherwise the duration its name encodes.

func (*Config) Validate

func (p *Config) Validate() error

Validate checks if a policy configuration is valid.

type Metadata

type Metadata struct {
	// PolicyName identifies the policy applied to this media
	PolicyName string `json:"policy_name"`

	// ExpiresAt is when this media should be deleted (nil = never expires)
	ExpiresAt *time.Time `json:"expires_at,omitempty"`

	// CreatedAt is when the policy was applied
	CreatedAt time.Time `json:"created_at"`
}

Metadata stores policy information in .meta files alongside media. This metadata is used by the enforcement system to determine when to delete files.

type Option

type Option func(*TimeBasedPolicyHandler)

Option configures a TimeBasedPolicyHandler.

func WithAutoStart

func WithAutoStart(autoStart bool) Option

WithAutoStart controls whether the background enforcement goroutine starts automatically when the handler is created. Requires WithBaseDir to be set. Default is false for backward compatibility with NewTimeBasedPolicyHandler.

func WithBaseDir

func WithBaseDir(baseDir string) Option

WithBaseDir sets the base directory for automatic policy enforcement. This is required when WithAutoStart(true) is used.

func WithClock added in v2.17.0

func WithClock(now func() time.Time) Option

WithClock sets the function the handler uses to read the current time when deciding whether media has expired. It defaults to time.Now; tests inject a fixed clock so expiry does not depend on the wall clock.

type TimeBasedPolicyHandler

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

TimeBasedPolicyHandler implements storage.MediaPolicyHandler for time-based retention policies. It applies expiration times to media based on policy names and enforces deletion of expired media through background scanning.

When an expiry index is built (via BuildExpiryIndex, which StartEnforcement calls), enforcement uses the index to check only files whose expiry time has passed, avoiding a full directory walk on every tick. TrackFile and TrackMedia keep a built index current but never build it themselves.

func NewTimeBasedPolicyHandler

func NewTimeBasedPolicyHandler(enforcementInterval time.Duration, opts ...Option) *TimeBasedPolicyHandler

NewTimeBasedPolicyHandler creates a new time-based policy handler. It accepts optional configuration via Option functions. To auto-start enforcement, use WithAutoStart(true) and WithBaseDir(dir).

func (*TimeBasedPolicyHandler) ApplyPolicy

func (h *TimeBasedPolicyHandler) ApplyPolicy(ctx context.Context, metadata *storage.MediaMetadata) error

ApplyPolicy implements storage.MediaPolicyHandler.ApplyPolicy. It returns an error when metadata names a policy that is neither registered nor parseable, so a store using the handler rejects media whose retention it could never enforce. An empty policy name is accepted.

func (*TimeBasedPolicyHandler) BeginMediaWrite added in v2.17.0

func (h *TimeBasedPolicyHandler) BeginMediaWrite(metaPath string) func()

BeginMediaWrite implements storage.MediaWriteGuard. Until the returned function is called, neither the index pass nor the directory walk deletes the item whose metadata file is at metaPath. It waits for an enforcement step already handling that path to finish, but not for steps handling other items. Overlapping writes to one path nest, and the returned function is safe to call more than once.

func (*TimeBasedPolicyHandler) BuildExpiryIndex

func (h *TimeBasedPolicyHandler) BuildExpiryIndex(baseDir string) error

BuildExpiryIndex scans the base directory once and builds an in-memory index of files with their expiry times. Subsequent calls to EnforcePolicy will use this index instead of walking the directory tree.

func (*TimeBasedPolicyHandler) EnforcePolicy

func (h *TimeBasedPolicyHandler) EnforcePolicy(ctx context.Context, baseDir string) error

EnforcePolicy implements storage.MediaPolicyHandler.EnforcePolicy. When an expiry index has been built by BuildExpiryIndex (which StartEnforcement calls), this method only checks files whose expiry time has passed, avoiding a full directory walk. Otherwise it walks baseDir, and also enforces any files recorded with TrackFile or TrackMedia, so they do not accumulate in the index.

func (*TimeBasedPolicyHandler) ExpiryIndexLen

func (h *TimeBasedPolicyHandler) ExpiryIndexLen() int

ExpiryIndexLen returns the number of entries in the expiry index. This is primarily useful for testing.

func (*TimeBasedPolicyHandler) MediaExpiresAt added in v2.17.0

func (h *TimeBasedPolicyHandler) MediaExpiresAt(metadata *storage.MediaMetadata) (time.Time, bool, error)

MediaExpiresAt implements storage.MediaExpiryReporter: media expires at its ExpiresAt when that is set, with or without a policy, and otherwise at its Timestamp plus its policy's duration. Media with neither never expires; a policy that is neither registered nor parseable is an error.

func (*TimeBasedPolicyHandler) RegisterPolicy

func (h *TimeBasedPolicyHandler) RegisterPolicy(policy Config) error

RegisterPolicy adds a policy configuration to the handler. Media whose PolicyName is a registered policy's name are kept for that policy's Retention, which lets a policy have a name of its own (with a RetentionRule) or override the duration a parseable name encodes. Registering a name again replaces the earlier policy.

func (*TimeBasedPolicyHandler) StartEnforcement

func (h *TimeBasedPolicyHandler) StartEnforcement(ctx context.Context, baseDir string)

StartEnforcement starts a background goroutine that periodically enforces policies. On the first call it builds an in-memory expiry index by scanning the base directory once, then uses that index on subsequent ticks to avoid repeated full directory walks.

func (*TimeBasedPolicyHandler) Stop

func (h *TimeBasedPolicyHandler) Stop()

Stop signals the enforcement goroutine to stop and waits for it to finish. It is safe to call Stop multiple times or when enforcement was never started.

func (*TimeBasedPolicyHandler) TrackFile

func (h *TimeBasedPolicyHandler) TrackFile(metaPath string, expiresAt time.Time)

TrackFile adds a file to the in-memory expiry index. This should be called when a new file with a retention policy is stored, to keep the index current without requiring a full directory scan. Tracking a file does not switch EnforcePolicy to index-only mode; only BuildExpiryIndex does.

func (*TimeBasedPolicyHandler) TrackMedia added in v2.17.0

func (h *TimeBasedPolicyHandler) TrackMedia(metaPath string, metadata *storage.MediaMetadata) error

TrackMedia implements storage.MediaPolicyTracker. It adds the media whose metadata file is at metaPath to the expiry index, expiring when MediaExpiresAt says, so media stored after StartEnforcement has built the index still expires. Media with no policy and no ExpiresAt is not tracked; a policy that is neither registered nor parseable is an error.

func (*TimeBasedPolicyHandler) UntrackFile

func (h *TimeBasedPolicyHandler) UntrackFile(metaPath string)

UntrackFile removes a file from the in-memory expiry index. This should be called when a file is explicitly deleted, to keep the index current.

Jump to

Keyboard shortcuts

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