retention

package
v1.64.1 Latest Latest
Warning

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

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

Documentation

Overview

Package retention decides which commits of a set a policy keeps.

Index

Constants

This section is empty.

Variables

View Source
var Default = Policy{KeepLast: 1, Brackets: []Bracket{
	{Period: Hourly, Count: 14 * 24},
	{Period: Daily, Count: 60},
	{Period: Weekly, Count: 12},
	{Period: Monthly},
}}

Default is the policy a store applies until it sets its own.

View Source
var ErrInvalidPolicy = errors.New("invalid retention policy")

ErrInvalidPolicy wraps every validation failure.

View Source
var KeepAll = Policy{KeepLast: 1, KeepWithin: time.Duration(math.MaxInt64)}

KeepAll is a policy that retires nothing.

Functions

This section is empty.

Types

type Bracket

type Bracket struct {
	Period Period
	Count  int
}

A Bracket keeps the last commit of each of Count UTC calendar periods. Brackets run consecutively back from now, each starting with the period the one before it ended in. A Count of zero is the tail: every period older than the brackets before it, kept forever.

func ParseBracket

func ParseBracket(s string) (Bracket, error)

ParseBracket reads a bracket as Bracket.String writes it.

func (Bracket) String

func (b Bracket) String() string

String is the bracket as <period>=<count>, or the period alone for the tail that keeps forever: "daily=60", "monthly".

type Commit

type Commit struct {
	ReceivedAt time.Time
	Partial    bool
	Pinned     bool
}

Commit is what evaluation needs to know about one commit of a set.

type Decision

type Decision struct {
	Keep    bool
	Reasons []string
}

Decision is the outcome for one commit: kept for the listed reasons, or retired.

func Evaluate

func Evaluate(commits []Commit, p Policy, now time.Time) []Decision

Evaluate applies the policy to a set's commits and returns one decision per commit, in input order. The newest commit is always kept. A partial commit is kept only while it is the newest; it counts for no rule. A pinned commit is kept and counts like any other. Commits within KeepWithin of now are kept regardless of counts.

func (Decision) RetainedBy

func (d Decision) RetainedBy() string

RetainedBy renders the reasons as the string stored on the commit row.

type Period

type Period string

Period is how finely a bracket keeps: one commit per hour, day, week or month of the stretch it covers.

const (
	Hourly  Period = "hourly"
	Daily   Period = "daily"
	Weekly  Period = "weekly"
	Monthly Period = "monthly"
)

The periods a bracket may keep by.

type Policy

type Policy struct {
	Brackets   []Bracket
	KeepLast   int
	KeepWithin time.Duration
}

Policy is what a set keeps. Brackets divide its past into consecutive stretches, each kept at its own granularity; KeepLast and KeepWithin hold on to the newest commits regardless of which bracket they fall in. The rules are ORed, and KeepLast is never below 1.

func Parse

func Parse(data []byte) (Policy, error)

Parse decodes and validates a JSON policy.

func (Policy) MarshalJSON

func (p Policy) MarshalJSON() ([]byte, error)

MarshalJSON writes keep_within as a duration string.

func (Policy) String

func (p Policy) String() string

String is the policy as the flags of goback set that set it: "keep-last=1 keep=daily=60 keep=monthly keep-within=24h0m0s".

func (*Policy) UnmarshalJSON

func (p *Policy) UnmarshalJSON(data []byte) error

UnmarshalJSON reads keep_within as a duration string. A policy written when periods were counted rather than bracketed is refused rather than read as the brackets it has none of, which would retire everything the counts were holding.

func (Policy) Validate

func (p Policy) Validate() error

Validate rejects a policy with a negative counter, a keep_last below 1 or nothing to keep at all.

func (Policy) Windows

func (p Policy) Windows(now, since time.Time) []Window

Windows lists the windows p keeps a commit of as of now, newest first: each period of each bracket, clipped to the bracket, and the tail's periods back to since. A window also keeps the last commit of any coarser period of a later bracket that ends in it.

type Window

type Window struct {
	Period   Period
	From, To time.Time
	Every    bool
}

A Window is one calendar period of a bracket, from From up to To (exclusive), whose last commit the policy keeps. Every marks a window within KeepWithin, which keeps all of its commits.

Jump to

Keyboard shortcuts

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