backendutil

package
v0.46.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package backendutil holds the shared plumbing used by every platform backend under backends/. It exists to eliminate the copy-pasted HTTP-client, hook, retry, and base-URL construction that was previously duplicated (often byte-for-byte) across the seven backend packages.

These helpers are intentionally internal: they are an implementation detail of the backends and must not become part of the SDK's public API.

Index

Constants

This section is empty.

Variables

View Source
var ErrLabelScanLimit = errors.New("label not found within scan limit")

ErrLabelScanLimit reports that the named label was not found within the scanned page budget. It is distinct from a definitive not-found: the label may exist beyond the limit, so callers surface it as a scan-limit error rather than a 404.

Functions

func ChainTransport

func ChainTransport(inner, outer http.RoundTripper) http.RoundTripper

ChainTransport chains two round trippers. The outer tripper is invoked first; if it returns a non-nil response or a non-nil error that result is returned, otherwise the inner transport (the underlying connection) handles the request.

func ConvertHooks

func ConvertHooks(h *provider.Hooks) *transport.Hooks

ConvertHooks adapts provider.Hooks into transport.Hooks. The request-hook signatures differ across the two packages (provider.RequestHook returns a context; transport.RequestHook returns an error), so each request hook is wrapped to discard the returned context. Identical across all backends.

func DefaultBaseURL

func DefaultBaseURL(base, def string) string

DefaultBaseURL returns base if non-empty, otherwise def.

func HTTPTransport

func HTTPTransport(skipTLS bool) http.RoundTripper

HTTPTransport returns an http.RoundTripper honouring the skipTLS flag: the default transport when verification is enabled, or a transport that disables certificate verification when the caller has explicitly opted into SkipTLS.

func MapRetryConfig

func MapRetryConfig(rc *provider.RetryConfig) *transport.RetryConfig

MapRetryConfig converts a provider.RetryConfig (which counts retries, i.e. attempts beyond the first) into a transport.RetryConfig (which counts total attempts), hence the +1. Returns nil for a nil input so callers can assign the result directly.

func NewScanLimitError added in v0.45.0

func NewScanLimitError(msg string) error

NewScanLimitError builds a ScanLimitError with the given message.

func NormalizeBaseURL

func NormalizeBaseURL(base string) string

NormalizeBaseURL returns base with a single trailing slash removed and a trailing "/api/v1" suffix removed. Used by Gitea/Forgejo-style backends whose SDKs add the version prefix themselves.

func ResolveLabelID added in v0.45.0

func ResolveLabelID(scan LabelPageFunc, name string, maxPages, perPage int) (int64, error)

ResolveLabelID scans pages for the named label and returns its ID, or ErrLabelScanLimit when the budget is exhausted or a short page is reached without a match. Scan errors propagate unchanged.

func ToTransportLogger

func ToTransportLogger(l provider.Logger) transport.Logger

ToTransportLogger adapts a provider.Logger to a transport.Logger.

Types

type IDCache added in v0.45.0

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

IDCache is a TTL cache from string keys to int64 IDs. Backends use it to memoize name→ID resolutions (labels, users) so repeated writes do not rescan. The zero value is not usable; construct with NewIDCache.

func NewIDCache added in v0.45.0

func NewIDCache(ttl time.Duration) *IDCache

NewIDCache constructs an IDCache with the given TTL (TTL <= 0 never expires entries).

func (*IDCache) Get added in v0.45.0

func (c *IDCache) Get(key string) (int64, bool)

Get returns the cached ID for key when present and fresh.

func (*IDCache) Put added in v0.45.0

func (c *IDCache) Put(key string, id int64)

Put stores id under key with a fresh TTL.

func (*IDCache) ResolveLabel added in v0.45.0

func (c *IDCache) ResolveLabel(key, name string, scan LabelPageFunc, maxPages, perPage int) (int64, error)

ResolveLabel resolves via ResolveLabelID and caches the result under key+"/"+name. Failures are not cached.

type LabelPageFunc added in v0.45.0

type LabelPageFunc func(page, perPage int) ([]LabelRef, error)

LabelPageFunc fetches one page of labels; page is 1-based, perPage is the requested page size, and a page returning fewer than perPage refs ends the scan.

type LabelRef added in v0.45.0

type LabelRef struct {
	ID   int64
	Name string
}

LabelRef is the minimal label shape the resolver needs from one platform page fetch.

type ScanLimitError added in v0.45.0

type ScanLimitError struct {
	Msg string
}

ScanLimitError wraps ErrLabelScanLimit with a platform-specific message (errors.Is reports it as ErrLabelScanLimit). Callers that need to distinguish "not found within the scan budget" from other failures use errors.Is(err, backendutil.ErrLabelScanLimit).

func (*ScanLimitError) Error added in v0.45.0

func (e *ScanLimitError) Error() string

func (*ScanLimitError) Unwrap added in v0.45.0

func (e *ScanLimitError) Unwrap() error

Jump to

Keyboard shortcuts

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