Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ COPY composer.json /usr/local/src/
RUN composer install --ignore-platform-reqs --optimize-autoloader \
--no-plugins --no-scripts --prefer-dist

FROM php:8.3-cli-alpine AS compile
FROM php:8.4-cli-alpine AS compile

ENV PHP_REDIS_VERSION=6.1.0 \
PHP_SWOOLE_VERSION=v5.1.3 \
PHP_MONGO_VERSION=1.20.0
PHP_SWOOLE_VERSION=v6.0.2 \
PHP_MONGO_VERSION=2.1.1

RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

Expand Down Expand Up @@ -67,9 +67,9 @@ RUN echo "opcache.enable_cli=1" >> $PHP_INI_DIR/php.ini
RUN echo "memory_limit=1024M" >> $PHP_INI_DIR/php.ini

COPY --from=composer /usr/local/src/vendor /usr/src/code/vendor
COPY --from=swoole /usr/local/lib/php/extensions/no-debug-non-zts-20230831/swoole.so /usr/local/lib/php/extensions/no-debug-non-zts-20230831/
COPY --from=redis /usr/local/lib/php/extensions/no-debug-non-zts-20230831/redis.so /usr/local/lib/php/extensions/no-debug-non-zts-20230831/
COPY --from=mongodb /usr/local/lib/php/extensions/no-debug-non-zts-20230831/mongodb.so /usr/local/lib/php/extensions/no-debug-non-zts-20230831/
COPY --from=swoole /usr/local/lib/php/extensions/no-debug-non-zts-20240924/swoole.so /usr/local/lib/php/extensions/no-debug-non-zts-20240924/
COPY --from=redis /usr/local/lib/php/extensions/no-debug-non-zts-20240924/redis.so /usr/local/lib/php/extensions/no-debug-non-zts-20240924/
COPY --from=mongodb /usr/local/lib/php/extensions/no-debug-non-zts-20240924/mongodb.so /usr/local/lib/php/extensions/no-debug-non-zts-20240924/

