From 3e559a1a7a425f384a869ef41d19d07134082807 Mon Sep 17 00:00:00 2001 From: Alex Snaps Date: Mon, 25 Nov 2024 11:03:23 -0500 Subject: [PATCH] Added source to existing errors Signed-off-by: Alex Snaps --- Cargo.lock | 180 +++++++++---------- limitador/src/errors.rs | 6 +- limitador/src/lib.rs | 40 ++--- limitador/src/storage/disk/expiring_value.rs | 3 +- limitador/src/storage/disk/mod.rs | 4 +- limitador/src/storage/mod.rs | 19 +- limitador/src/storage/redis/mod.rs | 10 +- limitador/src/storage/redis/redis_sync.rs | 1 + 8 files changed, 139 insertions(+), 124 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 73f343ae..5b7ed5c2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -65,7 +65,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e01ed3140b2f8d422c68afa1ed2e85d996ea619c988ac834d255db32138655cb" dependencies = [ "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -183,7 +183,7 @@ dependencies = [ "actix-router", "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -324,7 +324,7 @@ checksum = "c7c24de15d275a1ecfd47a380fb4d5ec9bfe0933f309ed5e705b775596a3574d" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -335,7 +335,7 @@ checksum = "721cae7de5c34fbb2acd27e21e6d2cf7b886dce0c27388d46c4e6c47ea4318dd" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -389,9 +389,9 @@ dependencies = [ [[package]] name = "axum" -version = "0.7.7" +version = "0.7.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "504e3947307ac8326a5437504c517c4b56716c9d98fac0028c2acc7ca47d70ae" +checksum = "edca88bc138befd0323b20752846e6587272d3b03b0343c8ea28a6f819e6e71f" dependencies = [ "async-trait", "axum-core 0.4.5", @@ -408,7 +408,7 @@ dependencies = [ "pin-project-lite", "rustversion", "serde", - "sync_wrapper 1.0.1", + "sync_wrapper 1.0.2", "tower 0.5.1", "tower-layer", "tower-service", @@ -446,7 +446,7 @@ dependencies = [ "mime", "pin-project-lite", "rustversion", - "sync_wrapper 1.0.1", + "sync_wrapper 1.0.2", "tower-layer", "tower-service", ] @@ -495,7 +495,7 @@ dependencies = [ "regex", "rustc-hash", "shlex", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -560,9 +560,9 @@ checksum = "9ac0150caa2ae65ca5bd83f25c7de183dea78d4d366469f148435e2acfbad0da" [[package]] name = "bytestring" -version = "1.3.1" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74d80203ea6b29df88012294f62733de21cfeab47f17b41af3a38bc30a03ee72" +checksum = "e465647ae23b2823b0753f50decb2d5a86d2bb2cac04788fafd1f80e45378e5f" dependencies = [ "bytes", ] @@ -765,9 +765,9 @@ checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" [[package]] name = "cpufeatures" -version = "0.2.15" +version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ca741a962e1b0bff6d724a1a0958b686406e853bb14061f218562e1896f95e6" +checksum = "16b80225097f2e5ae4e7179dd2266824648f3e2f49d9134d584b76389d31c4c3" dependencies = [ "libc", ] @@ -908,7 +908,7 @@ dependencies = [ "proc-macro2", "quote", "rustc_version", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -929,7 +929,7 @@ checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -1104,7 +1104,7 @@ checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -1191,9 +1191,9 @@ dependencies = [ [[package]] name = "h2" -version = "0.4.6" +version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "524e8ac6999421f49a846c2d4411f337e53497d8ec55d67753beffa43c5d9205" +checksum = "ccae279728d634d083c00f6099cb58f01cc99c145b84b8be2f6c74618d79922e" dependencies = [ "atomic-waker", "bytes", @@ -1244,9 +1244,9 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.15.1" +version = "0.15.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a9bfc1af68b1726ea47d3d5109de126281def866b33970e10fbab11b5dafab3" +checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" [[package]] name = "heapless" @@ -1380,14 +1380,14 @@ dependencies = [ [[package]] name = "hyper" -version = "1.5.0" +version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbbff0a806a4728c99295b254c8838933b5b082d75e3cb70c8dab21fdfbcfa9a" +checksum = "97818827ef4f364230e16705d4706e2897df2bb60617d6ca15d598025a3c481f" dependencies = [ "bytes", "futures-channel", "futures-util", - "h2 0.4.6", + "h2 0.4.7", "http 1.1.0", "http-body 1.0.1", "httparse", @@ -1417,7 +1417,7 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b90d566bffbce6a75bd8b09a05aa8c2cb1fabb6cb348f8840c9e4c90a0d83b0" dependencies = [ - "hyper 1.5.0", + "hyper 1.5.1", "hyper-util", "pin-project-lite", "tokio", @@ -1432,7 +1432,7 @@ checksum = "70206fc6890eaca9fde8a0bf71caa2ddfc9fe045ac9e5c70df101a7dbde866e0" dependencies = [ "bytes", "http-body-util", - "hyper 1.5.0", + "hyper 1.5.1", "hyper-util", "native-tls", "tokio", @@ -1451,7 +1451,7 @@ dependencies = [ "futures-util", "http 1.1.0", "http-body 1.0.1", - "hyper 1.5.0", + "hyper 1.5.1", "pin-project-lite", "socket2", "tokio", @@ -1574,7 +1574,7 @@ checksum = "1ec89e9337638ecdc08744df490b221a7399bf8d164eb52a665454e60e075ad6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -1621,7 +1621,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" dependencies = [ "equivalent", - "hashbrown 0.15.1", + "hashbrown 0.15.2", ] [[package]] @@ -1705,9 +1705,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.11" +version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" +checksum = "540654e97a3f4470a492cd30ff187bc95d89557a903a2bbf112e2fae98104ef2" [[package]] name = "jobserver" @@ -1767,9 +1767,9 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" [[package]] name = "libc" -version = "0.2.162" +version = "0.2.164" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398" +checksum = "433bfe06b8c75da9b2e3fbea6e5329ff87748f0b144ef75306e674c3f6f7c13f" [[package]] name = "libloading" @@ -1828,7 +1828,7 @@ dependencies = [ "cfg-if", "criterion", "dashmap", - "h2 0.4.6", + "h2 0.4.7", "metrics 0.24.0", "moka", "paste", @@ -1899,9 +1899,9 @@ checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" [[package]] name = "litemap" -version = "0.7.3" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "643cb0b8d4fcc284004d5fd0d67ccf61dfffadb7f75e1e71bc420f4688a3a704" +checksum = "4ee93343901ab17bd981295f2cf0026d4ad018c7c31ba84549a4ddbb47a45104" [[package]] name = "local-channel" @@ -1995,7 +1995,7 @@ checksum = "5d58e362dc7206e9456ddbcdbd53c71ba441020e62104703075a69151e38d85f" dependencies = [ "base64 0.22.1", "http-body-util", - "hyper 1.5.0", + "hyper 1.5.1", "hyper-tls", "hyper-util", "indexmap 2.6.0", @@ -2255,7 +2255,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -2266,9 +2266,9 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" [[package]] name = "openssl-src" -version = "300.4.0+3.4.0" +version = "300.4.1+3.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a709e02f2b4aca747929cca5ed248880847c650233cf8b8cdc48f40aaf4898a6" +checksum = "faa4eac4138c62414b5622d1b31c5c304f34b406b013c079c2bbc652fdd6678c" dependencies = [ "cc", ] @@ -2525,7 +2525,7 @@ checksum = "3c0f5fad0874fc7abcd4d750e76917eaebbecaa2c20bde22e1dbeeba8beb758c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -2576,9 +2576,9 @@ dependencies = [ [[package]] name = "portable-atomic" -version = "1.9.0" +version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc9c68a3f6da06753e9335d63e27f6b9754dd1920d941135b7ea8224f141adb2" +checksum = "280dc24453071f1b63954171985a0b0d30058d287960968b9b2aca264c8d4ee6" [[package]] name = "postcard" @@ -2615,7 +2615,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "64d1ec885c64d0457d564db4ec299b2dae3f9c02808b8ad9c3a089c591b18033" dependencies = [ "proc-macro2", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -2637,14 +2637,14 @@ dependencies = [ "proc-macro-error-attr2", "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] name = "proc-macro2" -version = "1.0.89" +version = "1.0.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" dependencies = [ "unicode-ident", ] @@ -2686,7 +2686,7 @@ dependencies = [ "prost 0.13.3", "prost-types", "regex", - "syn 2.0.87", + "syn 2.0.89", "tempfile", ] @@ -2700,7 +2700,7 @@ dependencies = [ "itertools 0.12.1", "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -2713,7 +2713,7 @@ dependencies = [ "itertools 0.13.0", "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -2948,9 +2948,9 @@ dependencies = [ [[package]] name = "rustix" -version = "0.38.40" +version = "0.38.41" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99e4ea3e1cdc4b559b8e5650f9c8e5998e3e5c1343b4eaf034565f32318d63c0" +checksum = "d7f649912bc1495e167a6edee79151c84b1bad49748cb4f1f1167f459f6224f6" dependencies = [ "bitflags 2.6.0", "errno", @@ -2991,9 +2991,9 @@ dependencies = [ [[package]] name = "schannel" -version = "0.1.26" +version = "0.1.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01227be5826fa0690321a2ba6c5cd57a19cf3f6a09e76973b58e61de6ab9d1c1" +checksum = "1f29ebaa345f945cec9fbbc532eb307f0fdad8161f281b6369539c8d84876b3d" dependencies = [ "windows-sys 0.59.0", ] @@ -3065,14 +3065,14 @@ checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] name = "serde_json" -version = "1.0.132" +version = "1.0.133" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d726bfaff4b320266d395898905d0eba0345aae23b54aee3a737e260fd46db03" +checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377" dependencies = [ "itoa", "memchr", @@ -3127,7 +3127,7 @@ checksum = "5d69265a08751de7844521fd15003ae0a888e035773ba05695c5c759a6f89eef" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -3255,9 +3255,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.87" +version = "2.0.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" +checksum = "44d46482f1c1c87acd84dea20c1bf5ebff4c757009ed6bf19cfd36fb10e92c4e" dependencies = [ "proc-macro2", "quote", @@ -3272,9 +3272,9 @@ checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" [[package]] name = "sync_wrapper" -version = "1.0.1" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7065abeca94b6a8a577f9bd45aa0867a2238b74e8eb67cf10d492bc39351394" +checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" [[package]] name = "synstructure" @@ -3284,7 +3284,7 @@ checksum = "c8af7666ab7b6390ab78131fb5b0fce11d6b7a6951602017c35fa82800708971" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -3346,7 +3346,7 @@ checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -3357,7 +3357,7 @@ checksum = "f077553d607adc1caf65430528a576c757a71ed73944b66ebb58ef2bbd243568" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -3457,7 +3457,7 @@ checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -3540,14 +3540,14 @@ checksum = "877c5b330756d856ffcc4553ab34a5684481ade925ecc54bcd1bf02b1d0d4d52" dependencies = [ "async-stream", "async-trait", - "axum 0.7.7", + "axum 0.7.9", "base64 0.22.1", "bytes", - "h2 0.4.6", + "h2 0.4.7", "http 1.1.0", "http-body 1.0.1", "http-body-util", - "hyper 1.5.0", + "hyper 1.5.1", "hyper-timeout 0.5.2", "hyper-util", "percent-encoding", @@ -3573,7 +3573,7 @@ dependencies = [ "prost-build", "prost-types", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -3655,7 +3655,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -3741,9 +3741,9 @@ checksum = "7e51b68083f157f853b6379db119d1c1be0e6e4dec98101079dec41f6f5cf6df" [[package]] name = "unicode-ident" -version = "1.0.13" +version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" +checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" [[package]] name = "unicode-xid" @@ -3759,9 +3759,9 @@ checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861" [[package]] name = "url" -version = "2.5.3" +version = "2.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d157f1b96d14500ffdc1f10ba712e780825526c03d9a49b4d0324b0d9113ada" +checksum = "32f8b686cadd1473f4bd0117a5d28d36b1ade384ea9b5069a1c40aefed7fda60" dependencies = [ "form_urlencoded", "idna", @@ -3867,7 +3867,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", "wasm-bindgen-shared", ] @@ -3889,7 +3889,7 @@ checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -3981,7 +3981,7 @@ checksum = "9107ddc059d5b6fbfbffdfa7a7fe3e22a226def0b2608f72e9d552763d3e1ad7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -3992,7 +3992,7 @@ checksum = "29bee4b38ea3cde66011baa44dba677c432a78593e202392d1e9070cf2a7fca7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] @@ -4100,9 +4100,9 @@ checksum = "1e9df38ee2d2c3c5948ea468a8406ff0db0b29ae1ffde1bcf20ef305bcc95c51" [[package]] name = "yoke" -version = "0.7.4" +version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c5b1314b079b0930c31e3af543d8ee1757b1951ae1e1565ec704403a7240ca5" +checksum = "120e6aef9aa629e3d4f52dc8cc43a015c7724194c97dfaf45180d2daf2b77f40" dependencies = [ "serde", "stable_deref_trait", @@ -4112,13 +4112,13 @@ dependencies = [ [[package]] name = "yoke-derive" -version = "0.7.4" +version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28cc31741b18cb6f1d5ff12f5b7523e3d6eb0852bbbad19d73905511d9849b95" +checksum = "2380878cad4ac9aac1e2435f3eb4020e8374b5f13c296cb75b4620ff8e229154" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", "synstructure", ] @@ -4140,27 +4140,27 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] name = "zerofrom" -version = "0.1.4" +version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91ec111ce797d0e0784a1116d0ddcdbea84322cd79e5d5ad173daeba4f93ab55" +checksum = "cff3ee08c995dee1859d998dea82f7374f2826091dd9cd47def953cae446cd2e" dependencies = [ "zerofrom-derive", ] [[package]] name = "zerofrom-derive" -version = "0.1.4" +version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ea7b4a3637ea8669cedf0f1fd5c286a17f3de97b8dd5a70a6c167a1730e63a5" +checksum = "595eed982f7d355beb85837f651fa22e90b3c044842dc7f2c2842c086f295808" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", "synstructure", ] @@ -4183,7 +4183,7 @@ checksum = "6eafa6dfb17584ea3e2bd6e76e0cc15ad7af12b09abdd1ca55961bed9b1063c6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.87", + "syn 2.0.89", ] [[package]] diff --git a/limitador/src/errors.rs b/limitador/src/errors.rs index 48df32ea..2bd4e98c 100644 --- a/limitador/src/errors.rs +++ b/limitador/src/errors.rs @@ -1,14 +1,14 @@ use crate::storage::StorageErr; use thiserror::Error; -#[derive(Error, Debug, Eq, PartialEq)] +#[derive(Error, Debug)] pub enum LimitadorError { #[error("error while accessing the limits storage: {0:?}")] - Storage(String), + Storage(StorageErr), } impl From for LimitadorError { fn from(e: StorageErr) -> Self { - Self::Storage(e.msg().to_owned()) + Self::Storage(e) } } diff --git a/limitador/src/lib.rs b/limitador/src/lib.rs index fd125d38..239e5d2c 100644 --- a/limitador/src/lib.rs +++ b/limitador/src/lib.rs @@ -221,6 +221,8 @@ pub struct RateLimiterBuilder { storage: Storage, } +type LimitadorResult = Result; + pub struct CheckResult { pub limited: bool, pub counters: Vec, @@ -336,7 +338,7 @@ impl RateLimiter { self.storage.add_limit(limit) } - pub fn delete_limit(&self, limit: &Limit) -> Result<(), LimitadorError> { + pub fn delete_limit(&self, limit: &Limit) -> LimitadorResult<()> { self.storage.delete_limit(limit)?; Ok(()) } @@ -349,7 +351,7 @@ impl RateLimiter { .collect() } - pub fn delete_limits(&self, namespace: &Namespace) -> Result<(), LimitadorError> { + pub fn delete_limits(&self, namespace: &Namespace) -> LimitadorResult<()> { self.storage.delete_limits(namespace)?; Ok(()) } @@ -359,7 +361,7 @@ impl RateLimiter { namespace: &Namespace, values: &HashMap, delta: u64, - ) -> Result { + ) -> LimitadorResult { let counters = self.counters_that_apply(namespace, values)?; for counter in counters { @@ -381,7 +383,7 @@ impl RateLimiter { namespace: &Namespace, values: &HashMap, delta: u64, - ) -> Result<(), LimitadorError> { + ) -> LimitadorResult<()> { let counters = self.counters_that_apply(namespace, values)?; counters @@ -396,7 +398,7 @@ impl RateLimiter { values: &HashMap, delta: u64, load_counters: bool, - ) -> Result { + ) -> LimitadorResult { let mut counters = self.counters_that_apply(namespace, values)?; if counters.is_empty() { @@ -431,7 +433,7 @@ impl RateLimiter { } } - pub fn get_counters(&self, namespace: &Namespace) -> Result, LimitadorError> { + pub fn get_counters(&self, namespace: &Namespace) -> LimitadorResult> { self.storage .get_counters(namespace) .map_err(|err| err.into()) @@ -440,10 +442,7 @@ impl RateLimiter { // Deletes all the limits stored except the ones received in the params. For // every limit received, if it does not exist, it is created. If it already // exists, its associated counters are not reset. - pub fn configure_with( - &self, - limits: impl IntoIterator, - ) -> Result<(), LimitadorError> { + pub fn configure_with(&self, limits: impl IntoIterator) -> LimitadorResult<()> { let limits_to_keep_or_create = classify_limits_by_namespace(limits); let namespaces_limits_to_keep_or_create: HashSet = @@ -479,7 +478,7 @@ impl RateLimiter { &self, namespace: &Namespace, values: &HashMap, - ) -> Result, LimitadorError> { + ) -> LimitadorResult> { let limits = self.storage.get_limits(namespace); let counters = limits @@ -512,7 +511,7 @@ impl AsyncRateLimiter { self.storage.add_limit(limit) } - pub async fn delete_limit(&self, limit: &Limit) -> Result<(), LimitadorError> { + pub async fn delete_limit(&self, limit: &Limit) -> LimitadorResult<()> { self.storage.delete_limit(limit).await?; Ok(()) } @@ -525,7 +524,7 @@ impl AsyncRateLimiter { .collect() } - pub async fn delete_limits(&self, namespace: &Namespace) -> Result<(), LimitadorError> { + pub async fn delete_limits(&self, namespace: &Namespace) -> LimitadorResult<()> { self.storage.delete_limits(namespace).await?; Ok(()) } @@ -535,7 +534,7 @@ impl AsyncRateLimiter { namespace: &Namespace, values: &HashMap, delta: u64, - ) -> Result { + ) -> LimitadorResult { let counters = self.counters_that_apply(namespace, values).await?; for counter in counters { @@ -556,7 +555,7 @@ impl AsyncRateLimiter { namespace: &Namespace, values: &HashMap, delta: u64, - ) -> Result<(), LimitadorError> { + ) -> LimitadorResult<()> { let counters = self.counters_that_apply(namespace, values).await?; for counter in counters { @@ -572,7 +571,7 @@ impl AsyncRateLimiter { values: &HashMap, delta: u64, load_counters: bool, - ) -> Result { + ) -> LimitadorResult { // the above where-clause is needed in order to call unwrap(). let mut counters = self.counters_that_apply(namespace, values).await?; @@ -609,10 +608,7 @@ impl AsyncRateLimiter { } } - pub async fn get_counters( - &self, - namespace: &Namespace, - ) -> Result, LimitadorError> { + pub async fn get_counters(&self, namespace: &Namespace) -> LimitadorResult> { self.storage .get_counters(namespace) .await @@ -625,7 +621,7 @@ impl AsyncRateLimiter { pub async fn configure_with( &self, limits: impl IntoIterator, - ) -> Result<(), LimitadorError> { + ) -> LimitadorResult<()> { let limits_to_keep_or_create = classify_limits_by_namespace(limits); let namespaces_limits_to_keep_or_create: HashSet = @@ -661,7 +657,7 @@ impl AsyncRateLimiter { &self, namespace: &Namespace, values: &HashMap, - ) -> Result, LimitadorError> { + ) -> LimitadorResult> { let limits = self.storage.get_limits(namespace); let counters = limits diff --git a/limitador/src/storage/disk/expiring_value.rs b/limitador/src/storage/disk/expiring_value.rs index 2932d6db..8ed4c48c 100644 --- a/limitador/src/storage/disk/expiring_value.rs +++ b/limitador/src/storage/disk/expiring_value.rs @@ -95,9 +95,10 @@ impl From for Vec { } impl From for StorageErr { - fn from(_: TryFromSliceError) -> Self { + fn from(e: TryFromSliceError) -> Self { Self { msg: "Corrupted byte sequence while reading 8 bytes for 64-bit integer".to_owned(), + source: Some(Box::new(e)), transient: false, } } diff --git a/limitador/src/storage/disk/mod.rs b/limitador/src/storage/disk/mod.rs index 1203313a..3bb288d7 100644 --- a/limitador/src/storage/disk/mod.rs +++ b/limitador/src/storage/disk/mod.rs @@ -8,9 +8,11 @@ pub use rocksdb_storage::RocksDbStorage as DiskStorage; impl From for StorageErr { fn from(error: rocksdb::Error) -> Self { + let transient = error.kind() == ErrorKind::TimedOut || error.kind() == ErrorKind::TryAgain; Self { msg: format!("Underlying storage error: {error}"), - transient: error.kind() == ErrorKind::TimedOut || error.kind() == ErrorKind::TryAgain, + source: Some(Box::new(error)), + transient, } } } diff --git a/limitador/src/storage/mod.rs b/limitador/src/storage/mod.rs index 403d21a6..06c42da6 100644 --- a/limitador/src/storage/mod.rs +++ b/limitador/src/storage/mod.rs @@ -3,8 +3,9 @@ use crate::limit::{Limit, Namespace}; use crate::InMemoryStorage; use async_trait::async_trait; use std::collections::{HashMap, HashSet}; +use std::error::Error; +use std::fmt::{Display, Formatter}; use std::sync::{Arc, RwLock}; -use thiserror::Error; #[cfg(feature = "disk_storage")] pub mod disk; @@ -308,13 +309,25 @@ pub trait AsyncCounterStorage: Sync + Send { async fn clear(&self) -> Result<(), StorageErr>; } -#[derive(Error, Debug)] -#[error("error while accessing the limits storage: {msg}")] +#[derive(Debug)] pub struct StorageErr { msg: String, + source: Option>, transient: bool, } +impl Display for StorageErr { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "error while accessing the limits storage: {}", self.msg) + } +} + +impl Error for StorageErr { + fn source(&self) -> Option<&(dyn Error + 'static)> { + self.source.as_ref().map(|source| source.as_ref()) + } +} + impl StorageErr { pub fn msg(&self) -> &str { &self.msg diff --git a/limitador/src/storage/redis/mod.rs b/limitador/src/storage/redis/mod.rs index 93167908..dbdad8b3 100644 --- a/limitador/src/storage/redis/mod.rs +++ b/limitador/src/storage/redis/mod.rs @@ -21,12 +21,14 @@ pub use redis_sync::RedisStorage; impl From for StorageErr { fn from(e: RedisError) -> Self { + let transient = e.is_timeout() + || e.is_connection_dropped() + || e.is_cluster_error() + || e.is_connection_refusal(); Self { msg: e.to_string(), - transient: e.is_timeout() - || e.is_connection_dropped() - || e.is_cluster_error() - || e.is_connection_refusal(), + source: Some(Box::new(e)), + transient, } } } diff --git a/limitador/src/storage/redis/redis_sync.rs b/limitador/src/storage/redis/redis_sync.rs index f9b655fa..9e136096 100644 --- a/limitador/src/storage/redis/redis_sync.rs +++ b/limitador/src/storage/redis/redis_sync.rs @@ -236,6 +236,7 @@ impl From<::r2d2::Error> for StorageErr { fn from(e: ::r2d2::Error) -> Self { Self { msg: e.to_string(), + source: Some(Box::new(e)), transient: false, } }