backends

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AsyncBackendStats

type AsyncBackendStats struct {
	StartedPuts        int64
	SuccessPuts        int64
	FailedPuts         int64
	TotalPutTimeMicros int64
}

AsyncBackendStats holds statistics for the async backend writer

type AsyncBackendWriter

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

AsyncBackendWriter wraps a Backend and provides asynchronous PUT operations. GET operations are still synchronous as they're in the critical path for builds. PUT operations spawn a goroutine on demand.

func NewAsyncBackendWriter

func NewAsyncBackendWriter(
	backend Backend,
	logger *slog.Logger,
) *AsyncBackendWriter

func (*AsyncBackendWriter) Clear

func (abw *AsyncBackendWriter) Clear() error

Clear passes through to the underlying backend

func (*AsyncBackendWriter) Close

func (abw *AsyncBackendWriter) Close() error

Close gracefully shuts down the async writer and waits for all in-flight operations to complete. It also closes the underlying backend.

func (*AsyncBackendWriter) Get

func (abw *AsyncBackendWriter) Get(actionID []byte) (outputID []byte, body io.ReadCloser, size int64, putTime *time.Time, miss bool, err error)

Get passes through to the underlying backend (synchronous). GET operations remain synchronous as they're in the critical path.

func (*AsyncBackendWriter) Put

func (abw *AsyncBackendWriter) Put(actionID, outputID []byte, body io.Reader, bodySize int64) error

Put spawns a goroutine to execute the PUT operation asynchronously. The body is copied to avoid holding references to the original data.

func (*AsyncBackendWriter) Stats

Stats returns current statistics about the async writer

type Backend

type Backend interface {
	// Put stores an object in the backend storage.
	// actionID is the cache key, outputID is stored with the body,
	// body is the content to store, and bodySize is the size in bytes.
	// The backend stores the data in its storage system and returns nil on success.
	Put(actionID, outputID []byte, body io.Reader, bodySize int64) error

	// Get retrieves an object from the backend storage.
	// actionID is the cache key to look up.
	// Returns outputID, body (as io.ReadCloser), size, putTime, and whether it was a miss.
	// The caller is responsible for closing the returned ReadCloser.
	// On a cache miss, returns miss=true and body=nil.
	Get(actionID []byte) (outputID []byte, body io.ReadCloser, size int64, putTime *time.Time, miss bool, err error)

	// Close performs any cleanup operations needed by the backend.
	Close() error

	// Clear removes all entries from the cache backend storage.
	Clear() error
}

Backend defines the interface for cache storage backends.

Implementations can be swapped to use different storage mechanisms. The backend is responsible ONLY for storing/retrieving data from its storage system. The server (server.go) is responsible for managing the local disk cache that Go accesses.

Implementations must be thread-safe and support concurrent operations, but the caller (server.go) guarantees that there will never be two inflight operations of the same type for the same actionID (singleflight) which makes implementing the backends simpler (no need to worry about locking at the filesystem layer).

type Debug

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

Debug wraps any Backend and adds debug logging. This allows any backend implementation to have debug logging without coupling the debug logic to the backend implementation.

func NewDebug

func NewDebug(backend Backend) *Debug

NewDebug creates a new debug wrapper around an existing backend.

func (*Debug) Clear

func (d *Debug) Clear() error

Clear removes all entries from the cache with debug logging.

func (*Debug) Close

func (d *Debug) Close() error

Close performs cleanup operations with debug logging.

func (*Debug) Get

func (d *Debug) Get(actionID []byte) ([]byte, io.ReadCloser, int64, *time.Time, bool, error)

Get retrieves an object from the backend storage with debug logging.

func (*Debug) Put

func (d *Debug) Put(actionID, outputID []byte, body io.Reader, bodySize int64) error

Put stores an object in the backend storage with debug logging.

type Error

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

Error wraps any Backend and randomly returns errors based on a configured percentage. This is useful for testing error handling and resilience.

func NewError

func NewError(backend Backend, errorRate float64) *Error

NewError creates a new error-injecting wrapper around an existing backend. errorRate should be between 0.0 (no errors) and 1.0 (all errors fail).

func (*Error) Clear

func (e *Error) Clear() error

