backend

package
v0.2.18 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BackendAPI  = "api"
	BackendDB   = "db"
	BackendAuto = "auto"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type FallbackEvent

type FallbackEvent struct {
	Reason    string // "api_error", "api_empty", "api_unavailable"
	Entity    string // "messages", "chats", etc.
	Timestamp time.Time
	Error     error // original error if any
}

FallbackEvent contains structured information about a fallback occurrence.

type FallbackOptions

type FallbackOptions[T any] struct {
	// Backend is the selected backend mode (api, db, auto)
	Backend string
	// FallbackAllowed indicates if fallback is permitted for this operation
	FallbackAllowed bool
	// ProgressWriter receives fallback status messages
	ProgressWriter io.Writer
	// PreAPIError is an error that occurred before API call (e.g., client creation failed)
	// If set and fallback is allowed, DB fallback is attempted without calling APICall
	PreAPIError error
	// APICall executes the API operation
	APICall func() (T, error)
	// DBCall executes the DB operation
	DBCall func(store *db.Store) (T, error)
	// IsEmpty checks if the result should trigger fallback
	IsEmpty func(T) bool
	// OpenStore opens a DB connection
	OpenStore func() (*db.Store, error)
	// EntityName is used in progress messages (e.g., "messages", "chats")
	EntityName string
	// OnFallback is called when fallback is triggered (for metrics/logging)
	OnFallback func(FallbackEvent)
	// WarnStale adds a warning that DB data may be stale when fallback is used
	WarnStale bool
}

FallbackOptions configures the WithFallback behavior.

type FallbackResult

type FallbackResult[T any] struct {
	Data         T
	UsedFallback bool
	FallbackMsg  string
}

FallbackResult wraps a result with metadata about whether fallback was used.

func WithFallback

func WithFallback[T any](ctx context.Context, opts FallbackOptions[T]) (FallbackResult[T], error)

WithFallback executes an API call with automatic DB fallback. It handles four fallback triggers: 1. Backend explicitly set to "db" 2. PreAPIError is set (e.g., client creation failed) 3. API call returns an error (when fallback allowed) 4. API call returns empty results (when fallback allowed)

The ctx parameter is currently unused but included for forward compatibility with future cancellation and timeout support.

func (FallbackResult[T]) IsEmpty

func (r FallbackResult[T]) IsEmpty() bool

IsEmpty returns true if the underlying data is empty. Works with slices by checking length via reflection.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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