forked from grafbase/grafbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrate_limiting.rs
64 lines (53 loc) · 1.5 KB
/
rate_limiting.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use std::collections::HashMap;
use std::net::IpAddr;
use std::sync::Arc;
use std::time::Duration;
use futures_util::future::BoxFuture;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Too many requests")]
ExceededCapacity,
#[error("internal error: {0}")]
Internal(String),
}
pub trait RateLimiterContext: Send + Sync {
fn header(&self, name: http::HeaderName) -> Option<&http::HeaderValue>;
fn graphql_operation_name(&self) -> Option<&str>;
fn ip(&self) -> Option<IpAddr>;
fn jwt_claim(&self, key: &str) -> Option<&serde_json::Value>;
fn key(&self) -> Option<&str> {
None
}
fn is_global(&self) -> bool {
true
}
}
pub trait RateLimiterInner: Send + Sync {
fn limit<'a>(&'a self, context: &'a dyn RateLimiterContext) -> BoxFuture<'a, Result<(), Error>>;
}
#[derive(Clone)]
pub struct RateLimiter {
inner: Arc<dyn RateLimiterInner>,
}
impl RateLimiter {
pub fn new(rate_limiter: impl RateLimiterInner + 'static) -> RateLimiter {
RateLimiter {
inner: Arc::new(rate_limiter),
}
}
}
impl std::ops::Deref for RateLimiter {
type Target = dyn RateLimiterInner;
fn deref(&self) -> &Self::Target {
self.inner.as_ref()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct GraphRateLimit {
pub limit: usize,
pub duration: Duration,
}
#[derive(Debug, Clone, Default)]
pub struct KeyedRateLimitConfig {
pub rate_limiting_configs: HashMap<String, GraphRateLimit>,
}