Documentation
¶
Overview ¶
Package httpx provides shared HTTP plumbing for mirava-core backends: URL joining, the shared default client, User-Agent construction, and a single speed-measurement loop used by every backend's Speed probe.
Index ¶
- Constants
- func ClientOr(c *http.Client) *http.Client
- func DefaultClient() *http.Client
- func Do(ctx context.Context, c *http.Client, req *http.Request) (*http.Response, error)
- func JoinURL(base string, paths ...string) (string, error)
- func MeasureSpeed(ctx context.Context, client *http.Client, rawURL string, opts MeasureOptions) (mbs float64, bytes int64, elapsed time.Duration, finalURL string, err error)
- func NewRequest(ctx context.Context, method, url string) (*http.Request, error)
- func ReadAll(resp *http.Response) ([]byte, error)
- func SetUserAgentVersion(v string)
- func UserAgent() string
- type MeasureOptions
- type Progress
Constants ¶
const DefaultTimeout = 60 * time.Second
DefaultTimeout bounds every individual HTTP request made by the default client. Backends accept an injected client; when none is provided this one is used. Callers that want per-probe budgets pass context deadlines instead.
const ModulePath = "github.com/MiravaOrg/mirava-core"
ModulePath is this module's import path.
Variables ¶
This section is empty.
Functions ¶
func ClientOr ¶
ClientOr returns c if it is non-nil, else the shared default client. All backends call this so a zero-value service never panics on a nil client.
func DefaultClient ¶
DefaultClient returns the process-wide client used when a backend is not given one. It is shared so connection pools are reused across backends.
func Do ¶
Do sends req via c, wrapping transport errors. On non-2xx responses the body is drained and closed by the caller via resp.Body.
func JoinURL ¶
JoinURL joins base with path segments, normalizing slashes so both "https://m/ubuntu/" and "https://m/ubuntu" produce the same result. This is the only way backends combine a mirror base URL with a path.
func MeasureSpeed ¶
func MeasureSpeed(ctx context.Context, client *http.Client, rawURL string, opts MeasureOptions) (mbs float64, bytes int64, elapsed time.Duration, finalURL string, err error)
MeasureSpeed downloads url for at most opts.Duration (bounded by ctx) and returns the average throughput in MB/s, total bytes, and elapsed time. It is the single implementation of the speed-test loop every backend uses.
func NewRequest ¶
NewRequest builds a GET request with the mirava User-Agent set. Returns an error suitable for *RequestError wrapping by the caller.
func SetUserAgentVersion ¶
func SetUserAgentVersion(v string)
SetUserAgentVersion overrides the version reported in the User-Agent header. Intended for builds where build info is unavailable (e.g. go run with -ldflags "-X github.com/.../internal/httpx.userAgentVersion=v0.4.0").
func UserAgent ¶
func UserAgent() string
UserAgent returns the User-Agent value used for all probe requests, e.g. "mirava-core/0.4.0 (https://github.com/MiravaOrg/mirava-core)".
Types ¶
type MeasureOptions ¶
type MeasureOptions struct {
// Duration bounds the download. Zero means unbounded (rely on ctx).
Duration time.Duration
// MinBytes is the smallest sample considered valid. Shorter downloads
// produce a measurement error instead of a wildly inflated speed.
MinBytes int64
// OnProgress, when non-nil, receives incremental progress.
OnProgress func(Progress)
// Headers added to the request (e.g. Docker registry auth).
Headers http.Header
}
MeasureOptions configures MeasureSpeed.