Skip to content

Commit

Permalink
adds tests, readme and ci
Browse files Browse the repository at this point in the history
  • Loading branch information
maeddin committed Jul 28, 2024
1 parent 21a4165 commit 965af92
Show file tree
Hide file tree
Showing 13 changed files with 388 additions and 207 deletions.
2 changes: 2 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ko_fi: splashbyte
custom: [ 'buymeacoffee.com/splashbyte' ]
30 changes: 30 additions & 0 deletions .github/workflows/flutter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Flutter Analyze & Test

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- uses: subosito/flutter-action@v2

- name: Install dependencies
run: flutter pub get

- name: Verify formatting
run: dart format --output=none --set-exit-if-changed .

- name: Analyze project source
run: flutter analyze

- name: Run tests
run: flutter test --coverage

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
with:
fail_ci_if_error: true
token: ${{ secrets.CODECOV_TOKEN }}
68 changes: 37 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
<!--
This README describes the package. If you publish this package to pub.dev,
this README's contents appear on the landing page for your package.
# token_bucket_algorithm

For information about how to write a good package README, see the guide for
[writing package pages](https://dart.dev/guides/libraries/writing-package-pages).
[![pub.dev](https://img.shields.io/pub/v/token_bucket_algorithm.svg?style=flat?logo=dart)](https://pub.dev/packages/token_bucket_algorithm)
[![github](https://img.shields.io/static/v1?label=platform&message=flutter&color=1ebbfd)](https://github.com/splashbyte/dart_token_bucket_algorithm)
[![likes](https://img.shields.io/pub/likes/token_bucket_algorithm)](https://pub.dev/packages/token_bucket_algorithm/score)
[![popularity](https://img.shields.io/pub/popularity/token_bucket_algorithm)](https://pub.dev/packages/token_bucket_algorithm/score)
[![pub points](https://img.shields.io/pub/points/token_bucket_algorithm)](https://pub.dev/packages/token_bucket_algorithm/score)
[![license](https://img.shields.io/github/license/splashbyte/dart_token_bucket_algorithm.svg)](https://github.com/SplashByte/dart_token_bucket_algorithm/blob/main/LICENSE)
[![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)

For general information about developing packages, see the Dart guide for
[creating packages](https://dart.dev/guides/libraries/create-library-packages)
and the Flutter guide for
[developing packages and plugins](https://flutter.dev/developing-packages).
-->
This Dart package provides rate limiting by using an implementation of the token bucket algorithm.

TODO: Put a short description of the package here that helps potential users
know whether this package might be useful for them.

## Features

TODO: List what your package can do. Maybe include images, gifs, or videos.

## Getting started

TODO: List prerequisites and provide or point to information on how to
start using the package.

## Usage

TODO: Include short and useful examples for package users. Add longer examples
to `/example` folder.
## Simple usage

```dart
const like = 'sample';
final bucket = TokenBucket(
size: 15,
refillInterval: const Duration(seconds: 1),
refillAmount: 10,
storage: MemoryTokenBucketStorage(), // optionally change the way the state of the bucket is stored
);
if(bucket.consume()) {
// Consumed 1 token successfully
}
if(bucket.consume(2)) {
// Consumed 2 tokens successfully
}
```

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

TODO: Tell users more about the package: where to find more information, how to
contribute to the package, how to file issues, what response they can expect
from the package authors, and more.
```dart
final bucket = AsyncTokenBucket(
size: 15,
refillInterval: const Duration(seconds: 1),
refillAmount: 10,
storage: MyCustomAsyncTokenBucketStorage(),
);
if(await bucket.consume()) {
// Consumed 1 token successfully
}
```
11 changes: 9 additions & 2 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
include: package:flutter_lints/flutter.yaml

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
analyzer:
language:
strict-casts: true
strict-raw-types: true

linter:
rules:
prefer_single_quotes: true
parameter_assignments: true
161 changes: 91 additions & 70 deletions coverage/lcov.info
Original file line number Diff line number Diff line change
@@ -1,81 +1,102 @@
SF:lib/src/token_bucket_state.dart
DA:5,1
DA:7,1
DA:8,1
DA:9,1
DA:10,1
DA:14,1
DA:15,5
DA:17,1
DA:18,1
DA:19,1
DA:21,2
DA:24,2
DA:25,1
DA:26,2
DA:29,1
DA:32,1
DA:33,3
DA:34,3
DA:35,3
DA:37,1
DA:38,5
LF:21
LH:21
end_of_record
SF:lib/src/token_bucket.dart
DA:14,1
DA:19,2
DA:20,2
DA:21,2
DA:27,1
DA:28,2
DA:25,1
DA:31,2
DA:32,1
DA:35,4
DA:36,2
DA:38,6
DA:40,1
DA:42,4
DA:50,1
DA:32,2
DA:33,2
DA:34,3
DA:35,1
DA:46,1
DA:47,2
DA:50,2
DA:51,1
DA:54,4
DA:55,2
DA:57,1
DA:59,2
DA:60,5
DA:63,6
DA:66,1
DA:68,3
DA:69,3
DA:71,2
DA:72,3
DA:73,1
DA:74,2
DA:79,1
DA:80,4
DA:81,3
DA:82,2
DA:89,1
DA:57,6
DA:59,1
DA:61,4
DA:70,1
DA:76,2
DA:78,1
DA:80,3
DA:83,1
DA:84,2
DA:86,2
DA:87,4
DA:92,1
DA:94,2
DA:96,1
DA:98,5
DA:99,3
DA:102,1
DA:95,4
DA:98,4
DA:101,1
DA:103,3
DA:104,3
DA:105,3
DA:107,3
DA:106,2
DA:107,2
DA:108,1
DA:109,2
LF:41
LH:41
DA:114,1
DA:115,4
DA:116,3
DA:117,2
DA:128,1
DA:134,2
DA:136,1
DA:138,1
DA:141,1
DA:142,2
DA:144,2
DA:145,4
DA:150,1
DA:152,2
DA:153,2
DA:154,1
DA:157,1
DA:159,3
DA:160,3
DA:162,2
DA:163,1
DA:164,2
LF:56
LH:56
end_of_record
SF:lib/src/token_bucket_storage.dart
DA:7,1
DA:17,1
DA:22,1
SF:lib/src/token_bucket_state.dart
DA:10,3
DA:13,2
DA:14,2
DA:15,2
DA:16,2
DA:20,1
DA:21,5
DA:23,1
DA:24,1
DA:25,1
DA:26,1
DA:30,1
LF:7
LH:7
DA:27,2
DA:30,2
DA:31,1
DA:32,2
DA:35,3
DA:38,2
DA:39,6
DA:40,6
DA:41,6
DA:43,1
DA:44,5
LF:21
LH:21
end_of_record
SF:lib/src/token_bucket_storage.dart
DA:7,2
DA:16,2
DA:27,2
DA:31,2
DA:32,2
DA:34,2
DA:35,2
DA:40,3
DA:44,1
DA:46,1
DA:47,3
DA:49,1
DA:50,3
LF:13
LH:13
end_of_record
18 changes: 18 additions & 0 deletions example/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:token_bucket_algorithm/token_bucket_algorithm.dart';

void main() {
final bucket = TokenBucket(
size: 15,
refillInterval: const Duration(seconds: 1),
refillAmount: 10,
storage: MemoryTokenBucketStorage(),
);

if(bucket.consume()) {
// Consumed 1 token successfully
}

if(bucket.consume(2)) {
// Consumed 2 tokens successfully
}
}
Loading

0 comments on commit 965af92

Please sign in to comment.