Documentation
¶
Overview ¶
Package catalog keeps discovered provider catalogs in a core.CatalogStore the way the gateway keeps its own: reads that serve a stored catalog while it is younger than a TTL and otherwise discover it, reads that never discover, and discovery fenced against invalidation, so a discovery that began before a catalog was invalidated never stores over what came after.
A Service keeps no package state. Its per-key locks and generations live in the Service, so keep one per process for a store, as a Runtime does.
Index ¶
- Constants
- Variables
- func Find(record core.CatalogRecord, model string) (core.ModelInfo, bool)
- type Diagnostics
- type Invalidation
- type Options
- type Read
- type Request
- type Service
- func (s *Service) Cached(ctx context.Context, key core.CatalogKey) Read
- func (s *Service) CachedLookup(ctx context.Context, key core.CatalogKey, model string) (core.ModelInfo, bool)
- func (s *Service) Invalidate(ctx context.Context, key core.CatalogKey, mode Invalidation) error
- func (s *Service) Lookup(ctx context.Context, request Request, model string) (core.ModelInfo, bool, error)
- func (s *Service) Read(ctx context.Context, request Request) Read
- func (s *Service) Refresh(ctx context.Context, request Request) (core.CatalogRecord, error)
- func (s *Service) TTL(instance string) time.Duration
- type Status
Constants ¶
const ( // FailureStateChanged is ErrStateChanged. FailureStateChanged = "state_changed" // FailureFailed is any other error. FailureFailed = "failed" )
Failure codes for errors that are not catalog failures of a provider, whose providers.CatalogError code a read reports as is.
const DefaultTTL = 15 * time.Minute
DefaultTTL is how long a catalog is served before a read discovers it again, when Options.TTL is zero. It is a Runtime's default; the gateway sets an hour.
Variables ¶
var ErrStateChanged = errors.New("catalog: the catalog changed during discovery")
ErrStateChanged reports a discovery an invalidation fenced out: the catalog was invalidated while it ran, so its result was not stored.
Functions ¶
Types ¶
type Diagnostics ¶
type Diagnostics struct {
Status Status `json:"status"`
// FailureCode is a providers.CatalogError code without the gateway's
// catalog_ prefix, or one of the Failure codes.
FailureCode string `json:"failure_code,omitempty"`
Detail string `json:"detail,omitempty"`
UpstreamStatus int `json:"upstream_status,omitempty"`
// Stale reports a catalog served past its TTL.
Stale bool `json:"stale"`
// FromCache reports a catalog served as stored, not just discovered.
FromCache bool `json:"from_cache"`
}
Diagnostics describe a read without its rows. They never quote an error: Detail is a *core.ProviderError's message, which is safe to show, or a fixed description.
type Invalidation ¶
type Invalidation int
Invalidation says whether invalidating a catalog fences the discoveries in flight.
const ( // Hard forgets the catalog and fences every discovery in flight, as a // revoked, replaced or reconfigured credential needs: a slow discovery // never resurrects rows the old credential listed. Hard Invalidation = iota // Soft forgets the catalog without fencing the discoveries in flight, // as a provider's own token refresh or metadata write needs: the // operation that caused it keeps its result. Soft )
type Options ¶
type Options struct {
// Store keeps the catalogs. Nil keeps them in this Service's memory.
// A core.ConditionalCatalogStore fences discoveries in every process
// that shares it; any other store fences them within this Service.
Store core.CatalogStore
// TTL is how long a catalog is served before a read discovers it
// again. Zero or less uses DefaultTTL.
TTL time.Duration
// InstanceTTL gives an instance its own TTL. A result of zero or less
// uses TTL.
InstanceTTL func(instance string) time.Duration
// SchemaVersion stamps every catalog the Service stores. A stored
// catalog stamped otherwise is neither served nor kept as stale: the
// rules that wrote it are not the rules that would read it.
SchemaVersion int
// KeepStale serves the stored catalog, marked stale, beside the error
// of a discovery that fails, as the gateway does. Without it a failed
// discovery returns only its error.
KeepStale bool
// Now returns the current time. Nil uses time.Now.
Now func() time.Time
}
Options configures a Service. Every field is optional.
type Read ¶
type Read struct {
// Record is the catalog served, or the zero record when there is none.
Record core.CatalogRecord
// Diagnostics describe the read without its rows.
Diagnostics Diagnostics
// Err is why the read could not discover or serve a catalog. With
// Options.KeepStale, Record may hold the stale catalog beside it.
Err error
}
Read is the outcome of reading one catalog.
type Request ¶
type Request struct {
Key core.CatalogKey
// Discover lists the catalog upstream. A read calls it when the stored
// catalog is missing or no longer fresh.
Discover func(ctx context.Context) ([]core.ModelInfo, error)
// NotBefore makes a catalog observed before it no longer fresh, as a
// Runtime does with catalogs observed before its settings changed.
NotBefore time.Time
// Rebase is asked whether a discovery that exactly one hard
// invalidation fenced may store anyway, as the gateway lets an
// operation whose own credential write caused the invalidation keep
// its result. Nil never rebases. It must not call the Service.
Rebase func() bool
}
Request asks for the catalog of one key.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service reads, discovers and invalidates catalogs. It is safe for concurrent use.
func (*Service) Cached ¶
Cached returns the stored catalog of key without discovering it, fresh or not, so a read path stays safe while an upstream is slow or down.
func (*Service) CachedLookup ¶
func (s *Service) CachedLookup(ctx context.Context, key core.CatalogKey, model string) (core.ModelInfo, bool)
CachedLookup returns the row of model in the stored catalog of key. It never discovers, so dispatch and transport planning can use it without adding an upstream call to a request.
func (*Service) Invalidate ¶
func (s *Service) Invalidate(ctx context.Context, key core.CatalogKey, mode Invalidation) error
Invalidate forgets the catalog of key. A ConditionalCatalogStore deletes it; any other store keeps a record that was never probed in its place, which no read serves.
func (*Service) Lookup ¶
func (s *Service) Lookup(ctx context.Context, request Request, model string) (core.ModelInfo, bool, error)
Lookup returns the row of model in the catalog Read serves, which may discover it. The error is the read's: with Options.KeepStale a row may come from a stale catalog beside it.
func (*Service) Read ¶
Read returns the catalog of the request's key: the stored one while it is fresh, which is younger than the TTL and observed no earlier than NotBefore, and otherwise a new discovery. The catalog returned is the one stored once the discovery is over, because another write or an invalidation may have superseded it. Discoveries of one key are serialized.
type Status ¶
type Status string
Status summarizes a read, as the gateway's catalog diagnostics do.
const ( // StatusSynced is a catalog with models. StatusSynced Status = "synced" // StatusNotSynced is no catalog: nothing stored, and nothing discovered. StatusNotSynced Status = "not_synced" // StatusEmpty is a catalog discovered without models. StatusEmpty Status = "empty" // StatusError is a read that failed, stale rows or not. StatusError Status = "error" )