storage

package
v0.252.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 24 Imported by: 0 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 storage keeps the raw bytes of messages the server has handled.

The database holds what a message was — who sent it, what happened to it — but not the message itself, because message bodies are large, are never queried, and would make the database expensive to back up. They live on disk instead, and optionally in an object store as well.

Two things need them. A delivery that failed and is waiting to be retried has to be able to reload the message it is delivering, possibly after a restart. And the dashboard cannot show a message it cannot read.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("storage: message not found")

ErrNotFound is returned when a message is not stored, which happens routinely: retention removes old messages, and a message received before storage was configured was never written.

Functions

func IsOutOfSpace added in v0.195.8

func IsOutOfSpace(err error) bool

IsOutOfSpace says whether a write failed because the disk or the object store has no room left: the directory's file system (ENOSPC), or an object store that answers 507 or names the error, as MinIO does when its drives are full. It is told apart from other failures so that a client is told to wait, rather than to try again at once, and is not told that the network is at fault.

Types

type FileStream added in v0.140.0

type FileStream struct {
	io.ReadCloser
	SizeBytes int64
}

FileStream is a bounded readable portion of a blob. The caller closes it.

type Files

type Files interface {
	// PutFile stores bytes under an identifier, overwriting any already
	// there.
	PutFile(ctx context.Context, id string, content []byte) error

	// GetFile returns stored bytes, or ErrNotFound.
	GetFile(ctx context.Context, id string) ([]byte, error)

	// DeleteFile removes them. Removing what is not there is not an error.
	DeleteFile(ctx context.Context, id string) error
}

Files stores opaque bytes, as distinct from messages.

A message has headers and a body and is parsed on the way in and out; a picture an operator uploaded is neither, and putting it through the message path would mean inventing headers for it and parsing them back. This is the same storage — the same directory, the same optional mirror, the same fallback from one to the other — with none of that in the way.

The lifetime is different too, and that is why the sweep does not touch these. A message is swept once it is older than the retention; a logo in a template is wanted for as long as the template is, which may be years.

type S3Settings

type S3Settings struct {
	Bucket string
	Region string

	// AccessKeyID and SecretAccessKey are credentials from the configuration
	// file. Empty means fall back to CredentialsFile, and then to the default
	// credential chain, so an instance role works.
	AccessKeyID     string
	SecretAccessKey string

	// CredentialsFile is an AWS shared credentials file.
	CredentialsFile string

	// Endpoint points at an S3-compatible service that is not AWS — MinIO,
	// Ceph, Garage, a bucket behind a proxy. Empty means AWS itself.
	//
	// Anything self-hosted needs this and PathStyle together: the
	// virtual-hosted addressing AWS prefers puts the bucket in the hostname,
	// which needs a wildcard DNS entry that a MinIO on the next machine
	// does not have.
	Endpoint string

	// PathStyle addresses a bucket as endpoint/bucket rather than
	// bucket.endpoint. Implied by Endpoint unless explicitly set otherwise.
	PathStyle bool
}

S3Settings describes the optional object store mirror.

type Settings

type Settings struct {
	// Mode is local or shared. Empty preserves existing configuration: a
	// directory selects local storage, otherwise the object store is required.
	Mode string

	// Directory is the local spool root. Empty in shared mode.
	Directory string

	// Retention is how long a message is kept before the sweep removes it.
	// Zero keeps messages forever, which will eventually fill the disk.
	Retention time.Duration

	// Keep says whether a message older than the retention is still wanted:
	// one a mailbox holds. Nil keeps nothing past the retention, which is
	// what the spool did before there were mailboxes.
	Keep func(ctx context.Context, id string) (bool, error)

	// TakeMirrorSweep says whether this instance sweeps the object store now,
	// given how often a sweep is wanted: true for one instance in each
	// interval, whichever it is, because every instance lists the whole bucket
	// and one sweep of it does for all. Nil means always, which is right for
	// one instance.
	TakeMirrorSweep func(ctx context.Context, interval time.Duration) (bool, error)

	// S3 mirrors messages to an object store as well. Optional; when nil no
	// AWS client is constructed.
	S3 *S3Settings
}

Settings describes where messages are kept.

type Storage

type Storage interface {
	// Put stores a message. Storing one that already exists overwrites it.
	Put(ctx context.Context, id string, headers []string, body []byte) error

	// Get returns a stored message, or ErrNotFound.
	Get(ctx context.Context, id string) ([]string, []byte, error)

	// Delete removes a message. Removing one that is not there is not an
	// error.
	Delete(ctx context.Context, id string) error

	// Files are stored here too: the same directory and the same mirror, with
	// none of the message parsing in the way.
	Files

	Close() error
}

Storage keeps raw messages, keyed by the identifier of the mail row they belong to.

func Open

func Open(settings *Settings) (Storage, error)

Open returns storage keeping messages in a directory, in an object store, or in both.

With both, the directory is the record and the object store a mirror: a write that reaches the disk has succeeded whatever the network is doing. With an object store alone there is no local copy at all, which is what several instances sharing one store want -- a directory then holds only what the instance that happened to handle a message wrote, which is a cache wearing a spool's name. The cost is that the store stops being a mirror and becomes the thing that has to answer: a write that cannot reach it fails, where before it was a warning.

type StreamFiles added in v0.140.0

type StreamFiles interface {
	PutFileStream(context.Context, string, io.ReadSeeker, int64) error
	OpenFileRange(context.Context, string, int64, int64) (*FileStream, error)
	DeleteFileEverywhere(context.Context, string) error
}

StreamFiles extends the existing blob store without buffering whole media. Writers own their seekable source; storage rewinds it before writing or retrying.

Jump to

Keyboard shortcuts

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