jaguar_throttle 2.4.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 30

jaguar_throttle #

Rate limiter for Jaguar

Usage #

class ExampleApi {
  Throttler throttle(_) =>
      new Throttler(new Rate(new Duration(seconds: 10), 100));

  @Get(path: '/hello')
  String sayHello(Context ctx) => 'hello';

Docs #

Quota #

Rate class provides an easy to define count per time interval. It is used to specify throttling quote of the Throttler

The constructor of Rate takes interval in Duration and count.

Rate(Duration interval, int count);

To create a rate of 100 requests per 10 seconds:

new Rate(new Duration(seconds: 10), 100);

Id maker #

ThrottleIdMaker creates the criteria by which requests are throttled. ThrottleIdMaker is a function that takes Context object and returns a String identifying the request.

typedef FutureOr<String> ThrottleIdMaker(Context ctx);

By default, Throttler uses throttleIdByIp as ThrottleIdMaker. throttleIdByIp throttles requests by remote IP address.

Store #

Throttler uses Cache to store request count stats. By default, defaultThrottleCache, which is an InMemoryCache, is used as store.

Headers #

Throttler sets X-RateLimit-Limit, X-RateLimit-Remaining and X-RateLimit-Reset rate-limit headers with quota, quota remaining and duration in seconds after which quota resets respectively. In case, the quota for the current interval is completely used, Throttler also sets the header Retry-After with duration in seconds after which the client can retry.

Changelog #

0.2.1 #

  • throttle function based throttler

0.2.0 #

  • Renamed Throttle to Throttler
  • Introduced Rate structure to hold quota

0.1.0 #

  • Expose ThrottleState through Interceptor output
  • Use Cache instead of ThrottleStore

0.0.1 #

  • Initial version, created by Stagehand


// Copyright (c) 2017, Ravi Teja Gudapati. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:jaguar/jaguar.dart';
import 'package:jaguar_throttle/jaguar_throttle.dart';
import 'package:jaguar_client/jaguar_client.dart';

final client = JsonClient(new http.IOClient());

runServer() async {
  final jaguar = Jaguar(port: 10000)
    ..get('/one', (_) => 'one', before: [Throttler.perMin(10)])
    ..get('/two', (_) => 'two', before: [Throttler.perMin(10)]);
  await jaguar.serve();

runClient() async {
  for (int i = 0; i < 10; i++) {
    await client
        .expect([statusCodeIs(200), bodyIs('one')]);
  await client
      .expect([statusCodeIs(429), bodyIs('Limit exceeded')]);
  await client
      .expect([statusCodeIs(429), bodyIs('Limit exceeded')]);

main() async {
  await runServer();
  await runClient();


Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  jaguar_throttle: ^2.4.1

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:jaguar_throttle/jaguar_throttle.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jan 17, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.2

Health suggestions

Fix lib/src/interceptor.dart. (-7.24 points)

Analysis of lib/src/interceptor.dart reported 15 hints, including:

line 25 col 7: Unnecessary new keyword.

line 32 col 7: Unnecessary new keyword.

line 39 col 7: Unnecessary new keyword.

line 46 col 7: Unnecessary new keyword.

line 51 col 16: Annotate overridden members.

Fix lib/src/jaguar_throttle_base.dart. (-6.31 points)

Analysis of lib/src/jaguar_throttle_base.dart reported 13 hints, including:

line 25 col 26: Prefer generic function type aliases.

line 34 col 3: Omit type annotations for local variables.

line 36 col 17: Unnecessary new keyword.

line 45 col 36: Unnecessary new keyword.

line 45 col 54: Unnecessary new keyword.

Fix lib/src/state.dart. (-3.45 points)

Analysis of lib/src/state.dart reported 7 hints, including:

line 21 col 14: Unnecessary new keyword.

line 24 col 5: Omit type annotations for local variables.

line 25 col 5: Omit type annotations for local variables.

line 25 col 50: Unnecessary new keyword.

line 26 col 5: Omit type annotations for local variables.

Fix lib/src/rate.dart. (-1.99 points)

Analysis of lib/src/rate.dart reported 4 hints:

line 27 col 27: Unnecessary new keyword.

line 30 col 27: Unnecessary new keyword.

line 33 col 28: Unnecessary new keyword.

line 36 col 27: Unnecessary new keyword.

Fix lib/src/bookkeep.dart. (-1.49 points)

Analysis of lib/src/bookkeep.dart reported 3 hints:

line 14 col 24: Unnecessary new keyword.

line 19 col 22: Unnecessary new keyword.

line 23 col 7: Unnecessary new keyword.

Maintenance issues and suggestions

Homepage URL doesn't exist. (-20 points)

At the time of the analysis the homepage field was unreachable.

Package is getting outdated. (-32.88 points)

The package was last published 69 weeks ago.

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
jaguar ^2.4.4 2.4.36
jaguar_cache ^2.1.2 2.1.2
Transitive dependencies
auth_header 2.1.4
charcode 1.1.2
collection 1.14.12
convert 2.1.1
crypto 2.1.4
http_server 0.9.8+3
jaguar_common 2.1.4
jaguar_serializer 2.2.12
logging 0.11.4
meta 1.1.8
mime 0.9.6+3
path 1.6.4
path_tree 2.2.2
stack_trace 1.9.3
typed_data 1.1.6
Dev dependencies
jaguar_client ^2.4.1
test ^1.3.0