http

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package http is a first-party HTTP/HTTPS provider for FastConf.

It performs periodic GETs against a configured URL, decodes the body using a registered FastConf Codec (yaml/json by default), and emits change events to drive Manager reloads. The implementation is the canonical "remote provider golden reference" for FastConf:

  • Stateless: no goroutines outside the user-controlled Watch loop.
  • ETag / If-None-Match aware: when the server supplies an ETag header, subsequent polls send If-None-Match and treat 304 as "unchanged" (no event, no reload work).
  • Body-hash fallback: if the server omits ETag, the provider hashes the response body and only emits an event when the hash changes — preserves FastConf's "no spurious reload" guarantee.
  • Pluggable HTTP client and clock for tests; default uses http.DefaultClient with a 10s timeout.
  • Implements contracts.Provider so it slots into fastconf.WithProvider(...) directly.

The module is intentionally tiny and depends only on the standard library plus fastconf/contracts so it can be vendored as a reference when authoring proprietary remote providers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Codec

type Codec = contracts.Codec

Codec is the minimal byte→map decoder this provider needs. It matches contracts.Codec exactly so the user can pass any registered FastConf codec (yaml, json, toml-via-plugin, ...) without adapter.

type Doer

type Doer interface {
	Do(req *nethttp.Request) (*nethttp.Response, error)
}

Doer is the subset of *http.Client needed by Provider — anything implementing this contract can be injected for tests, retries, or authenticated transports.

type Option

type Option func(*Provider)

Option mutates a Provider during construction. Use the With* helpers below to compose configuration without growing New's signature.

func WithClient

func WithClient(c Doer) Option

WithClient injects an alternate HTTP client (e.g. one with auth or instrumented for traces). Default: http.DefaultClient.

func WithHeader

func WithHeader(k, v string) Option

WithHeader adds a static request header (Bearer tokens, tenant IDs, ...). Multiple calls accumulate.

func WithInterval

func WithInterval(d time.Duration) Option

WithInterval overrides the watch poll interval (default: 30s).

func WithPriority

func WithPriority(p int) Option

WithPriority overrides the default priority (PriorityKV).

type Provider

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

Provider polls an HTTP endpoint on a configurable interval and emits a change event whenever the response payload (or its ETag) differs from the last accepted one.

func New

func New(name, url string, codec Codec, opts ...Option) (*Provider, error)

New constructs an HTTP-backed Provider. name is surfaced in Snapshot().Sources and metrics labels; codec decodes the response body. url MUST be absolute. A zero-value codec or empty url returns an error rather than panicking later in the reload loop.

func (*Provider) Load

func (p *Provider) Load(ctx context.Context) (map[string]any, error)

Load fetches the URL and returns the decoded payload. It updates the provider's ETag / body-hash bookkeeping so the next Watch tick can short-circuit unchanged responses. A 304 response returns the previously-loaded snapshot rather than an error.

func (*Provider) Name

func (p *Provider) Name() string

Name implements contracts.Provider.

func (*Provider) Priority

func (p *Provider) Priority() int

Priority implements contracts.Provider.

func (*Provider) Watch

func (p *Provider) Watch(ctx context.Context) (<-chan contracts.Event, error)

Watch ticks every interval (default 30s) and emits an event when the remote payload changes. Returning a closed channel on ctx cancel keeps the manager's watcher loop tidy.

Jump to

Keyboard shortcuts

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