From 2b190f0f0c8ac90cc2261d1402a3fdf1ec226e9d Mon Sep 17 00:00:00 2001 From: Sitaram Kalluri Date: Mon, 11 Sep 2023 07:31:42 +0530 Subject: [PATCH] feat: Implement AtRateLimiter spec changes --- packages/at_server_spec/CHANGELOG.md | 3 +++ .../src/at_rate_limiter/at_rate_limiter.dart | 18 ++++++++++++++++++ .../lib/src/connection/inbound_connection.dart | 3 ++- packages/at_server_spec/pubspec.yaml | 4 ++-- 4 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 packages/at_server_spec/lib/src/at_rate_limiter/at_rate_limiter.dart diff --git a/packages/at_server_spec/CHANGELOG.md b/packages/at_server_spec/CHANGELOG.md index d6a3f02ae..cd7e06452 100644 --- a/packages/at_server_spec/CHANGELOG.md +++ b/packages/at_server_spec/CHANGELOG.md @@ -1,3 +1,6 @@ +## 3.0.15 +- feat: Introduce AtRateLimiter to limit the requests based on the criteria defined +- fix: Modify InboundConnection to implement AtRateLimiter to limit requests ## 3.0.14 - fix: Rename TOTP to OTP ## 3.0.13 diff --git a/packages/at_server_spec/lib/src/at_rate_limiter/at_rate_limiter.dart b/packages/at_server_spec/lib/src/at_rate_limiter/at_rate_limiter.dart new file mode 100644 index 000000000..bd2a3d4f3 --- /dev/null +++ b/packages/at_server_spec/lib/src/at_rate_limiter/at_rate_limiter.dart @@ -0,0 +1,18 @@ +/// A rate limiter class that allows controlling the rate of requests within a specified time frame. +/// +/// This class provides a way to limit the number of requests that can be made +/// within a specified time frame. It keeps track of the timestamps of previous +/// requests and allows requests to be made only if they do not exceed the +/// maximum allowed requests per time frame. +abstract class AtRateLimiter { + /// The maximum number of requests allowed within the specified time frame. + late int maxRequestsPerTimeFrame; + + /// The duration of the time frame within which requests are limited. + late int timeFrameInMillis; + + /// Checks whether a new request is allowed based on the rate limiting rules. + /// + /// Returns `true` if the request is allowed, or `false` if it exceeds the rate limit. + bool isRequestAllowed(); +} diff --git a/packages/at_server_spec/lib/src/connection/inbound_connection.dart b/packages/at_server_spec/lib/src/connection/inbound_connection.dart index c4851ab47..bab07d14b 100644 --- a/packages/at_server_spec/lib/src/connection/inbound_connection.dart +++ b/packages/at_server_spec/lib/src/connection/inbound_connection.dart @@ -1,7 +1,8 @@ import 'dart:io'; +import 'package:at_server_spec/src/at_rate_limiter/at_rate_limiter.dart'; import 'package:at_server_spec/src/connection/at_connection.dart'; -abstract class InboundConnection extends AtConnection { +abstract class InboundConnection extends AtConnection implements AtRateLimiter { ///Returns true if remote socket and remote port of this and connection matches bool equals(InboundConnection connection); diff --git a/packages/at_server_spec/pubspec.yaml b/packages/at_server_spec/pubspec.yaml index 9a327f72d..0812c17ab 100644 --- a/packages/at_server_spec/pubspec.yaml +++ b/packages/at_server_spec/pubspec.yaml @@ -1,6 +1,6 @@ name: at_server_spec description: A Dart library containing abstract classes that defines what implementations of the root and secondary servers are responsible for. -version: 3.0.14 +version: 3.0.15 repository: https://github.com/atsign-foundation/at_server homepage: https://docs.atsign.com documentation: https://docs.atsign.com/atplatform/secondaryserver @@ -10,7 +10,7 @@ environment: dependencies: meta: ^1.8.0 - at_commons: ^3.0.54 + at_commons: ^3.0.55 dev_dependencies: lints: ^1.0.1