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
- Variables
- func WithStore(ctx context.Context, s *Store) context.Context
- type Artifact
- type Backend
- type Content
- type ExpireOptions
- type ExpireReport
- type Filter
- type KindStats
- type Options
- type Policy
- type Rehydrator
- type SaveRequest
- type Store
- func (s *Store) Claim(ctx context.Context, kind string, refs []string, ownerKind, ownerID string) error
- func (s *Store) Delete(ctx context.Context, id uuid.UUID) error
- func (s *Store) Expire(ctx context.Context, opts ExpireOptions) (ExpireReport, error)
- func (s *Store) List(ctx context.Context, f Filter) ([]Artifact, error)
- func (s *Store) Open(ctx context.Context, id uuid.UUID) (*Artifact, io.ReadCloser, error)
- func (s *Store) OpenRef(ctx context.Context, kind, ref string) (*Artifact, io.ReadCloser, error)
- func (s *Store) Release(ctx context.Context, kind string, refs []string, at time.Time) error
- func (s *Store) Save(ctx context.Context, req SaveRequest) (*Artifact, error)
- func (s *Store) Stats(ctx context.Context) ([]KindStats, error)
- type StoreKey
- type TenantResolver
- type Upstream
Constants ¶
const ( DefaultInlineMax int64 = 64 << 10 DefaultTouchEvery = time.Hour )
const DefaultExpireBatch = 500
Variables ¶
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 ¶
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 "".
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 ExpireReport ¶
ExpireReport counts what Expire removed (or, with DryRun, would remove). Bytes is the uncompressed size of the removed content.
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 ¶
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 ¶
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 (*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 ¶
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 ¶
List returns the context tenant's artifacts, newest first, without their inline content.
func (*Store) Open ¶
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) Release ¶
Release makes the artifacts expire at the given time, with the same all-or-nothing rule as Claim.
type TenantResolver ¶
TenantResolver returns the tenant of a call from its context. It must return an error when the context carries none.