IPC-ConcurrencyLimit

 view release on metacpan or  search on metacpan

lib/IPC/ConcurrencyLimit.pm  view on Meta::CPAN


No complicated master/slave setups and process/thread pooling.

=back

The implementation uses some form of locking to limit concurrency:
There's simply a limited number of locks to go around. The detailed
locking implementation is chosen using the C<type> parameter to the
constructor. The base distributions ships with one locking strategy only:
L<IPC::ConcurrencyLimit::Lock::Flock> for a file-locking based
concurrency limit.

Among the other potential strategies that are not part of
this distribution are NFS-based locking using L<File::SharedNFSLock>
or using MySQL's C<GET_LOCK>. Both of these schemes would
allow limiting concurrency across multiple hosts without a
special-purpose daemon.

=head1 METHODS

=head2 new

Creates a new concurrency limit. Creating the object
does B<not> lock anything. This requires a followup
call to the C<get_lock()> method!

After calling C<get_lock()>, the lock will be held
by the C<IPC::ConcurrencyLimit> object and released
when either C<release_lock()> is called or the
C<IPC::ConcurrencyLimit> object is freed.

Takes named parameters, one of which is the C<type>
parameter, which specifies the type of lock to use
and defaults to C<Flock>.

The C<max_procs> option indicates the maximum
number of locks that can be held at the same time
and thus usually the maximum no. of running processes.
It defaults to 1.

All concurrency limits that refer to the same resource/limit
B<must> use the same setting for C<max_procs>. If not, the
behaviour is undefined.

All other named parameters will be passed as options
to the lock implementation. See L<IPC::ConcurrencyLimit::Lock::Flock>
or other implementations.

=head2 get_lock

Creates the actual lock and if successful, returns its id
(starting from 1, not 0).

Returns undef if locking was unsuccessful.
Multiple calls do not stack. They will simply return
the same lock id as long as a lock is held.

B<WARNING!> make sure the variable holding the lock remains in scope at all
times, otherwise the lock will be released and locking will become apparently
ineffective. This is the most common reason for having several concurrent
processes running when only one is expected to be alive.

=head2 release_lock

Releases the lock. Returns 1 if a lock has been
released or undef if there had been no lock to be
released.

=head2 is_locked

Returns whether we have a lock.

=head2 lock_id

Returns the id of the lock or undef if there is none.

=head2 heartbeat

Check whether the lock is still valid. If so,
returns true. Otherwise, it releases (destroys) the lock
and returns false.

=head1 AUTHOR

Steffen Mueller, C<smueller@cpan.org>

Yves Orton

David Morel

Matt Koscica, C<mattk@cpan.org>

Ivan Kruglov

=head1 ACKNOWLEDGMENT

This module was originally developed for Booking.com.
With approval from Booking.com, this module was generalized
and put on CPAN, for which the authors would like to express
their gratitude.

=head1 COPYRIGHT AND LICENSE

 (C) 2011-2015 Steffen Mueller. All rights reserved.
 
 This code is available under the same license as Perl version
 5.8.1 or higher.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

=cut



( run in 0.975 second using v1.01-cache-2.11-cpan-437f7b0c052 )