Skip to content

Commit 965af92

Browse files
committed
adds tests, readme and ci
1 parent 21a4165 commit 965af92

13 files changed

Lines changed: 388 additions & 207 deletions

.github/FUNDING.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ko_fi: splashbyte
2+
custom: [ 'buymeacoffee.com/splashbyte' ]

.github/workflows/flutter.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Flutter Analyze & Test
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- uses: actions/checkout@v4
11+
12+
- uses: subosito/flutter-action@v2
13+
14+
- name: Install dependencies
15+
run: flutter pub get
16+
17+
- name: Verify formatting
18+
run: dart format --output=none --set-exit-if-changed .
19+
20+
- name: Analyze project source
21+
run: flutter analyze
22+
23+
- name: Run tests
24+
run: flutter test --coverage
25+
26+
- name: Upload coverage reports to Codecov
27+
uses: codecov/codecov-action@v3
28+
with:
29+
fail_ci_if_error: true
30+
token: ${{ secrets.CODECOV_TOKEN }}

README.md

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,45 @@
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
42

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+
[![pub.dev](https://img.shields.io/pub/v/token_bucket_algorithm.svg?style=flat?logo=dart)](https://pub.dev/packages/token_bucket_algorithm)
4+
[![github](https://img.shields.io/static/v1?label=platform&message=flutter&color=1ebbfd)](https://github.com/splashbyte/dart_token_bucket_algorithm)
5+
[![likes](https://img.shields.io/pub/likes/token_bucket_algorithm)](https://pub.dev/packages/token_bucket_algorithm/score)
6+
[![popularity](https://img.shields.io/pub/popularity/token_bucket_algorithm)](https://pub.dev/packages/token_bucket_algorithm/score)
7+
[![pub points](https://img.shields.io/pub/points/token_bucket_algorithm)](https://pub.dev/packages/token_bucket_algorithm/score)
8+
[![license](https://img.shields.io/github/license/splashbyte/dart_token_bucket_algorithm.svg)](https://github.com/SplashByte/dart_token_bucket_algorithm/blob/main/LICENSE)
9+
[![codecov](https://codecov.io/gh/splashbyte/dart_token_bucket_algorithm/branch/main/graph/badge.svg?token=NY1D6W88H2)](https://codecov.io/gh/splashbyte/dart_token_bucket_algorithm)
710

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.
1312

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
3014

3115
```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+
}
3330
```
3431

35-
## Additional information
32+
If you want to store the tokens asynchronously in a custom storage, you can also use the `AsyncTokenBucket`.
3633

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+
```

analysis_options.yaml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
include: package:flutter_lints/flutter.yaml
22

3-
# Additional information about this file can be found at
4-
# https://dart.dev/guides/language/analysis-options
3+
analyzer:
4+
language:
5+
strict-casts: true
6+
strict-raw-types: true
7+
8+
linter:
9+
rules:
10+
prefer_single_quotes: true
11+
parameter_assignments: true

coverage/lcov.info

Lines changed: 91 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,102 @@
1-
SF:lib/src/token_bucket_state.dart
2-
DA:5,1
3-
DA:7,1
4-
DA:8,1
5-
DA:9,1
6-
DA:10,1
7-
DA:14,1
8-
DA:15,5
9-
DA:17,1
10-
DA:18,1
11-
DA:19,1
12-
DA:21,2
13-
DA:24,2
14-
DA:25,1
15-
DA:26,2
16-
DA:29,1
17-
DA:32,1
18-
DA:33,3
19-
DA:34,3
20-
DA:35,3
21-
DA:37,1
22-
DA:38,5
23-
LF:21
24-
LH:21
25-
end_of_record
261
SF:lib/src/token_bucket.dart
27-
DA:14,1
28-
DA:19,2
29-
DA:20,2
30-
DA:21,2
31-
DA:27,1
32-
DA:28,2
2+
DA:25,1
333
DA:31,2
34-
DA:32,1
35-
DA:35,4
36-
DA:36,2
37-
DA:38,6
38-
DA:40,1
39-
DA:42,4
40-
DA:50,1
4+
DA:32,2
5+
DA:33,2
6+
DA:34,3
7+
DA:35,1
8+
DA:46,1
9+
DA:47,2
10+
DA:50,2
11+
DA:51,1
12+
DA:54,4
4113
DA:55,2
42-
DA:57,1
43-
DA:59,2
44-
DA:60,5
45-
DA:63,6
46-
DA:66,1
47-
DA:68,3
48-
DA:69,3
49-
DA:71,2
50-
DA:72,3
51-
DA:73,1
52-
DA:74,2
53-
DA:79,1
54-
DA:80,4
55-
DA:81,3
56-
DA:82,2
57-
DA:89,1
14+
DA:57,6
15+
DA:59,1
16+
DA:61,4
17+
DA:70,1
18+
DA:76,2
19+
DA:78,1
20+
DA:80,3
21+
DA:83,1
22+
DA:84,2
23+
DA:86,2
24+
DA:87,4
25+
DA:92,1
5826
DA:94,2
59-
DA:96,1
60-
DA:98,5
61-
DA:99,3
62-
DA:102,1
27+
DA:95,4
28+
DA:98,4
29+
DA:101,1
30+
DA:103,3
6331
DA:104,3
64-
DA:105,3
65-
DA:107,3
32+
DA:106,2
33+
DA:107,2
6634
DA:108,1
6735
DA:109,2
68-
LF:41
69-
LH:41
36+
DA:114,1
37+
DA:115,4
38+
DA:116,3
39+
DA:117,2
40+
DA:128,1
41+
DA:134,2
42+
DA:136,1
43+
DA:138,1
44+
DA:141,1
45+
DA:142,2
46+
DA:144,2
47+
DA:145,4
48+
DA:150,1
49+
DA:152,2
50+
DA:153,2
51+
DA:154,1
52+
DA:157,1
53+
DA:159,3
54+
DA:160,3
55+
DA:162,2
56+
DA:163,1
57+
DA:164,2
58+
LF:56
59+
LH:56
7060
end_of_record
71-
SF:lib/src/token_bucket_storage.dart
72-
DA:7,1
73-
DA:17,1
74-
DA:22,1
61+
SF:lib/src/token_bucket_state.dart
62+
DA:10,3
63+
DA:13,2
64+
DA:14,2
65+
DA:15,2
66+
DA:16,2
67+
DA:20,1
68+
DA:21,5
7569
DA:23,1
70+
DA:24,1
7671
DA:25,1
77-
DA:26,1
78-
DA:30,1
79-
LF:7
80-
LH:7
72+
DA:27,2
73+
DA:30,2
74+
DA:31,1
75+
DA:32,2
76+
DA:35,3
77+
DA:38,2
78+
DA:39,6
79+
DA:40,6
80+
DA:41,6
81+
DA:43,1
82+
DA:44,5
83+
LF:21
84+
LH:21
85+
end_of_record
86+
SF:lib/src/token_bucket_storage.dart
87+
DA:7,2
88+
DA:16,2
89+
DA:27,2
90+
DA:31,2
91+
DA:32,2
92+
DA:34,2
93+
DA:35,2
94+
DA:40,3
95+
DA:44,1
96+
DA:46,1
97+
DA:47,3
98+
DA:49,1
99+
DA:50,3
100+
LF:13
101+
LH:13
81102
end_of_record

example/main.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import 'package:token_bucket_algorithm/token_bucket_algorithm.dart';
2+
3+
void main() {
4+
final bucket = TokenBucket(
5+
size: 15,
6+
refillInterval: const Duration(seconds: 1),
7+
refillAmount: 10,
8+
storage: MemoryTokenBucketStorage(),
9+
);
10+
11+
if(bucket.consume()) {
12+
// Consumed 1 token successfully
13+
}
14+
15+
if(bucket.consume(2)) {
16+
// Consumed 2 tokens successfully
17+
}
18+
}

0 commit comments

Comments
 (0)