PHPackages                             joshdifabio/resource-pool - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Queues &amp; Workers](/categories/queues)
4. /
5. joshdifabio/resource-pool

ActiveLibrary[Queues &amp; Workers](/categories/queues)

joshdifabio/resource-pool
=========================

Regulate the concurrency level of your async components

v0.2.0(9y ago)3321.1k1[3 issues](https://github.com/joshdifabio/resource-pool/issues)1MITPHPPHP &gt;=5.4

Since Aug 5Pushed 9y ago1 watchersCompare

[ Source](https://github.com/joshdifabio/resource-pool)[ Packagist](https://packagist.org/packages/joshdifabio/resource-pool)[ RSS](/packages/joshdifabio-resource-pool/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (2)Versions (3)Used By (1)

Resource Pool
=============

[](#resource-pool)

[![Build Status](https://camo.githubusercontent.com/108e5ae9f68cca120907fd5e8bb28aee7c67199a2870acbcffa3eeaff1105b97/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6a6f73686469666162696f2f7265736f757263652d706f6f6c2e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/joshdifabio/resource-pool)[![Coverage](https://camo.githubusercontent.com/4e3649b3af6a638320a7dd74f55c77fcf5b9c0b189235f042588d6ec81b0216b/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f6a6f73686469666162696f2f7265736f757263652d706f6f6c2e7376673f7374796c653d666c61742d737175617265)](http://codecov.io/github/joshdifabio/resource-pool)[![Code Quality](https://camo.githubusercontent.com/53b3fcdcb5ac95a00bfe0c8a764d3fd6695228f29673a240ea9f842dd0e924cc/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6a6f73686469666162696f2f7265736f757263652d706f6f6c2e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/joshdifabio/resource-pool/)

Don't pwn your resources, pool them!

Introduction
------------

[](#introduction)

Resource pools allow you to regulate the concurrency level of your asynchronous PHP components and spare your servers from excessive load. You'll find them particularly useful if your application sends HTTP requests or spawns child processes using something like [ReactPHP](https://github.com/reactphp/react).

Basic usage
-----------

[](#basic-usage)

If you aren't familiar with [Promises](https://github.com/reactphp/promise), this section isn't going to make a lot of sense.

Consider an application which sends HTTP requests to a remote endpoint asynchronously.

```
function sendRequest($httpRequest) : PromiseInterface {
    // this would probably be something like Guzzle or React/HttpClient
}
```

### How you shouldn't do it

[](#how-you-shouldnt-do-it)

```
foreach (getThousandsOfRequests() as $request) {
    sendRequest($request)->then(function ($response) {
        // the response came back!
    });
}

// thousands of requests have been initiated concurrently
```

An implementation like this could easily send 100s or even 1000s of requests within a single second, causing huge load on the remote server as it tries to serve your requests. This is essentially a DoS attack, and will make sysadmins cry, who will then make you cry.

### How you should do it

[](#how-you-should-do-it)

Create a resource pool representing a fixed number of resources, for example five.

```
$pool = new \ResourcePool\Pool(5);
```

Before sending a request, allocate a resource from the pool. `Pool::allocateOne()` returns an `AllocationPromise` which resolves as soon as a resource becomes available.

```
foreach (getThousandsOfRequests() as $request) {
    // to() will invoke a function and then release the allocated resources once it's done
    $pool->allocateOne()->to('sendRequest', $request)->then(function ($response) {
        // the response came back!
    });
}

// five requests are running; the rest are queued and will be sent as others complete
```

That's it! You did it! This implementation will spawn a maximum of five concurrent requests.

Advanced usage
--------------

[](#advanced-usage)

Advanced user? Read on.

### Allocate multiple resources

[](#allocate-multiple-resources)

```
$pool->allocate(5)->to(function () {
    // this task requires five resources to run!
});
```

### Allocate all the resources

[](#allocate-all-the-resources)

```
$pool->allocateAll()->to(function () {
    // this requires all the resources!
});
```

### Release allocations manually

[](#release-allocations-manually)

```
// call then() instead of to() to work with the allocation directly
$pool->allocate(2)->then(function ($allocation) {
    // two things which need to run at the same time
    firstThing()->done([$allocation, 'releaseOne']);
    secondThing()->done([$allocation, 'releaseOne']);
});
```

### Force an allocation to resolve immediately

[](#force-an-allocation-to-resolve-immediately)

```
try {
    $allocation = $pool->allocate(2)->now();
} catch (\RuntimeException $e) {
    // throws a \RuntimeException if the pool cannot allocate two resources
}
```

You can also choose to burst beyond the size of the pool for a specific allocation.

```
$pool = new \ResourcePool\Pool(1);
$allocation = $pool->allocate(2)->force();
$pool->getUsage(); // 2
$pool->getAvailability(); // 0
$allocation->releaseAll();
$pool->getAvailability(); // 1
```

### Find out when a pool is idle

[](#find-out-when-a-pool-is-idle)

```
$pool->whenNextIdle(function () {
    // the pool is idle!
});
```

### Change the size of a pool

[](#change-the-size-of-a-pool)

```
$pool->setSize(100);
```

### Find out how many resources are allocated

[](#find-out-how-many-resources-are-allocated)

```
$pool->getUsage();
```

### Find out how many resources are available

[](#find-out-how-many-resources-are-available)

```
$pool->getAvailability();
```

Installation
------------

[](#installation)

Install Resource Pool using [composer](https://getcomposer.org/).

```
composer require joshdifabio/resource-pool

```

License
-------

[](#license)

Resource Pool is released under the [MIT](https://github.com/joshdifabio/resource-pool/blob/master/LICENSE) license.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 93% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~513 days

Total

2

Last Release

3426d ago

PHP version history (2 changes)v0.1.0PHP &gt;=5.3

v0.2.0PHP &gt;=5.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/18f6db1b0f349a143a47f8a3c490b56ad644aa62669a0015bed16a98cdfa1985?d=identicon)[joshdifabio](/maintainers/joshdifabio)

---

Top Contributors

[![joshdifabio](https://avatars.githubusercontent.com/u/5609851?v=4)](https://github.com/joshdifabio "joshdifabio (40 commits)")[![WyriHaximus](https://avatars.githubusercontent.com/u/147145?v=4)](https://github.com/WyriHaximus "WyriHaximus (3 commits)")

---

Tags

asyncconcurrencypromisereactphplock

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/joshdifabio-resource-pool/health.svg)

```
[![Health](https://phpackages.com/badges/joshdifabio-resource-pool/health.svg)](https://phpackages.com/packages/joshdifabio-resource-pool)
```

###  Alternatives

[amphp/amp

A non-blocking concurrency framework for PHP applications.

4.4k123.4M323](/packages/amphp-amp)[react/promise-timer

A trivial implementation of timeouts for Promises, built on top of ReactPHP.

34141.9M96](/packages/react-promise-timer)[react/socket

Async, streaming plaintext TCP/IP and secure TLS socket server and client connections for ReactPHP

1.3k116.9M402](/packages/react-socket)[react/dns

Async DNS resolver for ReactPHP

536114.1M100](/packages/react-dns)[react/promise-stream

The missing link between Promise-land and Stream-land for ReactPHP

11512.9M45](/packages/react-promise-stream)[clue/mq-react

Mini Queue, the lightweight in-memory message queue to concurrently do many (but not too many) things at once, built on top of ReactPHP

144691.7k4](/packages/clue-mq-react)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
