From 92e40d88b879e47f5520c5e904db6ebe1e88fb1f Mon Sep 17 00:00:00 2001 From: Tobe O Date: Fri, 16 Aug 2019 11:37:34 -0400 Subject: [PATCH] Redis example --- example/rate_limit_redis.dart | 37 +++++++++++++++++-------------- lib/src/service_rate_limiter.dart | 3 ++- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/example/rate_limit_redis.dart b/example/rate_limit_redis.dart index 97be75da..40d9c33b 100644 --- a/example/rate_limit_redis.dart +++ b/example/rate_limit_redis.dart @@ -1,24 +1,28 @@ +import 'package:angel_redis/angel_redis.dart'; import 'package:angel_framework/angel_framework.dart'; -import 'package:angel_framework/http.dart'; +import 'package:angel_production/angel_production.dart'; import 'package:angel_security/angel_security.dart'; -import 'package:logging/logging.dart'; -import 'package:pretty_logging/pretty_logging.dart'; +import 'package:resp_client/resp_client.dart'; +import 'package:resp_client/resp_commands.dart'; -main() async { - // Logging boilerplate. - Logger.root.onRecord.listen(prettyLog); +// We run this through angel_production, so that we can have +// multiple instances, all using the same Redis queue. +main(List args) => + Runner('rate_limit_redis', configureServer).run(args); - // Create an app, and HTTP driver. - var app = Angel(logger: Logger('rate_limit_redis')), http = AngelHttp(app); - - // Create a simple rate limiter that limits users to 5 +configureServer(Angel app) async { + // Create a simple rate limiter that limits users to 10 // queries per 30 seconds. // // In this case, we rate limit users by IP address. // // Our Redis store will be used to manage windows. - var rateLimiter = - InMemoryRateLimiter(5, Duration(seconds: 30), (req, res) => req.ip); + var connection = await connectSocket('localhost'); + var client = RespClient(connection); + var service = + RedisService(RespCommands(client), prefix: 'rate_limit_redis_example'); + var rateLimiter = ServiceRateLimiter( + 10, Duration(seconds: 30), service, (req, res) => req.ip); // `RateLimiter.handleRequest` is a middleware, and can be used anywhere // a middleware can be used. In this case, we apply the rate limiter to @@ -27,10 +31,9 @@ main() async { // Basic routes. app - ..get('/', (req, res) => 'Hello!') + ..get('/', (req, res) { + var instance = req.container.make(); + res.writeln('This is instance ${instance.id}.'); + }) ..fallback((req, res) => throw AngelHttpException.notFound()); - - // Start the server. - await http.startServer('127.0.0.1', 3000); - print('Rate limiting (redis) example listening at ${http.uri}'); } diff --git a/lib/src/service_rate_limiter.dart b/lib/src/service_rate_limiter.dart index 55b54d76..6b381db4 100644 --- a/lib/src/service_rate_limiter.dart +++ b/lib/src/service_rate_limiter.dart @@ -21,7 +21,8 @@ class ServiceRateLimiter extends RateLimiter { FutureOr> getCurrentWindow( RequestContext req, ResponseContext res, DateTime currentTime) async { var id = await getId(req, res); - var existing = await service.read(id); + var existing = await service.read(id).catchError((_) => null, + test: (e) => e is AngelHttpException && e.statusCode == 404); if (existing != null) { return RateLimitingWindow.fromJson(existing); }