Skip to content

Commit b4503b0

Browse files
committed
v1.0.0
Signed-off-by: xsahil03x <xdsahil@gmail.com>
1 parent 4e1e2b0 commit b4503b0

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## [1.0.0] - (14-12-2022)
2+
3+
* Added BackOff.
4+
* Added support for passing nullable values in Debounce and Throttle function.
5+
16
## [0.1.1] - (13-04-2021)
27

38
* Add example.

README.md

+48-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ _Rate limiting_ is a strategy for limiting an action. It puts a cap on how often
1717
- [Strategies](#strategies)
1818
- [Debounce](#debounce)
1919
- [Throttle](#throttle)
20+
- [BackOff](#backoff)
2021
- [Pending](#pending)
2122
- [Flush](#flush)
2223
- [Cancellation](#cancellation)
@@ -74,8 +75,6 @@ TextField(
7475
);
7576
```
7677

77-
78-
7978
### Throttle
8079
To _throttle_ a function means to ensure that the function is called at most once in a specified time period (for instance, once every 10 seconds). This means throttling will prevent a function from running if it has run “recently”. Throttling also ensures a function is run regularly at a fixed rate.
8180

@@ -120,6 +119,53 @@ RaisedButton(
120119
);
121120
```
122121

122+
### BackOff
123+
BackOff is a strategy that allows you to retry a function call multiple times with a delay between each call. It is useful when you want to retry a function call multiple times in case of failure.
124+
125+
#### Usage
126+
Creating backoff function is similar to debounce and throttle function.
127+
128+
1. Creating from scratch
129+
```dart
130+
final response = backOff(
131+
// Make a GET request
132+
() => http.get('https://google.com').timeout(Duration(seconds: 5)),
133+
maxAttempts: 5,
134+
maxDelay: Duration(seconds: 5),
135+
// Retry on SocketException or TimeoutException
136+
retryIf: (e, _) => e is SocketException || e is TimeoutException,
137+
);
138+
```
139+
2. Converting an existing function into backoff function
140+
```dart
141+
Future<String> regularFunction() async {
142+
// Make a GET request
143+
final response = await http.get('https://google.com').timeout(Duration(seconds: 5));
144+
return response.body;
145+
}
146+
147+
final response = regularFunction.backOff(
148+
maxAttempts: 5,
149+
maxDelay: Duration(seconds: 5),
150+
// Retry on SocketException or TimeoutException
151+
retryIf: (e, _) => e is SocketException || e is TimeoutException,
152+
);
153+
```
154+
155+
#### Example
156+
While making a network request, it is possible that the request fails due to network issues. In such cases, it is useful to retry the request multiple times with a delay between each call. This is where backoff strategy comes in handy.
157+
158+
```dart
159+
final response = backOff(
160+
// Make a GET request
161+
() => http.get('https://google.com').timeout(Duration(seconds: 5)),
162+
maxAttempts: 5,
163+
maxDelay: Duration(seconds: 5),
164+
// Retry on SocketException or TimeoutException
165+
retryIf: (e, _) => e is SocketException || e is TimeoutException,
166+
);
167+
```
168+
123169
### Pending
124170
Used to check if the there are functions still remaining to get invoked.
125171
```dart

example/pubspec.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ environment:
88
sdk: ">=2.12.0 <3.0.0"
99

1010
dependencies:
11-
rate_limiter: ^0.1.1
11+
rate_limiter:
12+
path: ../
1213

1314
dependency_overrides:
1415
rate_limiter:

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: rate_limiter
22
description: A pure dart package to apply useful rate limiting strategies on regular functions.
3-
version: 0.1.1
3+
version: 1.0.0
44
homepage: https://github.com/GetStream/rate_limiter
55
repository: https://github.com/GetStream/rate_limiter
66
issue_tracker: https://github.com/GetStream/rate_limiter/issues

0 commit comments

Comments
 (0)