httpx

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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.

View Source
const ModulePath = "github.com/MiravaOrg/mirava-core"

ModulePath is this module's import path.

Variables

This section is empty.

Functions

func ClientOr

func ClientOr(c *http.Client) *http.Client

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

func DefaultClient() *http.Client

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

func Do(ctx context.Context, c *http.Client, req *http.Request) (*http.Response, error)

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

func JoinURL(base string, paths ...string) (string, error)

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

func NewRequest(ctx context.Context, method, url string) (*http.Request, error)

NewRequest builds a GET request with the mirava User-Agent set. Returns an error suitable for *RequestError wrapping by the caller.

func ReadAll

func ReadAll(resp *http.Response) ([]byte, error)

ReadAll reads and closes resp.Body.

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.

type Progress

type Progress struct {
	Bytes    int64
	Total    int64 // 0 when unknown
	Elapsed  time.Duration
	SpeedMBs float64
}

Progress describes incremental speed-test progress. OnProgress is invoked at most every ProgressInterval (implementations decide).

Jump to

Keyboard shortcuts

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