Documentation
¶
Overview ¶
Package proxy provides URL rewriting and reverse proxy functionality for apt-proxy. It handles distribution-specific URL patterns and routes requests to configured mirrors.
Index ¶
- Constants
- func GetBaseTemplate(cacheSize string, filesNumber string, availableSize string, memoryUsage string, ...) string
- func GetInternalResType(url string) int
- func GetRewriteRulesByMode(reg *distro.Registry, mode int) []distro.Rule
- func HandleHomePage(rw http.ResponseWriter, r *http.Request, cacheDir string)
- func IsInternalUrls(url string) bool
- func MatchingRule(path string, rules []distro.Rule) (*distro.Rule, bool)
- func NewUpstreamTransport(enableKeepAlive bool) *http.Transport
- func RefreshRewriters(rewriters *URLRewriters, mode int, st *state.AppState, reg *distro.Registry)
- func RefreshRewritersWithEngine(rewriters *URLRewriters, mode int, st *state.AppState, reg *distro.Registry, ...)
- func RenderInternalUrls(url string, cacheDir string) (string, int)
- func RewriteRequestByMode(r *http.Request, rewriters *URLRewriters, mode int)
- func ServeStaticLogo(w http.ResponseWriter, r *http.Request)
- type HomePageData
- type Options
- type PackageStruct
- func (ap *PackageStruct) BenchmarkEngine() *benchmarks.Engine
- func (ap *PackageStruct) RefreshMirrors()
- func (ap *PackageStruct) Registry() *distro.Registry
- func (ap *PackageStruct) ServeHTTP(rw http.ResponseWriter, r *http.Request)
- func (ap *PackageStruct) State() *state.AppState
- func (ap *PackageStruct) Transport() http.RoundTripper
- type RetryableTransport
- type URLRewriter
- type URLRewriters
- func CreateNewRewriters(mode int, st *state.AppState, reg *distro.Registry) *URLRewriters
- func CreateNewRewritersAsync(mode int, st *state.AppState, reg *distro.Registry) *URLRewriters
- func CreateNewRewritersAsyncWithEngine(mode int, st *state.AppState, reg *distro.Registry, bench *benchmarks.Engine) *URLRewriters
- func CreateNewRewritersWithEngine(mode int, st *state.AppState, reg *distro.Registry, bench *benchmarks.Engine) *URLRewriters
Constants ¶
const ( DefaultResponseHeaderTimeout = 45 * time.Second DefaultIdleConnTimeout = 90 * time.Second DefaultMaxIdleConns = 100 )
Default transport timeouts and limits for upstream requests. Extract to constants for tuning and documentation.
const ( InternalPageHome string = "/" InternalPagePing string = "/_/ping/" )
const ( TypeNotFound int = 0 TypeHome int = 1 TypePing int = 2 )
const LabelNoValidValue = "N/A"
Variables ¶
This section is empty.
Functions ¶
func GetBaseTemplate ¶
func GetBaseTemplate(cacheSize string, filesNumber string, availableSize string, memoryUsage string, goroutines string) string
GetBaseTemplate renders the home page template with the provided statistics. It uses html/template for safe rendering and returns the rendered HTML string.
func GetInternalResType ¶
func GetRewriteRulesByMode ¶
GetRewriteRulesByMode returns caching rules for a specific mode. Prefers registry (config-loaded) rules when present.
func HandleHomePage ¶
func HandleHomePage(rw http.ResponseWriter, r *http.Request, cacheDir string)
HandleHomePage serves the home page with statistics
func IsInternalUrls ¶
func MatchingRule ¶
MatchingRule finds a matching rule for the given path
func NewUpstreamTransport ¶ added in v0.12.0
NewUpstreamTransport constructs a fresh upstream *http.Transport with apt-proxy's tuned timeouts and connection-pool defaults.
enableKeepAlive: true reuses connections to mirrors (recommended); false disables keep-alives.
func RefreshRewriters ¶
RefreshRewriters refreshes the rewriters with updated mirror configurations. This function is safe to call concurrently and will update the mirrors based on the supplied AppState/Registry. It clears the process-wide default benchmark engine's cache to force fresh benchmark tests; callers holding a per-Server *benchmarks.Engine should prefer RefreshRewritersWithEngine so they only flush their own cache.
IMPORTANT: This function creates new rewriters outside the lock to avoid blocking request processing during potentially slow network operations (benchmark tests). The lock is only held briefly during the pointer swap.
func RefreshRewritersWithEngine ¶ added in v0.12.0
func RefreshRewritersWithEngine(rewriters *URLRewriters, mode int, st *state.AppState, reg *distro.Registry, bench *benchmarks.Engine)
RefreshRewritersWithEngine is the engine-aware variant of RefreshRewriters. A nil engine falls back to benchmarks.Default(). Only the supplied engine's cache is cleared; per-Server engines no longer interfere with each other.
func RewriteRequestByMode ¶
func RewriteRequestByMode(r *http.Request, rewriters *URLRewriters, mode int)
RewriteRequestByMode rewrites the request URL to point to the configured mirror for the specified distribution mode. It matches the request path against distribution-specific patterns and replaces the URL scheme, host, and path with the mirror's configuration. If rewriters is nil, the function returns early.
func ServeStaticLogo ¶ added in v0.10.0
func ServeStaticLogo(w http.ResponseWriter, r *http.Request)
ServeStaticLogo writes the embedded apt-proxy logo PNG with long-lived caching headers. It is intentionally minimal so it can be wired up via any router that accepts an http.HandlerFunc.
Types ¶
type HomePageData ¶
type HomePageData struct {
CacheSize string
FilesNumber string
AvailableSize string
MemoryUsage string
Goroutines string
}
HomePageData holds the data for rendering the home page template
type Options ¶ added in v0.12.0
type Options struct {
State *state.AppState
Registry *distro.Registry
CacheDir string
Logger *logger.Logger
Mode int
EnableKeepAlive bool
Async bool // when true, use async (non-blocking) benchmarks during construction
TransportOverride http.RoundTripper // optional: caller-supplied transport (mainly for tests)
}
Options configures NewPackageStruct.
type PackageStruct ¶
type PackageStruct struct {
Handler http.Handler // The underlying HTTP handler (typically a reverse proxy)
Rules []distro.Rule // Caching rules for different package types
CacheDir string // Cache directory path for statistics
// contains filtered or unexported fields
}
PackageStruct is the main HTTP handler that routes requests to appropriate distribution-specific handlers and applies caching rules. It owns all of the per-Server state previously held in package-level globals: the AppState, distro Registry, URL rewriters, and the host-pattern cache.
func NewPackageStruct ¶ added in v0.12.0
func NewPackageStruct(opts Options) (*PackageStruct, error)
NewPackageStruct constructs a fully wired PackageStruct using the supplied state/registry. State and Registry are required; Logger defaults to logger.Default() when nil.
func (*PackageStruct) BenchmarkEngine ¶ added in v0.12.0
func (ap *PackageStruct) BenchmarkEngine() *benchmarks.Engine
BenchmarkEngine exposes this PackageStruct's private benchmark engine. Callers (tests, debug endpoints) should prefer this over benchmarks.Default() so they observe the same cache the Server uses.
func (*PackageStruct) RefreshMirrors ¶ added in v0.10.0
func (ap *PackageStruct) RefreshMirrors()
RefreshMirrors refreshes this PackageStruct's mirror configuration. Triggered by SIGHUP and POST /api/mirrors/refresh. The mutex serializes concurrent refreshes (the rewriter pointer swap inside RefreshRewriters has its own finer-grained lock; this outer lock prevents two refresh runs from racing to clear the benchmark cache and re-elect mirrors at the same time).
Cache-isolation note: this clears only this PackageStruct's private benchmarks.Engine cache, so a refresh on one Server no longer affects any other Server's mirror selection. (Historically the engine was a package-level singleton; that coupling was removed in favour of the per-Server engine field above.)
func (*PackageStruct) Registry ¶ added in v0.12.0
func (ap *PackageStruct) Registry() *distro.Registry
Registry returns the distribution registry backing this PackageStruct.
func (*PackageStruct) ServeHTTP ¶
func (ap *PackageStruct) ServeHTTP(rw http.ResponseWriter, r *http.Request)
ServeHTTP implements http.Handler interface. It processes incoming requests, matches them against caching rules, and routes them to the appropriate handler. If a matching rule is found, the request is processed with cache control headers.
func (*PackageStruct) State ¶ added in v0.12.0
func (ap *PackageStruct) State() *state.AppState
State returns the AppState backing this PackageStruct (read-only access for callers that need to inspect or mutate mirror configuration).
func (*PackageStruct) Transport ¶ added in v0.12.0
func (ap *PackageStruct) Transport() http.RoundTripper
Transport returns the upstream HTTP transport. Exposed mainly so the caller can wrap it (e.g. with httpcache) when constructing the final handler chain.
type RetryableTransport ¶
type RetryableTransport struct {
// contains filtered or unexported fields
}
RetryableTransport wraps an http.RoundTripper with retry logic and tracing support
func NewRetryableTransport ¶
func NewRetryableTransport(baseTransport http.RoundTripper) *RetryableTransport
NewRetryableTransport creates a new RetryableTransport with http-kit integration
func (*RetryableTransport) RoundTrip ¶
RoundTrip implements http.RoundTripper interface with retry and tracing support.
Retry safety: when the request has a body, we only retry if the caller supplied req.GetBody (e.g. via http.NewRequest with bytes.Reader/strings.Reader). Otherwise the body would already be consumed by the first attempt; we make a single attempt in that case rather than send a corrupt second request.
func (*RetryableTransport) SetRetryOptions ¶
func (rt *RetryableTransport) SetRetryOptions(opts *httpkit.RetryOptions)
SetRetryOptions allows customizing retry behavior
type URLRewriter ¶
type URLRewriter struct {
// contains filtered or unexported fields
}
URLRewriter holds the mirror and pattern for URL rewriting
type URLRewriters ¶
type URLRewriters struct {
Ubuntu *URLRewriter
UbuntuPorts *URLRewriter
Debian *URLRewriter
Centos *URLRewriter
Alpine *URLRewriter
Mu sync.RWMutex
}
URLRewriters manages rewriters for different distributions
func CreateNewRewriters ¶
CreateNewRewriters initializes rewriters based on mode using synchronous benchmark. May block startup for up to 30 seconds; prefer CreateNewRewritersAsync. Uses the process-wide default benchmarks.Engine; PackageStruct callers route through CreateNewRewritersWithEngine instead.
func CreateNewRewritersAsync ¶
CreateNewRewritersAsync initializes rewriters based on mode using async benchmark. Recommended for production use to minimize startup time.
func CreateNewRewritersAsyncWithEngine ¶ added in v0.12.0
func CreateNewRewritersAsyncWithEngine(mode int, st *state.AppState, reg *distro.Registry, bench *benchmarks.Engine) *URLRewriters
CreateNewRewritersAsyncWithEngine is the engine-aware variant of CreateNewRewritersAsync. A nil engine falls back to benchmarks.Default().
func CreateNewRewritersWithEngine ¶ added in v0.12.0
func CreateNewRewritersWithEngine(mode int, st *state.AppState, reg *distro.Registry, bench *benchmarks.Engine) *URLRewriters
CreateNewRewritersWithEngine is the engine-aware variant of CreateNewRewriters. A nil engine falls back to benchmarks.Default().