# Add Source Code
COPY . /usr/src/code
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"test": "./vendor/bin/phpunit --configuration phpunit.xml --debug"
},
"require": {
"php": ">=8.3",
"php": ">=8.4",
"utopia-php/telemetry": "0.1.*"
},
"require-dev": {
Expand All @@ -39,7 +39,7 @@
},
"config": {
"platform": {
"php": "8.3"
"php": "8.4"
},
"allow-plugins": {
"php-http/discovery": false,
Expand Down
20 changes: 4 additions & 16 deletions src/Pools/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@
*/
class Pool
{
/**
* @var string
*/
protected string $name;

/**
* @var int
*/
protected int $size = 0;

/**
* @var callable
*/
Expand Down Expand Up @@ -72,12 +62,10 @@ class Pool
* @param int $size
* @param callable(): TResource $init
*/
public function __construct(string $name, int $size, callable $init)
public function __construct(protected string $name, protected int $size, callable $init)
{
$this->name = $name;
$this->size = $size;
$this->init = $init;
$this->pool = array_fill(0, $size, true);
$this->pool = array_fill(0, $this->size, true);
$this->setTelemetry(new NoTelemetry());
}

Expand Down Expand Up @@ -313,7 +301,7 @@ public function count(): int
* @param Connection<TResource>|null $connection
* @return $this<TResource>
*/
public function reclaim(Connection $connection = null): static
public function reclaim(?Connection $connection = null): static
{
if ($connection !== null) {
$this->push($connection);
Expand All @@ -331,7 +319,7 @@ public function reclaim(Connection $connection = null): static
* @param Connection<TResource>|null $connection
* @return $this<TResource>
*/
public function destroy(Connection $connection = null): static
public function destroy(?Connection $connection = null): static
{
try {
if ($connection !== null) {
Expand Down
13 changes: 4 additions & 9 deletions tests/Pools/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ConnectionTest extends TestCase
*/
protected Connection $object;

#[\Override]
public function setUp(): void
{
$this->object = new Connection('x');
Expand Down Expand Up @@ -53,19 +54,15 @@ public function testSetResource(): void

public function testSetPool(): void
{
$pool = new Pool('test', 1, function () {
return 'x';
});
$pool = new Pool('test', 1, fn () => 'x');

$this->assertNull($this->object->getPool());
$this->assertInstanceOf(Connection::class, $this->object->setPool($pool));
}

public function testGetPool(): void
{
$pool = new Pool('test', 1, function () {
return 'x';
});
$pool = new Pool('test', 1, fn () => 'x');

$this->assertNull($this->object->getPool());
$this->assertInstanceOf(Connection::class, $this->object->setPool($pool));
Expand All @@ -82,9 +79,7 @@ public function testGetPool(): void

public function testReclaim(): void
{
$pool = new Pool('test', 2, function () {
return 'x';
});
$pool = new Pool('test', 2, fn () => 'x');

$this->assertEquals(2, $pool->count());

Expand Down
27 changes: 8 additions & 19 deletions tests/Pools/GroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,22 @@ class GroupTest extends TestCase
{
protected Group $object;

#[\Override]
public function setUp(): void
{
$this->object = new Group();
}

public function testAdd(): void
{
$this->object->add(new Pool('test', 1, function () {
return 'x';
}));
$this->object->add(new Pool('test', 1, fn () => 'x'));

$this->assertInstanceOf(Pool::class, $this->object->get('test'));
}

public function testGet(): void
{
$this->object->add(new Pool('test', 1, function () {
return 'x';
}));
$this->object->add(new Pool('test', 1, fn () => 'x'));

$this->assertInstanceOf(Pool::class, $this->object->get('test'));

Expand All @@ -40,9 +37,7 @@ public function testGet(): void

public function testRemove(): void
{
$this->object->add(new Pool('test', 1, function () {
return 'x';
}));
$this->object->add(new Pool('test', 1, fn () => 'x'));

$this->assertInstanceOf(Pool::class, $this->object->get('test'));

Expand All @@ -55,9 +50,7 @@ public function testRemove(): void

public function testReset(): void
{
$this->object->add(new Pool('test', 5, function () {
return 'x';
}));
$this->object->add(new Pool('test', 5, fn () => 'x'));

$this->assertEquals(5, $this->object->get('test')->count());

Expand All @@ -74,9 +67,7 @@ public function testReset(): void

public function testReconnectAttempts(): void
{
$this->object->add(new Pool('test', 5, function () {
return 'x';
}));
$this->object->add(new Pool('test', 5, fn () => 'x'));

$this->assertEquals(3, $this->object->get('test')->getReconnectAttempts());

Expand All @@ -87,9 +78,7 @@ public function testReconnectAttempts(): void

public function testReconnectSleep(): void
{
$this->object->add(new Pool('test', 5, function () {
return 'x';
}));
$this->object->add(new Pool('test', 5, fn () => 'x'));

$this->assertEquals(1, $this->object->get('test')->getReconnectSleep());

Expand All @@ -113,7 +102,7 @@ public function testUse(): void
$this->assertEquals(1, $pool3->count());

// @phpstan-ignore argument.type
$this->object->use(['pool1', 'pool3'], function ($one, $three) use ($pool1, $pool2, $pool3) {
$this->object->use(['pool1', 'pool3'], function ($one, $three) use ($pool1, $pool2, $pool3): void {
$this->assertEquals('1', $one);
$this->assertEquals('3', $three);

Expand Down
13 changes: 6 additions & 7 deletions tests/Pools/PoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ class PoolTest extends TestCase
*/
protected Pool $object;

#[\Override]
public function setUp(): void
{
$this->object = new Pool('test', 5, function () {
return 'x';
});
$this->object = new Pool('test', 5, fn () => 'x');
}

public function testGetName(): void
Expand Down Expand Up @@ -112,7 +111,7 @@ public function testPop(): void
public function testUse(): void
{
$this->assertEquals(5, $this->object->count());
$this->object->use(function ($resource) {
$this->object->use(function ($resource): void {
$this->assertEquals(4, $this->object->count());
$this->assertEquals('x', $resource);
});
Expand Down Expand Up @@ -267,7 +266,7 @@ public function testTelemetry(): void
$telemetry = new TestTelemetry();
$this->object->setTelemetry($telemetry);

$allocate = function (int $amount, callable $assertion) {
$allocate = function (int $amount, callable $assertion): void {
$connections = [];
for ($i = 0; $i < $amount; $i++) {
$connections[] = $this->object->pop();
Expand All @@ -282,15 +281,15 @@ public function testTelemetry(): void

$this->assertEquals(5, $this->object->count());

$allocate(3, function () use ($telemetry) {
$allocate(3, function () use ($telemetry): void {
$this->assertEquals([1, 2, 3], $telemetry->gauges['pool.connection.open.count']->values);
$this->assertEquals([1, 2, 3], $telemetry->gauges['pool.connection.active.count']->values);
$this->assertEquals([0, 0, 0], $telemetry->gauges['pool.connection.idle.count']->values);
});

$this->assertEquals(5, $this->object->count());

$allocate(1, function () use ($telemetry) {
$allocate(1, function () use ($telemetry): void {
$this->assertEquals([1, 2, 3, 3, 3, 3, 3], $telemetry->gauges['pool.connection.open.count']->values);
$this->assertEquals([1, 2, 3, 2, 1, 0, 1], $telemetry->gauges['pool.connection.active.count']->values);
$this->assertEquals([0, 0, 0, 1, 2, 3, 2], $telemetry->gauges['pool.connection.idle.count']->values);
Expand Down