|
1 | | -<!-- |
2 | | -This README describes the package. If you publish this package to pub.dev, |
3 | | -this README's contents appear on the landing page for your package. |
| 1 | +# token_bucket_algorithm |
4 | 2 |
|
5 | | -For information about how to write a good package README, see the guide for |
6 | | -[writing package pages](https://dart.dev/guides/libraries/writing-package-pages). |
| 3 | +[](https://pub.dev/packages/token_bucket_algorithm) |
| 4 | +[](https://github.com/splashbyte/dart_token_bucket_algorithm) |
| 5 | +[](https://pub.dev/packages/token_bucket_algorithm/score) |
| 6 | +[](https://pub.dev/packages/token_bucket_algorithm/score) |
| 7 | +[](https://pub.dev/packages/token_bucket_algorithm/score) |
| 8 | +[](https://github.com/SplashByte/dart_token_bucket_algorithm/blob/main/LICENSE) |
| 9 | +[](https://codecov.io/gh/splashbyte/dart_token_bucket_algorithm) |
7 | 10 |
|
8 | | -For general information about developing packages, see the Dart guide for |
9 | | -[creating packages](https://dart.dev/guides/libraries/create-library-packages) |
10 | | -and the Flutter guide for |
11 | | -[developing packages and plugins](https://flutter.dev/developing-packages). |
12 | | ---> |
| 11 | +This Dart package provides rate limiting by using an implementation of the token bucket algorithm. |
13 | 12 |
|
14 | | -TODO: Put a short description of the package here that helps potential users |
15 | | -know whether this package might be useful for them. |
16 | | - |
17 | | -## Features |
18 | | - |
19 | | -TODO: List what your package can do. Maybe include images, gifs, or videos. |
20 | | - |
21 | | -## Getting started |
22 | | - |
23 | | -TODO: List prerequisites and provide or point to information on how to |
24 | | -start using the package. |
25 | | - |
26 | | -## Usage |
27 | | - |
28 | | -TODO: Include short and useful examples for package users. Add longer examples |
29 | | -to `/example` folder. |
| 13 | +## Simple usage |
30 | 14 |
|
31 | 15 | ```dart |
32 | | -const like = 'sample'; |
| 16 | +final bucket = TokenBucket( |
| 17 | + size: 15, |
| 18 | + refillInterval: const Duration(seconds: 1), |
| 19 | + refillAmount: 10, |
| 20 | + storage: MemoryTokenBucketStorage(), // optionally change the way the state of the bucket is stored |
| 21 | +); |
| 22 | +
|
| 23 | +if(bucket.consume()) { |
| 24 | + // Consumed 1 token successfully |
| 25 | +} |
| 26 | +
|
| 27 | +if(bucket.consume(2)) { |
| 28 | + // Consumed 2 tokens successfully |
| 29 | +} |
33 | 30 | ``` |
34 | 31 |
|
35 | | -## Additional information |
| 32 | +If you want to store the tokens asynchronously in a custom storage, you can also use the `AsyncTokenBucket`. |
36 | 33 |
|
37 | | -TODO: Tell users more about the package: where to find more information, how to |
38 | | -contribute to the package, how to file issues, what response they can expect |
39 | | -from the package authors, and more. |
| 34 | +```dart |
| 35 | +final bucket = AsyncTokenBucket( |
| 36 | + size: 15, |
| 37 | + refillInterval: const Duration(seconds: 1), |
| 38 | + refillAmount: 10, |
| 39 | + storage: MyCustomAsyncTokenBucketStorage(), |
| 40 | +); |
| 41 | +
|
| 42 | +if(await bucket.consume()) { |
| 43 | + // Consumed 1 token successfully |
| 44 | +} |
| 45 | +``` |
0 commit comments