introduce rate limiter for sentry

This commit is contained in:
Nikita Galaiko 2023-11-14 16:58:51 +01:00 committed by GitButler
parent 839511a8e8
commit 779c731f1c
3 changed files with 100 additions and 3 deletions

85
Cargo.lock generated
View File

@ -1095,6 +1095,19 @@ dependencies = [
"syn 2.0.31",
]
[[package]]
name = "dashmap"
version = "5.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856"
dependencies = [
"cfg-if",
"hashbrown 0.14.0",
"lock_api",
"once_cell",
"parking_lot_core",
]
[[package]]
name = "debugid"
version = "0.8.0"
@ -1704,6 +1717,12 @@ version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76d3d132be6c0e6aa1534069c705a74a5997a356c0dc2f86a47765e5617c5b65"
[[package]]
name = "futures-timer"
version = "3.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e64b03909df88034c26dc1547e8970b91f98bdb65165d6a4e9110d94263dbb2c"
[[package]]
name = "futures-util"
version = "0.3.28"
@ -1939,8 +1958,10 @@ dependencies = [
"filetime",
"futures",
"git2",
"governor",
"itertools",
"md5",
"nonzero_ext",
"notify",
"notify-debouncer-full",
"num_cpus",
@ -2058,6 +2079,24 @@ dependencies = [
"system-deps 6.1.1",
]
[[package]]
name = "governor"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "821239e5672ff23e2a7060901fa622950bbd80b649cdaadd78d1c1767ed14eb4"
dependencies = [
"cfg-if",
"dashmap",
"futures",
"futures-timer",
"no-std-compat",
"nonzero_ext",
"parking_lot",
"quanta",
"rand 0.8.5",
"smallvec",
]
[[package]]
name = "group"
version = "0.13.0"
@ -2860,6 +2899,15 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c41e0c4fef86961ac6d6f8a82609f55f31b05e4fce149ac5710e439df7619ba4"
[[package]]
name = "mach2"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d0d1830bcd151a6fc4aea1369af235b36c1528fe976b8ff678683c9995eade8"
dependencies = [
"libc",
]
[[package]]
name = "malloc_buf"
version = "0.0.6"
@ -3052,6 +3100,12 @@ dependencies = [
"memoffset 0.7.1",
]
[[package]]
name = "no-std-compat"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b93853da6d84c2e3c7d730d6473e8817692dd89be387eb01b94d7f108ecb5b8c"
[[package]]
name = "nodrop"
version = "0.1.14"
@ -3068,6 +3122,12 @@ dependencies = [
"minimal-lexical",
]
[[package]]
name = "nonzero_ext"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38bf9645c8b145698bb0b18a4637dcacbc421ea49bef2317e4fd8065a387cf21"
[[package]]
name = "notify"
version = "6.1.1"
@ -3822,6 +3882,22 @@ dependencies = [
"bytemuck",
]
[[package]]
name = "quanta"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a17e662a7a8291a865152364c20c7abc5e60486ab2001e8ec10b24862de0b9ab"
dependencies = [
"crossbeam-utils",
"libc",
"mach2",
"once_cell",
"raw-cpuid",
"wasi 0.11.0+wasi-snapshot-preview1",
"web-sys",
"winapi",
]
[[package]]
name = "quick-xml"
version = "0.29.0"
@ -3943,6 +4019,15 @@ dependencies = [
"rand_core 0.5.1",
]
[[package]]
name = "raw-cpuid"
version = "10.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c297679cb867470fa8c9f67dbba74a78d78e3e98d7cf2b08d6d71540f797332"
dependencies = [
"bitflags 1.3.2",
]
[[package]]
name = "raw-window-handle"
version = "0.5.2"

View File

@ -32,11 +32,14 @@ diffy = "0.3.0"
filetime = "0.2.22"
futures = "0.3"
git2 = { version = "0.18.1", features = ["vendored-openssl", "vendored-libgit2"] }
governor = "0.6.0"
itertools = "0.11"
md5 = "0.7.0"
nonzero_ext = "0.3.0"
notify = { version = "6.0.1" }
notify-debouncer-full = "0.3.1"
num_cpus = "1.16.0"
once_cell = "1.0"
r2d2 = "0.8.10"
r2d2_sqlite = "0.22.0"
rand = "0.8.5"

View File

@ -1,5 +1,12 @@
use std::sync::Arc;
use governor::{
clock::QuantaClock,
state::{InMemoryState, NotKeyed},
Quota, RateLimiter,
};
use nonzero_ext::nonzero;
use once_cell::sync::OnceCell;
use sentry::ClientInitGuard;
use sentry_tracing::SentryLayer;
use tauri::PackageInfo;
@ -8,6 +15,9 @@ use tracing_subscriber::registry::LookupSpan;
use crate::users;
static SENTRY_QUOTA: Quota = Quota::per_second(nonzero!(1_u32)); // 1 per second at most.
static SENTRY_LIMIT: OnceCell<RateLimiter<NotKeyed, InMemoryState, QuantaClock>> = OnceCell::new();
/// Should be called once on application startup, and the returned guard should be kept alive for
/// the lifetime of the application.
pub fn init(package_info: &PackageInfo) -> ClientInitGuard {
@ -19,9 +29,8 @@ pub fn init(package_info: &PackageInfo) -> ClientInitGuard {
_ => "unknown",
}.into()),
release: Some(package_info.version.to_string().into()),
before_send: Some(Arc::new(|event| {
Some(event)
})),
before_send: Some({
Arc::new(|event| SENTRY_LIMIT.get_or_init(|| RateLimiter::direct(SENTRY_QUOTA)).check().is_ok().then_some(event))}),
attach_stacktrace: true,
default_integrations: true,
..Default::default()