You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+48-2
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,7 @@ _Rate limiting_ is a strategy for limiting an action. It puts a cap on how often
17
17
-[Strategies](#strategies)
18
18
- [Debounce](#debounce)
19
19
- [Throttle](#throttle)
20
+
- [BackOff](#backoff)
20
21
-[Pending](#pending)
21
22
-[Flush](#flush)
22
23
-[Cancellation](#cancellation)
@@ -74,8 +75,6 @@ TextField(
74
75
);
75
76
```
76
77
77
-
78
-
79
78
### Throttle
80
79
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.
81
80
@@ -120,6 +119,53 @@ RaisedButton(
120
119
);
121
120
```
122
121
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.
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.
0 commit comments