Help us shape the future of Go! Take the 2026 Go Developer Survey

session

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package session is the upload session shared by the object-store blob backends (s3, gcs, azure; decision 0027). Object stores cannot append, so each backend buffers appended bytes into parts and uploads a part whenever one is full; between HTTP requests the session must survive in the metadata store so a Docker chunked upload or a CLI multipart upload resumes in a later request or another process.

A Session holds what every backend needs: the size, the declared digest, the saved blob.Hasher state and the pending tail shorter than one part, plus a backend-specific document D (S3's multipart upload ID and part list, GCS's part objects, Azure's block count). It is persisted through a Store after every append. InUploads keeps sessions in the uploads table under a per-store backend name such as "s3:hot", so two stores of one kind never share or misattribute sessions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FlushFunc

type FlushFunc func(ctx context.Context, part []byte) error

FlushFunc hands one part to the backend. On success the session drops the part from Pending; on error the bytes stay pending (and hashed), so the next Append or Commit retries the same part.

type Memory

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

Memory is a Store in process memory. Sessions do not survive a restart; it serves tests and embedders without a metadata store.

func NewMemory

func NewMemory() *Memory

NewMemory returns an empty Memory.

func (*Memory) Delete

func (m *Memory) Delete(_ context.Context, id string) error

Delete forgets a session.

func (*Memory) Get

func (m *Memory) Get(_ context.Context, id string) ([]byte, error)

Get returns a copy of the session.

func (*Memory) Put

func (m *Memory) Put(_ context.Context, id string, state []byte, _ int64) error

Put stores a copy of state.

type Session

type Session[D any] struct {
	// ID names the session for Resume.
	ID string
	// Size counts every appended byte, uploaded or pending.
	Size int64
	// Expected is the SHA-256 declared at BeginUpload, or "".
	Expected string
	// Pending is the tail not yet handed to the backend.
	Pending []byte
	// Data is the backend's state.
	Data D
	// contains filtered or unexported fields
}

Session is one upload session. D is the backend's own state; it is stored as JSON, so its fields carry json tags and are the storage format.

func Begin

func Begin[D any](ctx context.Context, st Store, opts ...blob.UploadOption) (*Session[D], error)

Begin starts a session with a new upload ID and saves it.

func Resume

func Resume[D any](ctx context.Context, st Store, id string) (*Session[D], error)

Resume loads a saved session. Unknown or malformed IDs, and sessions whose state cannot be restored, return an error wrapping blob.ErrUploadUnknown: there is no local copy of the uploaded parts to rehash, so a damaged session ends.

func (*Session[D]) Append

func (s *Session[D]) Append(
	ctx context.Context,
	r io.Reader,
	partSize int64,
	flush FlushFunc,
) (int64, error)

Append reads r into Pending, calling flush whenever partSize bytes are buffered, and saves the session. Bytes read are hashed and counted even when flush or the read fails, so the session stays consistent and the client may continue from Size.

func (*Session[D]) Flush

func (s *Session[D]) Flush(ctx context.Context, flush FlushFunc) error

Flush hands the pending bytes to flush when there are any and drops them on success.

func (*Session[D]) Forget

func (s *Session[D]) Forget(ctx context.Context) error

Forget removes the saved session.

func (*Session[D]) Live

func (s *Session[D]) Live(ctx context.Context) error

Live reports, with an error wrapping blob.ErrUploadUnknown, a session that was committed or aborted since this handle was resumed.

func (*Session[D]) Save

func (s *Session[D]) Save(ctx context.Context) error

Save persists the session.

func (*Session[D]) Verify

func (s *Session[D]) Verify(want blob.Digests) (blob.Digests, error)

Verify compares every non-empty field of want, with SHA256 defaulting to the declared digest, against the digests of everything appended, and returns those digests. A mismatch wraps blob.ErrDigestMismatch; the caller aborts the session.

type Store

type Store interface {
	Put(ctx context.Context, id string, state []byte, size int64) error
	Get(ctx context.Context, id string) ([]byte, error)
	Delete(ctx context.Context, id string) error
}

Store keeps encoded sessions between requests. Get returns an error wrapping blob.ErrUploadUnknown for an unknown ID; Delete of a missing session succeeds.

func InUploads

func InUploads(us store.UploadStore, backend string) Store

InUploads keeps sessions in the metadata store's uploads table (decision 0004) under backend, which names the blob store instance, for example "s3:hot" or "gcs:archive". A session stored under another backend name is unknown here, so the stale-upload reaper always knows which store aborts a session.

Jump to

Keyboard shortcuts

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