-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f93d65d
commit 14b199a
Showing
7 changed files
with
87 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import 'dart:io'; | ||
|
||
import 'base_http_exception.dart'; | ||
|
||
class InternalServerError extends BaseHttpException { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import '../http/response/response.dart'; | ||
import 'base_http_exception.dart'; | ||
|
||
class ThrottleException extends BaseHttpException { | ||
ThrottleException( | ||
{required super.message, | ||
required super.code, | ||
super.responseType = ResponseType.json, | ||
super.errorCode = 'Rate limiting'}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:vania/vania.dart'; | ||
|
||
import '../../exception/throttle_exception.dart'; | ||
import '../throttle_requests.dart'; | ||
|
||
class Throttle extends Middleware { | ||
final int maxAttempts; | ||
final Duration duration; | ||
|
||
Throttle({ | ||
this.maxAttempts = 6, | ||
this.duration = const Duration(seconds: 60), | ||
}) { | ||
throttle = ThrottleRequests(maxAttempts: maxAttempts, duration: duration); | ||
} | ||
|
||
late ThrottleRequests throttle; | ||
|
||
@override | ||
Future handle(Request req) async { | ||
final clientIp = req.ip; | ||
if (clientIp == null) { | ||
req.response.statusCode = HttpStatus.internalServerError; | ||
req.response.write('Error determining client IP'); | ||
await req.response.close(); | ||
return; | ||
} | ||
|
||
if (!throttle.request(clientIp)) { | ||
throw ThrottleException( | ||
message: 'Too Many Requests.', | ||
code: HttpStatus.tooManyRequests, | ||
); | ||
} | ||
next?.handle(req); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class ThrottleRequests { | ||
final int maxAttempts; | ||
final Duration duration; | ||
final Map<String, _RateLimit> _limits = {}; | ||
|
||
ThrottleRequests({required this.maxAttempts, required this.duration}); | ||
|
||
bool request(String ip) { | ||
final currentTime = DateTime.now(); | ||
|
||
_limits.putIfAbsent(ip, () => _RateLimit(0, currentTime)); | ||
final limit = _limits[ip]!; | ||
|
||
if (currentTime.difference(limit.windowStart).compareTo(duration) >= 0) { | ||
limit.count = 0; | ||
limit.windowStart = currentTime; | ||
} | ||
|
||
if (limit.count < maxAttempts) { | ||
limit.count++; | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
class _RateLimit { | ||
int count; | ||
DateTime windowStart; | ||
|
||
_RateLimit(this.count, this.windowStart); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters