artifact

package
v0.1.106 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: Apache-2.0 Imports: 23 Imported by: ? 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 artifact stores tenant-scoped files: small ones inline in Postgres, larger ones in a Backend, with expiry, tombstones and re-download from an upstream URL (rehydration).

Index

Constants

View Source
const (
	DefaultInlineMax  int64 = 64 << 10
	DefaultTouchEvery       = time.Hour
)
View Source
const DefaultExpireBatch = 500

Variables

View Source
var (
	// ErrNoStore is returned by Blobs when no store is attached to the context.
	ErrNoStore = errors.New("artifact: no store attached to context")
	// ErrNotFound means no artifact matches within the context tenant.
	ErrNotFound = errors.New("artifact: not found")
	// ErrGone means the artifact's blob was deleted and cannot be fetched again.
	ErrGone = errors.New("artifact: content is gone")
	// ErrTooLarge means a body exceeded SaveRequest.MaxSize.
	ErrTooLarge = errors.New("artifact: body exceeds the maximum size")
	// ErrBlobNotFound is what a Backend returns for a key it does not hold.
	ErrBlobNotFound = errors.New("artifact: blob not found")
	// ErrChanged means the artifact was saved again while its content was fetched from
	// its upstream; the newer content was kept, and opening it again serves it.
	ErrChanged = errors.New("artifact: changed while its content was fetched again")
	// ErrPathInUse means AdoptPath names a backend object another artifact already owns.
	ErrPathInUse = errors.New("artifact: path belongs to another artifact")
)

Functions

func WithStore

func WithStore(ctx context.Context, s *Store) context.Context

Types

type Artifact

type Artifact struct {
	ID             uuid.UUID  `json:"id" gorm:"primaryKey"`
	TenantID       uuid.UUID  `json:"tenant_id"`
	Kind           string     `json:"kind"`
	Ref            string     `json:"ref"`
	OwnerKind      string     `json:"owner_kind,omitempty"`
	OwnerID        string     `json:"owner_id,omitempty"`
	CreatedBy      string     `json:"created_by,omitempty"`
	Filename       string     `json:"filename,omitempty"`
	ContentType    string     `json:"content_type,omitempty"`
	Size           int64      `json:"size"`
	Checksum       string     `json:"checksum"`
	Path           string     `json:"-"`
	Content        []byte     `json:"-"`
	Compression    string     `json:"compression,omitempty"`
	UpstreamKind   string     `json:"upstream_kind,omitempty"`
	UpstreamURL    string     `json:"upstream_url,omitempty"`
	UpstreamETag   string     `json:"upstream_etag,omitempty" gorm:"column:upstream_etag"`
	CreatedAt      time.Time  `json:"created_at"`
	UpdatedAt      time.Time  `json:"updated_at"`
	LastAccessedAt time.Time  `json:"last_accessed_at"`
	ExpiresAt      *time.Time `json:"expires_at,omitempty"`
	BlobDeletedAt  *time.Time `json:"blob_deleted_at,omitempty"`
}

Artifact is a row of the artifacts table. Nullable text columns read as "".

func (Artifact) TableName

func (Artifact) TableName() string

type Backend

type Backend interface {
	// Read returns ErrBlobNotFound (wrapped or not) for a key it does not hold.
	Read(ctx context.Context, key string) (io.ReadCloser, error)
	Write(ctx context.Context, key string, body io.Reader) error
	// Delete removes key; a missing key is not an error.
	Delete(ctx context.Context, key string) error
}

Backend holds the blobs of artifacts too large to store inline.

func FromFilesystem

func FromFilesystem(filesystem fs.FilesystemRW) Backend

FromFilesystem adapts a commons-db filesystem. Only filesystems whose Read reports a missing path as fs.ErrNotExist (local, SFTP, SMB) map it to ErrBlobNotFound; any other Read error is returned as is.

type Content

type Content struct {
	Body        io.ReadCloser
	ContentType string
	ETag        string
}

Content is what a Rehydrator fetched. The store closes Body.

type ExpireOptions

type ExpireOptions struct {
	// Now defaults to the store's clock.
	Now      time.Time
	Policies map[string]Policy
	// Batch is the rows locked per transaction; 0 means DefaultExpireBatch.
	Batch  int
	DryRun bool
}

type ExpireReport

type ExpireReport struct {
	Tombstoned int
	Deleted    int
	Bytes      int64
}

ExpireReport counts what Expire removed (or, with DryRun, would remove). Bytes is the uncompressed size of the removed content.

type Filter

type Filter struct {
	Kind      string
	OwnerKind string
	OwnerID   string
	Limit     int
}

Filter narrows List; empty fields match everything and Limit 0 is unlimited.

type KindStats

type KindStats struct {
	Kind       string `json:"kind"`
	Count      int64  `json:"count"`
	Bytes      int64  `json:"bytes"`
	Tombstoned int64  `json:"tombstoned"`
	Expiring   int64  `json:"expiring"`
}

KindStats summarises one kind for the context tenant. Bytes counts only rows that still hold content.

type Options