Clear removes all entries from the cache, potentially returning an error.

func (*Error) Close

func (e *Error) Close() error

Close performs cleanup operations, potentially returning an error.

func (*Error) Get

func (e *Error) Get(actionID []byte) ([]byte, io.ReadCloser, int64, *time.Time, bool, error)

Get retrieves an object from the backend storage, potentially returning an error.

func (*Error) GetStats

func (e *Error) GetStats() (putErrors, getErrors, closeErrors, clearErrors int64)

GetStats returns the number of errors injected for each operation type. This method is thread-safe.

func (*Error) Put

func (e *Error) Put(actionID, outputID []byte, body io.Reader, bodySize int64) error

Put stores an object in the backend storage, potentially returning an error.

type GCS added in v1.5.0

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

GCS implements Backend using Google Cloud Storage. This backend only handles GCS operations; local disk caching is handled by server.go.

func NewGCS added in v1.5.0

func NewGCS(bucket, prefix string) (*GCS, error)

NewGCS creates a new GCS-based cache backend. bucket is the GCS bucket name where cache files will be stored. prefix is an optional prefix for all GCS object names (e.g., "cache/" or "").

func (*GCS) Clear added in v1.5.0

func (g *GCS) Clear() error

Clear removes all entries from the cache in GCS.

func (*GCS) Close added in v1.5.0

func (g *GCS) Close() error

Close performs cleanup operations.

func (*GCS) Get added in v1.5.0

func (g *GCS) Get(actionID []byte) ([]byte, io.ReadCloser, int64, *time.Time, bool, error)

Get retrieves an object from GCS. Returns the object data as an io.ReadCloser that must be closed by the caller.

func (*GCS) Put added in v1.5.0

func (g *GCS) Put(actionID, outputID []byte, body io.Reader, bodySize int64) error

Put stores an object in GCS.

type Noop

type Noop struct{}

Noop is a no-op backend that does nothing. This is useful when using only local disk caching (via server.go's localCache) without any distributed/remote backend storage.

func NewNoop

func NewNoop() *Noop

NewNoop creates a new no-op backend.

func (*Noop) Clear

func (n *Noop) Clear() error

Clear does nothing. The local cache in server.go manages its own clearing if needed.

func (*Noop) Close

func (n *Noop) Close() error

Close does nothing.

func (*Noop) Get

func (n *Noop) Get(actionID []byte) ([]byte, io.ReadCloser, int64, *time.Time, bool, error)

Get always returns a miss. The local cache in server.go handles retrieving cached entries.

func (*Noop) Put

func (n *Noop) Put(actionID, outputID []byte, body io.Reader, bodySize int64) error

Put does nothing and always succeeds. The local cache in server.go handles the actual storage.

type S3

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

S3 implements Backend using AWS S3. This backend only handles S3 operations; local disk caching is handled by server.go.

func NewS3

func NewS3(bucket, prefix string, awsCfg S3Config) (*S3, error)

NewS3 creates a new S3-based cache backend. bucket is the S3 bucket name where cache files will be stored. prefix is an optional prefix for all S3 keys (e.g., "cache/" or ""). awsCfg provides explicit AWS configuration (region, credentials) resolved by the caller.

func (*S3) Clear

func (s *S3) Clear() error

Clear removes all entries from the cache in S3.

func (*S3) Close

func (s *S3) Close() error

Close performs cleanup operations.

func (*S3) Get

func (s *S3) Get(actionID []byte) ([]byte, io.ReadCloser, int64, *time.Time, bool, error)

Get retrieves an object from S3. Returns the object data as an io.ReadCloser that must be closed by the caller.

func (*S3) Put

func (s *S3) Put(actionID, outputID []byte, body io.Reader, bodySize int64) error

Put stores an object in S3.

type S3Config added in v1.5.0

type S3Config struct {
	Region          string
	AccessKeyID     string
	SecretAccessKey string
	SessionToken    string
	UsePathStyle    bool
}

S3Config holds AWS-specific configuration for the S3 backend. These are resolved from environment variables in main.go using the GOBUILDCACHE_-prefixed convention, allowing users to provide AWS credentials without polluting the environment for other processes (e.g., test binaries spawned by go test).

Jump to

Keyboard shortcuts

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