lifetime

package
v0.0.0-...-4b0e997 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2021 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package lifetime provides specification of overridable TTLs.

Index

Constants

View Source
const (
	// MaxDuration is the longest duration representable with time.Duration.
	// This can be used to create a TTL that is effectively infinite.
	MaxDuration time.Duration = 1<<63 - 1
)

Variables

View Source
var (
	// Zero is the canonical zero-duration lifetime. It should be returned
	// instead of nil when Lifetime is used as a value time. Note a
	// zero-duration lifetime can still be useful for indicating the creation
	// time of a value, which is why New doesn't return this value if the
	// duration is 0. Using this value in a comparison to check for a zero
	// duration is likely a bug.
	Zero = Lifetime{}
)

Functions

This section is empty.

Types

type Lifetime

type Lifetime struct {

	// Created is when the value was produced or retrieved in UTC.
	Created time.Time

	// TTL is the duration after Created for which the value is valid.
	TTL time.Duration
}

Lifetime describes when a value was created, and the time from that instant that it expires. This should be used as an immutable value type.

We represent lifetimes as a time of creation, plus a duration (which is the literal TTL). This allows finding out when a value was created from its TTL, without having to embed it in the value. We require this at a library level for implementing TTL overrides.

func New

func New(duration time.Duration) Lifetime

New creates a new lifetime, starting now, lasting for the specified duration.

func (Lifetime) Cap

func (l Lifetime) Cap(duration time.Duration) Lifetime

Cap imposes an upper limit on the lifetime from the point of creation. If the existing TTL is shorter than the cap, the original lifetime is returned.

func (Lifetime) Expired

func (l Lifetime) Expired() bool

Expired returns true if the lifetime has passed, or false if it is still valid.

func (Lifetime) Expires

func (l Lifetime) Expires() time.Time

Expires returns when the lifetime becomes invalid in UTC.

func (Lifetime) Remaining

func (l Lifetime) Remaining() time.Duration

Remaining returns the time left before the lifetime expires. This will be negative if the lifetime has expired.

Jump to

Keyboard shortcuts

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