Skip to content

Commit

Permalink
refactor: adjust plugin handle
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Apr 13, 2024
1 parent b10ba0a commit ed38f38
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 89 deletions.
107 changes: 57 additions & 50 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ rust-version = "1.74"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-trait = "0.1.79"
async-trait = "0.1.80"
base64 = "0.22.0"
bytes = "1.6.0"
bytesize = "1.3.0"
Expand Down
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- [ ] tls cert auto update
- [ ] support validate config before save(web)
- [ ] auto reload config and restart
- [ ] support plugin for proxy and response
- [x] authentication for admin page
- [x] custom error for pingora error
- [x] support alpn for location
Expand Down
9 changes: 2 additions & 7 deletions src/config/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::{proxy::Limiter, utils};
use crate::utils;
use base64::{engine::general_purpose::STANDARD, Engine};
use glob::glob;
use http::HeaderValue;
Expand Down Expand Up @@ -127,7 +127,6 @@ pub struct LocationConf {
pub gzip_level: Option<u32>,
pub br_level: Option<u32>,
pub zstd_level: Option<u32>,
pub limit: Option<String>,
pub remark: Option<String>,
}

Expand Down Expand Up @@ -158,11 +157,7 @@ impl LocationConf {
message: format!("{} upstream is not found(location:{name})", self.upstream),
});
}
if let Some(limit) = &self.limit {
Limiter::new(limit).map_err(|err| Error::Invalid {
message: format!("{err}({limit})"),
})?;
}

Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

pub mod config;
pub mod http_extra;
pub mod plugin;
pub mod proxy;
pub mod serve;
pub mod state;
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use std::sync::Arc;

mod config;
mod http_extra;
mod plugin;
mod proxy;
mod serve;
mod state;
Expand Down
12 changes: 12 additions & 0 deletions src/proxy/limit.rs → src/plugin/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use super::ProxyPlugin;
use crate::state::State;
use crate::utils;
use async_trait::async_trait;
use pingora::proxy::Session;
use pingora_limits::inflight::Inflight;
use snafu::{ResultExt, Snafu};
Expand Down Expand Up @@ -108,6 +110,16 @@ impl Limiter {
Ok(())
}
}
#[async_trait]
impl ProxyPlugin for Limiter {
async fn handle(&self, session: &mut Session, ctx: &mut State) -> pingora::Result<bool> {
let _ = self
.incr(session, ctx)
.map_err(|e| utils::new_internal_error(429, e.to_string()))?;
Ok(false)
}
}

#[cfg(test)]
mod tests {
use crate::state::State;
Expand Down
Loading

0 comments on commit ed38f38

Please sign in to comment.