type Options struct {
	DB      *gorm.DB
	Backend Backend
	Tenant  TenantResolver
	// WithTenant is Tenant's counterpart: it puts a tenant back on a context,
	// so rehydrators run under the artifact's tenant.
	WithTenant func(context.Context, uuid.UUID) context.Context
	// InlineMax is the largest body stored in the row; 0 means DefaultInlineMax.
	InlineMax   int64
	Rehydrators map[string]Rehydrator
	// Now defaults to time.Now.
	Now func() time.Time
	// TouchEvery throttles last_accessed_at updates; 0 means DefaultTouchEvery.
	TouchEvery time.Duration
}

type Policy

type Policy struct {
	IdleWindow time.Duration
}

Policy expires a kind's artifacts that nobody opened for IdleWindow. It applies only to artifacts without an owner: one that Claim (or a Save naming an owner) gave an owner is held by it, and expires only at its expires_at.

type Rehydrator

type Rehydrator interface {
	Fetch(ctx context.Context, a Artifact) (Content, error)
}

Rehydrator downloads an artifact's content again from its upstream. ctx carries the artifact's own tenant (Options.WithTenant), not the caller's.

type SaveRequest

type SaveRequest struct {
	Kind        string
	Ref         string
	Filename    string
	ContentType string
	OwnerKind   string
	OwnerID     string
	CreatedBy   string
	// Compress stores the content gzipped. With AdoptPath it declares that the
	// adopted object already is gzip.
	Compress  bool
	ExpiresAt *time.Time
	Upstream  *Upstream
	Body      io.Reader
	// AdoptPath registers an object already stored in the Backend at this key,
	// in place and without copying it. Body must be nil. The key must be canonical
	// and outside the store's own artifacts/ key space. One artifact owns each
	// object: adopting one another artifact owns fails with ErrPathInUse.
	AdoptPath string
	// MaxSize rejects bodies larger than this many bytes with ErrTooLarge; 0 is
	// unlimited.
	MaxSize int64
}

SaveRequest describes one artifact. Saving the same (Kind, Ref) again replaces every field and the content, keeping the artifact's ID.

type Store

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

func Blobs

func Blobs(ctx context.Context) (*Store, error)

Blobs returns the store attached to ctx, or ErrNoStore.

func New

func New(opts Options) (*Store, error)

func (*Store) Claim

func (s *Store) Claim(ctx context.Context, kind string, refs []string, ownerKind, ownerID string) error

Claim gives the artifacts an owner and stops them expiring. Every ref must exist in the context tenant, or nothing changes and ErrNotFound is returned. ownerKind is required: it is what exempts an artifact from idle expiry.

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, id uuid.UUID) error

Delete removes the artifact's row, then its blob. A blob that cannot be deleted is left behind and reported, never referenced by a row.

func (*Store) Expire

func (s *Store) Expire(ctx context.Context, opts ExpireOptions) (ExpireReport, error)

Expire removes the content of every tenant's artifacts that passed expires_at or sat idle beyond their kind's policy. It ignores the context tenant. Rows with an upstream URL become tombstones that Open can rehydrate, and all other rows are deleted; their blobs are deleted once that commits, so no kept row ever points at a deleted blob.

func (*Store) List

func (s *Store) List(ctx context.Context, f Filter) ([]Artifact, error)

List returns the context tenant's artifacts, newest first, without their inline content.

func (*Store) Open

func (s *Store) Open(ctx context.Context, id uuid.UUID) (*Artifact, io.ReadCloser, error)

Open returns the artifact with id in the context tenant and its content. A deleted or missing blob is fetched again by the Rehydrator registered for the artifact's upstream kind; without one Open fails with ErrGone.

func (*Store) OpenRef

func (s *Store) OpenRef(ctx context.Context, kind, ref string) (*Artifact, io.ReadCloser, error)

OpenRef is Open by the consumer's (kind, ref) handle.

func (*Store) Release

func (s *Store) Release(ctx context.Context, kind string, refs []string, at time.Time) error

Release makes the artifacts expire at the given time, with the same all-or-nothing rule as Claim.

func (*Store) Save

func (s *Store) Save(ctx context.Context, req SaveRequest) (*Artifact, error)

Save stores a body (or adopts an existing backend object) and upserts its row. A blob the upsert replaced is deleted after the commit; if that fails the saved artifact is returned together with the error.

func (*Store) Stats

func (s *Store) Stats(ctx context.Context) ([]KindStats, error)

Stats summarises the context tenant's artifacts per kind.

type StoreKey

type StoreKey struct{}

StoreKey is the context key a Store is attached under.

type TenantResolver

type TenantResolver func(context.Context) (uuid.UUID, error)

TenantResolver returns the tenant of a call from its context. It must return an error when the context carries none.

type Upstream

type Upstream struct {
	Kind string
	URL  string
	ETag string
}

Upstream records where an artifact's content can be downloaded again.

Directories

Path Synopsis
Package schema applies the artifacts table.
Package schema applies the artifacts table.

Jump to

Keyboard shortcuts

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