The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

MojoX::IOLoop::Throttle - throttle Mojo events

VERSION

Version 0.01_25. (DEV)

SYNOPSIS

  #!/usr/bin/env perl
  use Mojo::Base -strict;
  use MojoX::IOLoop::Throttle;
  $| = 1;

  # New throttle object
  my $throttle = MojoX::IOLoop::Throttle->new(

    # Allow not more than [limit_run] running (parallel,incomplete) jobs
    limit_run => 3,

    # do not start more than [limit_period] jobs per [period] seconds
    period       => 2,
    limit_period => 4,

    # Simulate a little latency
    delay => 0.05
  );

  my $count;

  # Subscribe to finish event
  $throttle->on(finish =>
      sub { say "I've processed $count jobs! Bye-bye"; Mojo::IOLoop->stop; });

  # Throttle 20 jobs!
  $throttle->limit(20);


  # CallBack to throttle
  $throttle->run(
    cb => sub {
      my ($thr, %args) = @_;

      # get an option passed to us
      my $test = delete $args{test};

      # We a beginning one job
      $thr->begin;

      my $rand_time = rand() / 5;
      say "Job $rand_time started: $test";

      $thr->iowatcher->timer(
        $rand_time => sub {
          say "job $rand_time ended";
          $count++;

          # Say that we end (to decrease limit_run count and let other job to start)
          $thr->end();
        }
      );
    },

    # Also we can pass arguments to code
    test => 'hello'
  );

  # Let's start
  Mojo::IOLoop->start();

DESCRIPTION

  AHTUNG!!!

  This is a very first development release. Be patient. Documentation is in progress.
  You can find some working real-life examples in 'example' dir.
  
  If your are going to use this module now, use subclassing, because all method and options are experimental

OBJECT METHODS

begin

Say that the job was started

end

Say that the job was ended

drop

Drop timers, counters and events

add_limit

  $thr->add_limit($n);
  

Increase a limit attr. If agrument is omitter, increase on 1

run

  $thr->run(cb => sub {...}, @other_params);
  
  Starts doing job

ATTRIBUTES

limit

total limit of shots

limit_run

max. number of jobs running in parallell

limit_period

limit number of shots per some period

period

time for limit_period

delay

simulate a lattency (timer resolution)

AUTHOR

Alex, <alexbyk at cpan.org>

BUGS

Please report any bugs or feature requests to bug-mojox-ioloop-throttle at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MojoX-IOLoop-Throttle. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc MojoX::IOLoop::Throttle

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

Copyright 2012 Alex.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.