Pingap Core
Pingap Core is the foundational library for the Pingap project, providing a robust set of core components and utilities for building high-performance, extensible proxy and networking applications in Rust.
Overview
This library offers a modular toolkit designed to handle the entire lifecycle of HTTP requests and responses. It includes a powerful request context system, flexible plugin architecture, efficient HTTP header and response manipulation, background task management, and more. It is built on top of the pingora framework, extending its capabilities with specialized features.
Core Features
- Request Context (
Ctx): A central struct that tracks the state of each request, including timing metrics, connection details, upstream information, caching status, and custom variables. It also provides utilities for generating detailed logs andServer-Timingheaders. - Plugin System: An extensible plugin architecture that allows developers to hook into various stages of the request/response lifecycle (
PluginStep). This enables custom logic for authentication, rate-limiting, header modification, and more. A response that a plugin answers with skips the response step; a plugin that returnstruefromhandles_plugin_response()still gets to set its headers on it. - HTTP Helpers: A rich set of utilities for working with HTTP headers and responses:
- Header Manipulation: Parse, create, and modify HTTP headers, with support for dynamic value substitution (e.g.,
$hostname,$ja4,$remote_addr,$http_user_agent). - Response Builders: Fluent builders (
HttpResponseBuilder) for easily constructing complete HTTP responses, with helpers for common types like JSON, HTML, text, and redirects. A header given twice is the last one given, exceptSet-Cookie,WWW-Authenticate,Proxy-AuthenticateandLink, which are sent once per value. - Streaming Responses: Support for chunked responses (
HttpChunkResponse) to efficiently stream large bodies of data.
- Header Manipulation: Parse, create, and modify HTTP headers, with support for dynamic value substitution (e.g.,
- Background Task Service: A generic service (
BackgroundTaskService) for running periodic tasks in the background, such as health checks, data synchronization, or cleanup routines. - Rate Limiting: An efficient, TTL-based LRU cache (
TtlLruLimit) for implementing rate-limiting logic based on a maximum number of requests within a given time window. - Notification Service: A simple, extensible trait (
Notification) for sending alerts and notifications through various channels. - High-Performance Utilities: Shared helpers for the request path — clock reads, header manipulation, constant-time comparison and duration formatting.
Modules
ctx: Provides the centralCtxstruct for request lifecycle state management.CtxLogFieldnames the values the access log can print; a log format parses the name once and callsCtx::append_log_fieldper request.http_header: Contains helpers for parsing and manipulating HTTP request headers.resolve_static_header_valuefolds$hostnameand$ENV_VARinto plain values when the configuration is loaded;$http_user_agentreadsUser-Agent(underscores stand for dashes);get_hostkeeps an IPv6 literal whole.http_response: Provides builders and structs for creating HTTP responses.plugin: Defines thePlugintrait andPluginStepenum for building custom plugins.service: Includes theBackgroundTaskServicefor running background tasks. Every task of a service keeps its own time: a round starts the tasks that are free, and one that is still running from an earlier round is left out of it (and warned about, with how long it has been running) instead of holding the others up. Thecounta task is given is the number of its own runs, so a task that acts on every nth run does not miss its turn in a round it sat out. Routine rounds log at debug level.ttl_lru_limit: Implements a TTL-based LRU rate limiter. Counters are atomic, so concurrent increments on one key are all counted.notification: Defines the trait for sending notifications.identity: Who the process is, for what it says about itself: the name and the labels of the deployment (basic.name,basic.labels), set once withset_identityand read withget_identityby the webhook, the access log and the tracer.parse_labelsreads thekey:valueentries of the configuration.proxy_protocol: The PROXY protocol on the connecting side.new_proxy_protocol_headerbuilds a version 1 or 2 header for a client's addresses, andProxyProtocolConnectoris a pingoraConnectthat writes it as the first bytes of a connection (PeerOptions::custom_l4). Used bysend_proxy_protocolof an upstream and by its health checks; the listening side is inpingap-proxy.util: Contains miscellaneous utilities, including time caching and hostname retrieval.
License
This project is licensed under the Apache-2.0 License.