policy

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package policy provides storage retention and cleanup policy management.

Index

Constants

This section is empty.

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 added in v1.1.3

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"`
}

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

func (*Config) Validate added in v1.1.3

func (p *Config) Validate() error

Validate checks if a policy configuration is valid.

type Metadata added in v1.1.3

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"`
}

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

type Option added in v1.3.10

type Option func(*TimeBasedPolicyHandler)

Option configures a TimeBasedPolicyHandler.

func WithAutoStart added in v1.3.10

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 added in v1.3.10

func WithBaseDir(baseDir string) Option

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

type TimeBasedPolicyHandler

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

TimeBasedPolicyHandler implements PolicyHandler 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 or TrackFile), enforcement uses the index to check only files whose expiry time has passed, avoiding a full directory walk on every tick.

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.PolicyHandler.ApplyPolicy

func (*TimeBasedPolicyHandler) BuildExpiryIndex added in v1.3.10

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.PolicyHandler.EnforcePolicy. When an expiry index has been built (via BuildExpiryIndex or TrackFile), this method only checks files whose expiry time has passed, avoiding a full directory walk. Otherwise it falls back to walking the directory tree.

func (*TimeBasedPolicyHandler) ExpiryIndexLen added in v1.3.10

func (h *TimeBasedPolicyHandler) ExpiryIndexLen() int

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

func (*TimeBasedPolicyHandler) RegisterPolicy

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

RegisterPolicy adds a policy configuration to the handler.

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 added in v1.3.10

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.

func (*TimeBasedPolicyHandler) UntrackFile added in v1.3.10

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