Async-ResourcePool

 view release on metacpan or  search on metacpan

lib/Async/ResourcePool.pm  view on Meta::CPAN

package Async::ResourcePool v0.1.3;

=head1 NAME

Async::ResourcePool - Resource pooling for asynchronous programs.

=head1 DESCRIPTION

This module implements the simple functionality of creating a source pool for
event-based/asynchronous programs.  It provides consumers with the ability to
have some code execute whenever a resource happens to be ready.  Further, it
allows resources to be categorized (by label) and limited as such.

=cut

use strict;
use warnings FATAL => "all";
use Carp qw( croak );

=head1 CONSTRUCTOR

=over 4

=item new [ ATTRIBUTES ]

=cut

sub new {
    my ($class, %params) = @_;

    my $self = bless {
        %params,

        _resources  => {},
        _allocated  => 0,
        _wait_queue => [],
        _available_queue  => [],
    }, $class;

    return $self;
}

=back

=head1 ATTRIBUTES

=over 4

=item factory -> CodeRef(POOL, CodeRef(RESOURCE, MESSAGE))

The factory for generating the resource.  The factory is a subroutine reference
which accepts an instance of this object and a callback as a reference.  The
callback, to be invoked when the resource has been allocated.

If no resource could be allocated due to error, then undef should be supplied
with the second argument being a string describing the failure.

=cut

sub factory {
    my ($self, $value) = @_;

    if (@_ == 2) {
        $self->{factory} = $value;
    }

    $self->{factory};
}

=item limit -> Int



( run in 0.668 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )