插件体系
插件系统允许您通过添加各类插件来扩展 Location 的功能,支持鉴权、流控、响应头设置等多种场景。通过插件,您可以灵活地定制请求处理流程。
插件执行时点
插件可以在请求处理的不同阶段执行,目前支持以下三个时点:
-
Request
: 请求最开始阶段- 适用于权限验证等需要早期拦截的场景
-
ProxyUpstream
: 转发至上游节点之前- 在读取缓存之后执行
- 适用于需要限制上游访问但允许缓存访问的场景
- 例如:限制对上游的请求频率,但允许高并发读取缓存
-
Response
: 上游响应之后- 用于处理和修改上游返回的响应数据
#[async_trait]
pub trait Plugin: Sync + Send {
/// Returns a unique key that identifies this specific plugin instance.
///
/// # Purpose
/// - Can be used for caching plugin results
/// - Helps differentiate between multiple instances of the same plugin type
/// - Useful for tracking and debugging
///
/// # Default
/// Returns an empty string by default, which means no specific instance identification.
fn hash_key(&self) -> String {
"".to_string()
}
/// Processes an HTTP request at a specified lifecycle step.
///
/// # Parameters
/// * `_step` - Current processing step in the request lifecycle (e.g., pre-routing, post-routing)
/// * `_session` - Mutable reference to the HTTP session containing request data
/// * `_ctx` - Mutable reference to the request context for storing state
///
/// # Returns
/// * `Ok((executed, response))` where:
/// * `executed` - Boolean flag:
/// - `true`: Plugin performed meaningful logic for this request
/// - `false`: Plugin was skipped or did nothing for this request
/// * `response` - Optional HTTP response:
/// - `Some(response)`: Terminates request processing and returns this response to client
/// - `None`: Allows request to continue to next plugin or upstream
/// * `Err` - Returns error if plugin processing failed
async fn handle_request(
&self,
_step: PluginStep,
_session: &mut Session,
_ctx: &mut Ctx,
) -> pingora::Result<(bool, Option<HttpResponse>)> {
Ok((false, None))
}
/// Processes an HTTP response at a specified lifecycle step.
///
/// # Parameters
/// * `_step` - Current processing step in the response lifecycle
/// * `_session` - Mutable reference to the HTTP session
/// * `_ctx` - Mutable reference to the request context
/// * `_upstream_response` - Mutable reference to the upstream response header
///
/// # Returns
/// * `Ok(modified)` - Boolean flag:
/// - `true`: Plugin modified the response in some way
/// - `false`: Plugin did not modify the response
/// * `Err` - Returns error if plugin processing failed
async fn handle_response(
&self,
_step: PluginStep,
_session: &mut Session,
_ctx: &mut Ctx,
_upstream_response: &mut ResponseHeader,
) -> pingora::Result<bool> {
Ok(false)
}
}
主要分三个实现:
category
: 插件类型,用于区分该插件是哪类形式的插件step
: 插件的执行时序,按需选择插件的执行时序,不同的插件可选择的选择不一样handle_request
: 插件的转发前执行逻辑,若返回的是Ok(Some(HttpResponse))
,则表示请求已处理完成,不再转发到上游节点,并将该响应传输至请求端handle_response
: 插件的响应前执逻辑,若返回的是Ok(true)
,则表示该插件被成功执行
Stats 性能统计插件
Stats插件用于暴露应用的性能指标数据,方便第三方系统采集监控。可以通过以下两种方式使用:
- 使用内置的
pingap:stats
插件 - 自定义配置新的 Stats 插件
配置示例
[plugins.stats]
category = "stats"
path = "/stats" # 访问路径
remark = "性能指标采集"
配置参数
path
: 指标数据访问路径,如配置为/stats
则可通过该路径获取性能指标step
: 插件执行时机,可选request
或proxy_upstream
使用方式
配置完成后, 可以通过访问对应 location 下的 /stats
路径获取性能指标数据。
Ping
Ping插件提供了一个简单的健康检查端点,用于监控服务是否正常运行。当配置了反向代理时,也可以作为 pingap 的健康检查机制,该插件固定执行在request
阶段。
[plugins.pingpong]
category = "ping"
path = "/ping" # 健康检查路径