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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
//! [<img alt="github" src="https://img.shields.io/badge/github-udoprog/leaky-bucket?style=for-the-badge&logo=github" height="20">](https://github.com/udoprog/leaky-bucket)
//! [<img alt="crates.io" src="https://img.shields.io/crates/v/leaky-bucket.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/leaky-bucket)
//! [<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-leaky-bucket?style=for-the-badge&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K" height="20">](https://docs.rs/leaky-bucket)
//! [<img alt="build status" src="https://img.shields.io/github/workflow/status/udoprog/leaky-bucket/CI/main?style=for-the-badge" height="20">](https://github.com/udoprog/leaky-bucket/actions?query=branch%3Amain)
//!
//! A token-based rate limiter based on the [leaky bucket] algorithm.
//!
//! If the bucket overflows and goes over its max configured capacity, the task
//! that tried to acquire the tokens will be suspended until the required number
//! of tokens has been drained from the bucket.
//!
//! Since this crate uses timing facilities from tokio it has to be used within
//! a Tokio runtime with the [`time` feature] enabled.
//!
//! <br>
//!
//! ## Usage
//!
//! Add the following to your `Cargo.toml`:
//!
//! ```toml
//! leaky-bucket = "0.12.0"
//! ```
//!
//! <br>
//!
//! ## Examples
//!
//! The core type is the [`RateLimiter`] type, which allows for limiting the
//! throughput of a section using its [`acquire`] and [`acquire_one`] methods.
//!
//! ```
//! use leaky_bucket::RateLimiter;
//! use std::time;
//!
//! #[tokio::main]
//! async fn main() {
//! let limiter = RateLimiter::builder()
//! .max(10)
//! .initial(0)
//! .refill(5)
//! .build();
//!
//! let start = time::Instant::now();
//!
//! println!("Waiting for permit...");
//!
//! // Should take ~400 ms to acquire in total.
//! let a = limiter.acquire(7);
//! let b = limiter.acquire(3);
//! let c = limiter.acquire(10);
//!
//! let ((), (), ()) = tokio::join!(a, b, c);
//!
//! println!(
//! "I made it in {:?}!",
//! time::Instant::now().duration_since(start)
//! );
//! }
//! ```
//!
//! <br>
//!
//! ## Implementation details
//!
//! Each rate limiter has two acquisition modes. A fast path and a slow path.
//! The fast path is used if the desired number of tokens are readily available,
//! and involves incrementing an atomic counter indicating that the acquired
//! number of tokens have been added to the bucket.
//!
//! If this counter goes over its configured maximum capacity, it overflows into
//! a slow path. Here one of the acquiring tasks will switch over to work as a
//! *core*. This is known as *core switching*.
//!
//! ```
//! use leaky_bucket::RateLimiter;
//! use std::time;
//!
//! # #[tokio::main] async fn main() {
//! let limiter = RateLimiter::builder()
//! .initial(10)
//! .interval(time::Duration::from_millis(100))
//! .build();
//!
//! // This is instantaneous since the rate limiter starts with 10 tokens to
//! // spare.
//! limiter.acquire(10).await;
//!
//! // This however needs to core switch and wait for a while until the desired
//! // number of tokens is available.
//! limiter.acquire(3).await;
//! # }
//! ```
//!
//! The core is responsible for sleeping for the configured interval so that
//! more tokens can be added. After which it ensures that any tasks that are
//! waiting to acquire including itself are appropriately unsuspended.
//!
//! On-demand core switching is what allows this rate limiter implementation to
//! work without a coordinating background thread. But we need to ensure that
//! any asynchronous tasks that uses [`RateLimiter`] must either run an
//! [`acquire`] call to completion, or be *cancelled* by being dropped.
//!
//! If none of these hold, the core might leak and be locked indefinitely
//! preventing any future use of the rate limiter from making progress. This is
//! similar to if you would lock an asynchronous [`Mutex`] but never drop its
//! guard.
//!
//! > You can run this example with:
//! >
//! > ```sh
//! > cargo run --example block-forever
//! > ```
//!
//! ```
//! use leaky_bucket::RateLimiter;
//! use std::future::Future;
//! use std::sync::Arc;
//! use std::task::Context;
//!
//! struct Waker;
//! # impl std::task::Wake for Waker { fn wake(self: Arc<Self>) { } }
//!
//! # #[tokio::main] async fn main() {
//! let limiter = Arc::new(RateLimiter::builder().build());
//!
//! let waker = Arc::new(Waker).into();
//! let mut cx = Context::from_waker(&waker);
//!
//! let mut a0 = Box::pin(limiter.acquire(1));
//! // Poll once to ensure that the core task is assigned.
//! assert!(a0.as_mut().poll(&mut cx).is_pending());
//! assert!(a0.is_core());
//!
//! // We leak the core task, preventing the rate limiter from making progress
//! // by assigning new core tasks.
//! std::mem::forget(a0);
//!
//! // Awaiting acquire here would block forever.
//! // limiter.acquire(1).await;
//! # }
//! ```
//!
//! <br>
//!
//! ## Fairness
//!
//! By default [`RateLimiter`] uses a *fair* scheduler. This ensures that the
//! core task makes progress even if there are many tasks waiting to acquire
//! tokens. As a result it causes more frequent core switching, increasing the
//! total work needed. An unfair scheduler is expected to do a bit less work
//! under contention. But without fair scheduling some tasks might end up taking
//! longer to acquire than expected.
//!
//! This behavior can be tweaked with the [`Builder::fair`] option.
//!
//! ```
//! use leaky_bucket::RateLimiter;
//!
//! let limiter = RateLimiter::builder()
//! .fair(false)
//! .build();
//! ```
//!
//! The `unfair-scheduling` example can showcase this phenomenon.
//!
//! ```sh
//! cargh run --example unfair-scheduling
//! ```
//!
//! ```text
//! # fair
//! Max: 1011ms, Total: 1012ms
//! Timings:
//! 0: 101ms
//! 1: 101ms
//! 2: 101ms
//! 3: 101ms
//! 4: 101ms
//! ...
//! # unfair
//! Max: 1014ms, Total: 1014ms
//! Timings:
//! 0: 1014ms
//! 1: 101ms
//! 2: 101ms
//! 3: 101ms
//! 4: 101ms
//! ...
//! ```
//!
//! As can be seen above the first task in the *unfair* scheduler takes longer
//! to run because it prioritises releasing other tasks waiting to acquire over
//! itself.
//!
//! [`acquire_one`]: https://docs.rs/leaky-bucket/0/leaky_bucket/struct.RateLimiter.html#method.acquire_one
//! [`acquire`]: https://docs.rs/leaky-bucket/0/leaky_bucket/struct.RateLimiter.html#method.acquire
//! [`Builder::fair`]: https://docs.rs/leaky-bucket/0/leaky_bucket/struct.Builder.html#method.fair
//! [`Mutex`]: https://docs.rs/tokio/1/tokio/sync/struct.Mutex.html
//! [`RateLimiter`]: https://docs.rs/leaky-bucket/0/leaky_bucket/struct.RateLimiter.html
//! [`time` feature]: https://docs.rs/tokio/1/tokio/#feature-flags
//! [leaky bucket]: https://en.wikipedia.org/wiki/Leaky_bucket
#![deny(missing_docs)]
#![deny(rustdoc::missing_doc_code_examples)]
use parking_lot::{Mutex, MutexGuard};
use std::cell::UnsafeCell;
use std::convert::TryFrom as _;
use std::fmt;
use std::future::Future;
use std::marker;
use std::mem;
use std::pin::Pin;
use std::ptr;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::task::{Context, Poll, Waker};
use tokio::time;
use tracing::trace;
#[doc(hidden)]
pub mod linked_list;
use self::linked_list::{LinkedList, Node};
/// Default factor for how to calculate max refill value.
const DEFAULT_REFILL_MAX_FACTOR: usize = 10;
/// Interval to bump the shared mutex guard to allow other parts of the system
/// to make process. Processes which loop should use this number to determine
/// how many times it should loop before calling [MutexGuard::bump].
///
/// If we do not respect this limit we might inadvertently end up starving other
/// tasks from making progress so that they can unblock.
const BUMP_LIMIT: usize = 16;
/// Linked task state.
struct Task {
/// Remaining tokens that need to be satisfied.
remaining: usize,
/// Link to [Linking::complete].
complete: Option<ptr::NonNull<AtomicBool>>,
/// The waker associated with the node.
waker: Option<Waker>,
}
impl Task {
/// Construct a new task state with the given permits remaining.
const fn new() -> Self {
Self {
remaining: 0,
complete: None,
waker: None,
}
}
/// Test if the current node is completed.
fn is_completed(&self) -> bool {
self.remaining == 0
}
/// Fill the current node from the given pool of tokens and modify it.
fn fill(&mut self, current: &mut usize) {
let removed = usize::min(self.remaining, *current);
self.remaining -= removed;
*current -= removed;
}
}
/// A borrowed rate limiter.
struct BorrowedRateLimiter<'a>(&'a RateLimiter);
impl AsRef<RateLimiter> for BorrowedRateLimiter<'_> {
fn as_ref(&self) -> &RateLimiter {
self.0
}
}
struct Critical {
/// Current balance of tokens. A value of 0 means that it is empty. Goes up
/// to [`RateLimiter::max`].
balance: usize,
/// Waiter list.
waiters: LinkedList<Task>,
/// The deadline for when more tokens can be be added.
deadline: time::Instant,
/// If the core is available.
available: bool,
}
impl Critical {
/// Release the current core. Beyond this point the current task may no
/// longer interact exclusively with the core.
#[tracing::instrument(skip(self), level = "trace")]
fn release(&mut self) {
trace!("releasing core");
self.available = true;
// Find another task that might take over as core. Once it has acquired
// core status it will have to make sure it is no longer linked into the
// wait queue.
//
// We have to do this, because another task might miss that the core is
// available since it's hidden behind an atomic, so we wake any task up
// to ensure that it will always be picked up.
//
// Safety: We're holding the lock guard to all the waiters so we can be
// certain that we have exclusive access.
unsafe {
if let Some(mut node) = self.waiters.front_mut() {
trace!(node = ?node, "waking next core");
if let Some(waker) = node.as_mut().waker.take() {
waker.wake();
}
}
}
}
}
/// A token-bucket rate limiter.
pub struct RateLimiter {
/// Tokens to add every `per` duration.
refill: usize,
/// Interval in milliseconds to add tokens.
interval: time::Duration,
/// Max number of tokens associated with the rate limiter.
max: usize,
/// If the rate limiter is fair or not.
fair: bool,
/// Critical state of the rate limiter.
critical: Mutex<Critical>,
}
impl RateLimiter {
/// Construct a new [`Builder`] for a [`RateLimiter`].
///
/// # Examples
///
/// ```
/// use leaky_bucket::RateLimiter;
/// use std::time::Duration;
///
/// let limiter = RateLimiter::builder()
/// .initial(100)
/// .refill(100)
/// .max(1000)
/// .interval(Duration::from_millis(250))
/// .fair(false)
/// .build();
/// ```
pub fn builder() -> Builder {
Builder::default()
}
/// Get the current token balance.
///
/// This indicates how many tokens can be requested without blocking.
///
/// # Examples
///
/// ```
/// use leaky_bucket::RateLimiter;
///
/// # #[tokio::main] async fn main() {
/// let limiter = RateLimiter::builder()
/// .initial(100)
/// .build();
///
/// assert_eq!(limiter.balance(), 100);
/// limiter.acquire(10).await;
/// assert_eq!(limiter.balance(), 90);
/// # }
/// ```
pub fn balance(&self) -> usize {
self.critical.lock().balance
}
/// Acquire a single permit.
///
/// # Examples
///
/// ```
/// use leaky_bucket::RateLimiter;
///
/// # #[tokio::main] async fn main() {
/// let limiter = RateLimiter::builder()
/// .initial(10)
/// .build();
///
/// limiter.acquire_one().await;
/// # }
/// ```
pub fn acquire_one(&self) -> Acquire<'_> {
self.acquire(1)
}
/// Acquire the given number of permits, suspending the current task until
/// they are available.
///
/// If zero permits are specified, this function never suspends the current
/// task.
///
/// # Examples
///
/// ```
/// use leaky_bucket::RateLimiter;
///
/// # #[tokio::main] async fn main() {
/// let limiter = RateLimiter::builder()
/// .initial(10)
/// .build();
///
/// limiter.acquire(10).await;
/// # }
/// ```
pub fn acquire(&self, permits: usize) -> Acquire<'_> {
Acquire(AcquireFut::new(BorrowedRateLimiter(self), permits))
}
/// Acquire a permit using an owned future.
///
/// If zero permits are specified, this function never suspends the current
/// task.
///
/// This required the [`RateLimiter`] to be wrapped inside of an
/// [`std::sync::Arc`] but will in contrast permit the acquire operation to
/// be owned by another struct making it more suitable for embedding.
///
/// # Examples
///
/// ```
/// use leaky_bucket::RateLimiter;
/// use std::sync::Arc;
///
/// # #[tokio::main] async fn main() {
/// let limiter = Arc::new(RateLimiter::builder().initial(10).build());
///
/// limiter.acquire_owned(10).await;
/// # }
/// ```
///
/// Example when embedded into another future. This wouldn't be possible
/// with [`RateLimiter::acquire`] since it would otherwise hold a reference
/// to the corresponding [`RateLimiter`] instance.
///
/// ```
/// use leaky_bucket::{AcquireOwned, RateLimiter};
/// use pin_project::pin_project;
/// use std::future::Future;
/// use std::pin::Pin;
/// use std::sync::Arc;
/// use std::task::{Context, Poll};
/// use std::time::Duration;
///
/// #[pin_project]
/// struct MyFuture {
/// limiter: Arc<RateLimiter>,
/// #[pin]
/// acquire: Option<AcquireOwned>,
/// }
///
/// impl Future for MyFuture {
/// type Output = ();
///
/// fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
/// let mut this = self.project();
///
/// loop {
/// if let Some(acquire) = this.acquire.as_mut().as_pin_mut() {
/// futures::ready!(acquire.poll(cx));
/// return Poll::Ready(());
/// }
///
/// this.acquire.set(Some(this.limiter.clone().acquire_owned(100)));
/// }
/// }
/// }
///
/// # #[tokio::main] async fn main() {
/// let limiter = Arc::new(RateLimiter::builder().initial(100).build());
///
/// let future = MyFuture { limiter, acquire: None };
/// future.await;
/// # }
/// ```
pub fn acquire_owned(self: Arc<Self>, permits: usize) -> AcquireOwned {
AcquireOwned(AcquireFut::new(self, permits))
}
}
// Safety: All the internals of acquire is thread safe and correctly
// synchronized. The embedded waiter queue doesn't have anything inherently
// unsafe in it.
unsafe impl Send for RateLimiter {}
unsafe impl Sync for RateLimiter {}
/// A builder for a [`RateLimiter`].
pub struct Builder {
/// The max number of tokens.
max: Option<usize>,
/// The initial count of tokens.
initial: usize,
/// Tokens to add every `per` duration.
refill: usize,
/// Interval to add tokens in milliseconds.
interval: time::Duration,
/// If the rate limiter is fair or not.
fair: bool,
}
impl Builder {
/// Configure the max number of tokens to use.
///
/// If unspecified, this will default to be 2 times the [`refill`] or the
/// [`initial`] value, whichever is largest.
///
/// # Examples
///
/// ```
/// use leaky_bucket::RateLimiter;
///
/// let limiter = RateLimiter::builder()
/// .max(10_000)
/// .build();
/// ```
///
/// [`refill`]: Builder::refill
/// [`initial`]: Builder::initial
pub fn max(&mut self, max: usize) -> &mut Self {
self.max = Some(max);
self
}
/// Configure the initial number of tokens to configure. The default value
/// is `0`.
///
/// # Examples
///
/// ```
/// use leaky_bucket::RateLimiter;
///
/// let limiter = RateLimiter::builder()
/// .initial(10)
/// .build();
/// ```
pub fn initial(&mut self, initial: usize) -> &mut Self {
self.initial = initial;
self
}
/// Configure the time duration between which we add [`refill`] number to
/// the bucket rate limiter.
///
/// # Panics
///
/// This panics if the provided interval does not fit within the millisecond
/// bounds of a [usize] or is zero.
///
/// ```should_panic
/// use leaky_bucket::RateLimiter;
/// use std::time;
///
/// let limiter = RateLimiter::builder()
/// .interval(time::Duration::from_secs(u64::MAX))
/// .build();
/// ```
///
/// ```should_panic
/// use leaky_bucket::RateLimiter;
/// use std::time;
///
/// let limiter = RateLimiter::builder()
/// .interval(time::Duration::from_millis(0))
/// .build();
/// ```
///
/// # Examples
///
/// ```
/// use leaky_bucket::RateLimiter;
/// use std::time;
///
/// let limiter = RateLimiter::builder()
/// .interval(time::Duration::from_millis(100))
/// .build();
/// ```
///
/// [`refill`]: Builder::refill
pub fn interval(&mut self, interval: time::Duration) -> &mut Self {
assert! {
interval.as_millis() != 0,
"interval must be non-zero",
};
assert! {
u64::try_from(interval.as_millis()).is_ok(),
"interval must fit within a 64-bit integer"
};
self.interval = interval;
self
}
/// The number of tokens to add at each [`interval`] interval. The default
/// value is `1`.
///
/// # Panics
///
/// Panics if a refill amount of `0` is specified.
///
/// # Examples
///
/// ```
/// use leaky_bucket::RateLimiter;
/// use std::time;
///
/// let limiter = RateLimiter::builder()
/// .refill(100)
/// .build();
/// ```
///
/// [`interval`]: Builder::interval
pub fn refill(&mut self, refill: usize) -> &mut Self {
assert!(refill > 0, "refill amount cannot be zero");
self.refill = refill;
self
}
/// Configure the rate limiter to be fair. By default the rate limiter is
/// *fair* which ensures that all tasks make steady progress even under
/// contention. But an unfair scheduler might have a higher total
/// throughput.
///
/// # Examples
///
/// ```
/// use leaky_bucket::RateLimiter;
///
/// let limiter = RateLimiter::builder()
/// .refill(100)
/// .fair(false)
/// .build();
/// ```
pub fn fair(&mut self, fair: bool) -> &mut Self {
self.fair = fair;
self
}
/// Construct a new [`RateLimiter`].
///
/// # Examples
///
/// ```
/// use leaky_bucket::RateLimiter;
/// use std::time;
///
/// let limiter = RateLimiter::builder()
/// .refill(100)
/// .interval(time::Duration::from_millis(200))
/// .max(10_000)
/// .build();
/// ```
pub fn build(&self) -> RateLimiter {
let deadline = time::Instant::now() + self.interval;
let max = match self.max {
Some(max) => max,
None => usize::max(self.refill, self.initial).saturating_mul(DEFAULT_REFILL_MAX_FACTOR),
};
let initial = usize::min(self.initial, max);
RateLimiter {
refill: self.refill,
interval: self.interval,
max,
fair: self.fair,
critical: Mutex::new(Critical {
balance: initial,
waiters: LinkedList::new(),
deadline,
available: true,
}),
}
}
}
/// Construct a new builder with default options.
///
/// # Examples
///
/// ```
/// use leaky_bucket::Builder;
///
/// let limiter = Builder::default().build();
/// ```
impl Default for Builder {
fn default() -> Self {
Self {
max: None,
initial: 0,
refill: 1,
interval: time::Duration::from_millis(100),
fair: true,
}
}
}
/// The state of an acquire operation.
#[allow(clippy::large_enum_variant)]
enum State {
/// Initial unconfigured state.
Initial,
/// The acquire is waiting to be released by the core.
Waiting,
/// This operation is currently the core.
///
/// We need to take care to ensure that we don't move the configured sleep.
/// Since it needs to be pinned to be polled.
Core {
/// The current sleep of the core.
sleep: time::Sleep,
},
/// The operation is completed.
Complete,
}
/// Internal state of the acquire. This is separated because it can be computed
/// in constant time.
struct AcquireState {
/// If we are linked or not.
linked: bool,
/// Inner state of the acquire.
linking: UnsafeCell<Linking>,
}
impl AcquireState {
#[allow(clippy::declare_interior_mutable_const)]
const INITIAL: AcquireState = AcquireState {
linked: false,
linking: UnsafeCell::new(Linking {
task: Node::new(Task::new()),
complete: AtomicBool::new(false),
_pin: marker::PhantomPinned,
}),
};
/// Access the completion flag.
pub fn complete(&self) -> &AtomicBool {
// Safety: This is always safe to access since it's atomic.
unsafe {
let ptr = self.linking.get() as *const _ as *const Node<Task>;
let ptr = ptr.add(1) as *const AtomicBool;
&*ptr
}
}
/// Get the underlying task.
pub unsafe fn task(&self) -> &Node<Task> {
let ptr = self.linking.get() as *mut Node<Task>;
&*ptr
}
/// Get the underlying task mutably.
pub unsafe fn task_mut(&mut self) -> &mut Node<Task> {
let ptr = self.linking.get() as *mut Node<Task>;
&mut *ptr
}
/// Get the underlying task mutably and completion flag as a pair.
pub unsafe fn update_project(&mut self) -> (&mut Node<Task>, &AtomicBool, &mut bool) {
let node = self.linking.get() as *mut Node<Task>;
let complete = node.add(1) as *const _ as *const AtomicBool;
let node = &mut *(node as *mut Node<Task>);
let complete = &*complete;
(node, complete, &mut self.linked)
}
/// Update the waiting state for this acquisition task. This might require
/// that we update the associated waker.
#[tracing::instrument(skip(self, critical, waker), level = "trace")]
fn update(&mut self, critical: &mut MutexGuard<'_, Critical>, waker: &Waker) {
// Safety: we're ensured to do this under the critical lock since we've
// passed the relevant guard in through `waiters`.
let (task, complete, linked) = unsafe { self.update_project() };
if !*linked {
trace!("linking self");
*linked = true;
unsafe {
critical.waiters.push_front(task.into());
}
}
let w = &mut task.waker;
let new_waker = match w {
None => true,
Some(w) => !w.will_wake(waker),
};
if new_waker {
trace!("updating waker");
*w = Some(waker.clone());
}
if task.complete.is_none() {
trace!("setting complete");
task.complete = Some(complete.into());
}
}
/// Ensure that the current core task is correctly linked up if needed.
#[tracing::instrument(skip(self, critical, lim), level = "trace")]
unsafe fn link_core(&mut self, critical: &mut Critical, lim: &RateLimiter) {
if lim.fair {
// Fair scheduling needs to ensure that the core is part of the wait
// queue, and will be woken up in-order with other tasks.
if !mem::replace(&mut self.linked, true) {
critical.waiters.push_front(self.task_mut().into());
}
} else {
// Unfair scheduling the core task is not supposed to be in the wait
// queue, so remove it from there if we've successfully stolen it.
// Ensure that the current task is *not* linked since it is now to
// become the coordinator for everyone else.
if mem::take(&mut self.linked) {
critical.waiters.remove(self.task_mut().into());
}
}
}
/// Refill the wait queue with the given number of tokens.
#[tracing::instrument(skip(self, critical, lim), level = "trace")]
fn drain_wait_queue(
&self,
critical: &mut MutexGuard<'_, Critical>,
tokens: usize,
lim: &RateLimiter,
) {
critical.balance = critical.balance.saturating_add(tokens);
trace!(tokens = tokens, "draining tokens");
let mut bump = 0;
// Safety: we're holding the lock guard to all the waiters so we can be
// sure that we have exclusive access to the wait queue.
unsafe {
while critical.balance > 0 {
let mut node = match critical.waiters.pop_back() {
Some(node) => node,
None => break,
};
let n = node.as_mut();
n.fill(&mut critical.balance);
trace! {
balance = critical.balance,
remaining = n.remaining,
"filled node",
};
if !n.is_completed() {
critical.waiters.push_back(node);
break;
}
if let Some(complete) = n.complete.take() {
complete.as_ref().store(true, Ordering::Release);
}
if let Some(waker) = n.waker.take() {
waker.wake();
}
bump += 1;
if bump == BUMP_LIMIT {
MutexGuard::bump(critical);
bump = 0;
}
}
}
if critical.balance > lim.max {
critical.balance = lim.max;
}
}
/// Drain the given number of tokens through the core. Returns `true` if the
/// core has been completed.
#[tracing::instrument(skip(self, critical, tokens, lim), level = "trace")]
fn drain_core(
&mut self,
critical: &mut MutexGuard<'_, Critical>,
tokens: usize,
lim: &RateLimiter,
) -> bool {
self.drain_wait_queue(critical, tokens, lim);
if lim.fair {
debug_assert! {
self.linked,
"core must be linked for fair scheduler",
};
// We only need to check the state since the current core holder is
// linked up to the wait queue.
//
// Safety: we're doing this under the critical lock so we know we
// have exclusive access to the node.
if unsafe { self.task().is_completed() } {
// Task was unlinked by the drain action.
self.linked = false;
return true;
}
false
} else {
debug_assert! {
!self.linked,
"core must not be linked for an unfair scheduler",
};
// If the limiter is not fair, we need to in addition to draining
// remaining tokens from linked nodes, drain it from ourselves. We
// fill the current holder of the core last (self). To ensure that
// it stays around for as long as possible.
//
// Safety: we know that no one else holds the task at this point.
// The in particular the task is not linked into the wait queue.
let c = unsafe { &mut *self.task_mut() };
c.fill(&mut critical.balance);
c.is_completed()
}
}
/// Assume the current core and calculate how long we must sleep for in
/// order to do it.
///
/// # Safety
///
/// This might link the current task into the task queue, so the caller must
/// ensure that it is pinned.
#[tracing::instrument(skip(self, critical, lim), level = "trace")]
unsafe fn assume_core(
&mut self,
critical: &mut MutexGuard<'_, Critical>,
lim: &RateLimiter,
) -> bool {
self.link_core(critical, lim);
let (tokens, deadline) = match calculate_drain(critical.deadline, lim.interval) {
Some(tokens) => tokens,
None => return true,
};
// It is appropriate to update the deadline.
critical.deadline = deadline;
if self.drain_core(critical, tokens, lim) {
// We synthetically "ran" at the current time minus the remaining time
// we need to wait until the last update period.
critical.release();
return false;
}
true
}
}
impl fmt::Debug for AcquireState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AcquireState").finish()
}
}
/// The future associated with acquiring permits from a rate limiter using
/// [`RateLimiter::acquire`].
pub struct Acquire<'a>(AcquireFut<BorrowedRateLimiter<'a>>);
impl Acquire<'_> {
/// Test if this acquire task is currently coordinating the rate limiter.
///
/// # Examples
///
/// ```
/// use leaky_bucket::RateLimiter;
/// use std::future::Future;
/// use std::sync::Arc;
/// use std::task::Context;
///
/// struct Waker;
/// # impl std::task::Wake for Waker { fn wake(self: Arc<Self>) { } }
///
/// # #[tokio::main] async fn main() {
/// let limiter = RateLimiter::builder().build();
///
/// let waker = Arc::new(Waker).into();
/// let mut cx = Context::from_waker(&waker);
///
/// let a1 = limiter.acquire(1);
/// tokio::pin!(a1);
///
/// assert!(!a1.is_core());
/// assert!(a1.as_mut().poll(&mut cx).is_pending());
/// assert!(a1.is_core());
///
/// a1.as_mut().await;
///
/// // After completion this is no longer a core.
/// assert!(!a1.is_core());
/// # }
/// ```
pub fn is_core(&self) -> bool {
self.0.is_core()
}
}
impl Future for Acquire<'_> {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let inner = unsafe { Pin::map_unchecked_mut(self, |this| &mut this.0) };
inner.poll(cx)
}
}
/// The future associated with acquiring permits from a rate limiter using
/// [`RateLimiter::acquire_owned`].
pub struct AcquireOwned(AcquireFut<Arc<RateLimiter>>);
impl AcquireOwned {
/// Test if this acquire task is currently coordinating the rate limiter.
///
/// # Examples
///
/// ```
/// use leaky_bucket::RateLimiter;
/// use std::future::Future;
/// use std::sync::Arc;
/// use std::task::Context;
///
/// struct Waker;
/// # impl std::task::Wake for Waker { fn wake(self: Arc<Self>) { } }
///
/// # #[tokio::main] async fn main() {
/// let limiter = Arc::new(RateLimiter::builder().build());
///
/// let waker = Arc::new(Waker).into();
/// let mut cx = Context::from_waker(&waker);
///
/// let a1 = limiter.acquire_owned(1);
/// tokio::pin!(a1);
///
/// assert!(!a1.is_core());
/// assert!(a1.as_mut().poll(&mut cx).is_pending());
/// assert!(a1.is_core());
///
/// a1.as_mut().await;
///
/// // After completion this is no longer a core.
/// assert!(!a1.is_core());
/// # }
/// ```
pub fn is_core(&self) -> bool {
self.0.is_core()
}
}
impl Future for AcquireOwned {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let inner = unsafe { Pin::map_unchecked_mut(self, |this| &mut this.0) };
inner.poll(cx)
}
}
struct AcquireFut<T>
where
T: AsRef<RateLimiter>,
{
/// Inner shared state.
lim: T,
/// The number of permits associated with this future.
permits: usize,
/// State of the acquisition.
state: State,
/// The internal acquire state.
internal: AcquireState,
}
impl<T> AcquireFut<T>
where
T: AsRef<RateLimiter>,
{
#[inline]
fn new(lim: T, permits: usize) -> Self {
Self {
lim,
permits,
state: State::Initial,
internal: AcquireState::INITIAL,
}
}
fn is_core(&self) -> bool {
matches!(&self.state, State::Core { .. })
}
}
// Safety: All the internals of acquire is thread safe and correctly
// synchronized. The embedded waiter queue doesn't have anything inherently
// unsafe in it.
unsafe impl<T> Send for AcquireFut<T> where T: AsRef<RateLimiter> {}
unsafe impl<T> Sync for AcquireFut<T> where T: AsRef<RateLimiter> {}
impl<T> Future for AcquireFut<T>
where
T: AsRef<RateLimiter>,
{
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = unsafe { self.get_unchecked_mut() };
let lim = this.lim.as_ref();
loop {
match &mut this.state {
State::Initial => {
// Safety: The task is not linked up yet, so we can safely
// inspect the number of permits without having to
// synchronize.
if this.permits == 0 {
this.state = State::Complete;
return Poll::Ready(());
}
let mut critical = lim.critical.lock();
// If we've hit a deadline, calculate the number of tokens
// to drain and perform it in line here. This is necessary
// because the core isn't aware of how long we sleep between
// each acquire, so we need to perform some of the drain
// work here in order to avoid acruing a debt that needs to
// be filled later in.
//
// If we didn't do this, and the process slept for a long
// time, the next time a core is acquired it would be very
// far removed from the expected deadline and has no idea
// when permits were acquired, so it would over-eagerly
// release a lot of acquires and accumulate permits.
//
// This is tested for in the `test_idle` suite of tests.
if let Some((tokens, deadline)) =
calculate_drain(critical.deadline, lim.interval)
{
trace!(tokens = tokens, "inline drain");
// We pre-emptively update the deadline of the core
// since it might bump, and we don't want other
// processes to observe that the deadline has been
// reached.
critical.deadline = deadline;
this.internal.drain_wait_queue(&mut critical, tokens, lim);
}
// Test the fast path first, where we simply subtract the
// permits available from the current balance.
if let Some(balance) = critical.balance.checked_sub(this.permits) {
critical.balance = balance;
this.state = State::Complete;
return Poll::Ready(());
}
let balance = mem::take(&mut critical.balance);
// Safety: This is done in a pinned section, so we know that
// the linked section stays alive for the duration of this
// future due to pinning guarantees.
unsafe {
this.internal.task_mut().remaining = this.permits - balance;
}
// Try to take over as core. If we're unsuccessful we just
// ensure that we're linked into the wait queue.
if !mem::take(&mut critical.available) {
this.internal.update(&mut critical, cx.waker());
this.state = State::Waiting;
return Poll::Pending;
}
// Safety: This is done in a pinned section, so we know that
// the linked section stays alive for the duration of this
// future due to pinning guarantees.
unsafe { this.internal.link_core(&mut critical, lim) };
trace!(until = ?critical.deadline, "taking over core and sleeping");
this.state = State::Core {
sleep: time::sleep_until(critical.deadline),
};
trace!("no immediate tokens available");
}
State::Waiting => {
// If we are complete, then return as ready.
//
// This field is atomic, so we can safely read it under shared
// access and do not require a lock.
if this.internal.complete().load(Ordering::Acquire) {
this.state = State::Complete;
return Poll::Ready(());
}
// Note: we need to operate under this lock to ensure that
// the core acquired here (or elsewhere) observes that the
// current task has been linked up.
let mut critical = lim.critical.lock();
// Try to take over as core. If we're unsuccessful we
// just ensure that we're linked into the wait queue.
if !mem::take(&mut critical.available) {
this.internal.update(&mut critical, cx.waker());
return Poll::Pending;
}
// Safety: This is done in a pinned section, so we know that
// the linked section stays alive for the duration of this
// future due to pinning guarantees.
let assumed = unsafe { this.internal.assume_core(&mut critical, lim) };
if !assumed {
// Marks as completed.
this.state = State::Complete;
return Poll::Ready(());
}
trace!(until = ?critical.deadline, "taking over core and sleeping");
this.state = State::Core {
sleep: time::sleep_until(critical.deadline),
};
}
State::Core { sleep } => {
let mut sleep = unsafe { Pin::new_unchecked(sleep) };
if sleep.as_mut().poll(cx).is_pending() {
return Poll::Pending;
}
let now = time::Instant::now();
trace!(now = ?now, "sleep completed");
let mut critical = lim.critical.lock();
critical.deadline = now + lim.interval;
// Safety: we know that we're the only one with access to core
// because we ensured it as we acquire the `available` lock.
if this.internal.drain_core(&mut critical, lim.refill, lim) {
critical.release();
this.state = State::Complete;
return Poll::Ready(());
}
trace!(sleep = ?lim.interval, "keeping core and sleeping");
sleep.as_mut().reset(critical.deadline);
}
State::Complete => {
panic!("polled after completion");
}
}
}
}
}
impl<T> Drop for AcquireFut<T>
where
T: AsRef<RateLimiter>,
{
fn drop(&mut self) {
let lim = self.lim.as_ref();
match &mut self.state {
State::Waiting => unsafe {
debug_assert! {
self.internal.linked,
"waiting nodes have to be linked",
};
// While the node is linked into the wait queue we have to
// ensure it's only accessed under a lock, but once it's been
// unlinked we can do what we want with it.
let mut critical = lim.critical.lock();
critical.waiters.remove(self.internal.task_mut().into());
},
State::Core { .. } => unsafe {
let mut critical = lim.critical.lock();
if mem::take(&mut self.internal.linked) {
critical.waiters.remove(self.internal.task_mut().into());
}
critical.release();
},
_ => (),
}
}
}
/// All of the state that is linked into the wait queue.
///
/// This is only ever accessed through raw pointer manipulation to avoid issues
/// with field aliasing.
#[repr(C)]
struct Linking {
/// The node in the linked list.
task: Node<Task>,
/// If this node has been released or not. We make this an atomic to permit
/// access to it without synchronization.
complete: AtomicBool,
/// Avoids noalias heuristics from kicking in on references to a `Linking`
/// struct.
_pin: marker::PhantomPinned,
}
/// Calculate refill amount. Returning a tuple of how much to fill and remaining
/// duration to sleep until the next refill time if appropriate.
fn calculate_drain(
deadline: time::Instant,
interval: time::Duration,
) -> Option<(usize, time::Instant)> {
let now = time::Instant::now();
if now < deadline {
return None;
}
// Time elapsed in milliseconds since the last deadline.
let millis = interval.as_millis();
let since = now.saturating_duration_since(deadline).as_millis();
let tokens = usize::try_from(since / millis + 1).unwrap_or(usize::MAX);
let rem = u64::try_from(since % millis).unwrap_or(u64::MAX);
// Calculated time remaining until the next deadline.
let deadline = now + (interval - time::Duration::from_millis(rem));
Some((tokens, deadline))
}
#[cfg(test)]
mod tests {
use super::{Acquire, AcquireOwned, RateLimiter};
fn is_send<T: Send>() {}
fn is_sync<T: Sync>() {}
#[test]
fn assert_send_sync() {
is_send::<AcquireOwned>();
is_sync::<AcquireOwned>();
is_send::<RateLimiter>();
is_sync::<RateLimiter>();
is_send::<Acquire<'_>>();
is_sync::<Acquire<'_>>();
}
}