Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
48 changes: 46 additions & 2 deletions src/object-pool/src/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,85 @@

class Channel
{
/**
* Coroutine channel for handling objects in coroutine mode.
*/
protected CoChannel $channel;

/**
* Queue for handling objects in non-coroutine mode.
*/
protected SplQueue $queue;

public function __construct(protected int $size)
{
/**
* Constructor for Channel.
*
* @param int $size The maximum size of the channel
*/
public function __construct(
/**
* The maximum size of the channel.
*
* @var int
*/
protected int $size
) {
$this->channel = new CoChannel($size);
$this->queue = new SplQueue();
}

/**
* Retrieves an object from the channel.
*
* @param float $timeout The maximum time to wait for an object
*
* @return false|object The retrieved object or false on timeout
*/
public function pop(float $timeout): false|object
{
if ($this->isCoroutine()) {
return $this->channel->pop($timeout);
}

return $this->queue->shift();
}

/**
* Adds an object to the channel.
*
* @param object $data The object to add to the channel
*
* @return bool Whether the operation was successful
*/
public function push(object $data): bool
{
if ($this->isCoroutine()) {
return $this->channel->push($data);
}
$this->queue->push($data);

return true;
}

/**
* Gets the current number of objects in the channel.
*
* @return int The number of objects in the channel
*/
public function length(): int
{
if ($this->isCoroutine()) {
return $this->channel->getLength();
}

return $this->queue->count();
}

/**
* Determines if the code is running in a coroutine context.
*
* @return bool Whether the current execution is in a coroutine
*/
protected function isCoroutine(): bool
{
return Coroutine::id() > 0;
Expand Down
57 changes: 0 additions & 57 deletions src/object-pool/src/ConstantFrequency.php

This file was deleted.

25 changes: 25 additions & 0 deletions src/object-pool/src/Contracts/RecycleStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Hypervel\ObjectPool\Contracts;

use Hypervel\ObjectPool\ObjectPool;

interface RecycleStrategy
{
/**
* Determines if the pool should recycle objects at this time.
*/
public function shouldRecycle(ObjectPool $pool): bool;

/**
* Performs the recycling operation on the pool.
*/
public function recycle(ObjectPool $pool): void;

/**
* Returns the timestamp of the last recycling operation.
*/
public function getLastRecycledTimestamp(): int;
}
13 changes: 13 additions & 0 deletions src/object-pool/src/Contracts/TimeRecycleStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Hypervel\ObjectPool\Contracts;

interface TimeRecycleStrategy extends RecycleStrategy
{
/**
* Gets the time interval between recycling operations.
*/
public function getRecycleTime(): float;
}
12 changes: 0 additions & 12 deletions src/object-pool/src/LowFrequencyInterface.php

This file was deleted.

Loading