fx

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package fx fetches the ECB euro reference rates through the host, persists the last good snapshot and converts it to USD-based display rates. USD stays the only stored and computed unit; these rates are display-only.

Index

Constants

View Source
const MaxBytes = 1 << 20

MaxBytes caps the fetched ECB document.

View Source
const Timeout = 30 * time.Second

Timeout bounds one fetch.

Variables

This section is empty.

Functions

func ParseECB

func ParseECB(body []byte) (asOf string, perEUR map[string]float64, err error)

ParseECB decodes the ECB daily reference rates document. It returns the reference date and the rates per 1 EUR. Anything but exactly one dated cube with valid, unique currency codes and positive rates including USD is an error, so shape drift fails closed.

func Rates

func Rates(currencies []string, perEUR, fixed map[string]float64) map[string]float64

Rates returns units of X per 1 USD for each configured currency that has a rate. perEUR (ECB, may be nil) wins; fixed fills currencies ECB lacks. USD is always 1. A currency without a rate from either source is omitted.

Types

type Settings

type Settings struct {
	Enabled bool // source == ecb
	URL     string
	Refresh time.Duration
}

Settings configures the worker.

type State

type State struct {
	Snapshot *store.FXSnapshot // nil until a document was loaded
	Error    string            // last fetch error ("" = last attempt ok)
}

State is the published FX state.

type Worker

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

Worker owns the ECB fetch lifecycle.

func New

func New(host abi.Host, st *store.Store, s Settings, publish func(State)) *Worker

New creates a worker. publish must be cheap and non-blocking.

func (*Worker) FetchOnce

func (w *Worker) FetchOnce(ctx context.Context)

FetchOnce performs one conditional fetch and updates state and storage.

func (*Worker) Kick

func (w *Worker) Kick()

Kick requests an immediate re-evaluation.

func (*Worker) Load

func (w *Worker) Load(ctx context.Context) error

Load installs the persisted snapshot and last error.

func (*Worker) Run

func (w *Worker) Run(ctx context.Context)

Run fetches when due until ctx is canceled. While disabled it only waits for a settings change.

func (*Worker) State

func (w *Worker) State() State

State returns the current state.

func (*Worker) Update

func (w *Worker) Update(s Settings)

Update replaces settings; enabling or a changed URL triggers a fetch.

Jump to

Keyboard shortcuts

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