Giter Club home page Giter Club logo

promises-ratelimiter's Introduction

Build Status

NAME

Future::Limiter - impose rate and resource limits

SYNOPSIS

  my $l = Future::Limiter->from_yaml(<<'YAML');
      request:
          # Make no more than 1 request per second
          # have no more than 4 requests in flight at a time
          # If there is a backlog, process them as quickly as possible
          - burst : 3
          rate : 60/60
          - maximum: 4
      namelookup:
          - burst : 3
          rate : 60/60
  YAML

  ...

my ($host_token, $rate_token);
$limiter->limit( 'request', $hostname, $url )->then(sub {
    my ($host_token, $url ) = @_;
    request_url( $url )->on_ready( undef $host_token );
})->then(sub {
    ...
    undef $host_token;
    undef $rate_token;
});

This module provides an API to handle rate limits and resource limits in a unified API.

Usage with Future::AsyncAwait

The usage with Future::AsyncAwait is much more elegant, as you only need to keep the token around and other parameters live implicitly in your scope:

my( $host_token ) = await $concurrency->limit( $hostname );
my( $rate_token ) = await $rate->limit( $hostname );
request_url( $url )
...

METHODS

$limiter->limit( $eventname, $eventkey, @args )

my ($token,@args) = await $limiter->limit('fetch',$url->hostname);
... do work
undef $token; # release token

$limiter->limit('fetch',$url->hostname,$url)->then(sub( $token, $url) {
    return http_request($url)->on_ready(sub { undef $token });
};

The method to rate-limit an event from occurring, by key. The key can be undef to mean a global limit on the event.

The method returns a future that will return a token to release the current limit and the arguments passed in.

TODO

Persistence of the rate limiter, or periodical writeback of the current limits to a shared file / scoreboard to allow for cross-process limiting

SEE ALSO

Future::Mutex

REPOSITORY

The public repository of this module is http://github.com/Corion/Future-Limiter.

SUPPORT

The public support forum of this module is https://perlmonks.org/.

BUG TRACKER

Please report bugs in this module via the RT CPAN bug queue at https://rt.cpan.org/Public/Dist/Display.html?Name=Future-Limiter or via mail to [email protected].

AUTHOR

Max Maischein [email protected]

COPYRIGHT (c)

Copyright 2018-2019 by Max Maischein [email protected].

LICENSE

This module is released under the same terms as Perl itself.

promises-ratelimiter's People

Watchers

Max Maischein avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.