Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Lock heartbeat #820

Closed
wants to merge 2 commits into from
Closed
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
43 changes: 41 additions & 2 deletions src/lock/src/Driver/AbstractLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
namespace FriendsOfHyperf\Lock\Driver;

use FriendsOfHyperf\Lock\Exception\LockTimeoutException;
use Hyperf\Context\ApplicationContext;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Engine\Coroutine;
use Hyperf\Stringable\Str;
use Hyperf\Support\Traits\InteractsWithTime;
use Override;

use Throwable;
use function Hyperf\Support\now;

abstract class AbstractLock implements LockInterface
Expand All @@ -32,6 +36,8 @@ abstract class AbstractLock implements LockInterface
*/
protected int $sleepMilliseconds = 250;

protected bool $isRun = true;

/**
* Create a new lock instance.
*/
Expand All @@ -45,6 +51,11 @@ public function __construct(protected string $name, protected int $seconds, ?str
*/
abstract public function acquire(): bool;

/**
* Set the lock.
*/
abstract protected function set(): bool;

/**
* Release the lock.
*/
Expand All @@ -55,10 +66,14 @@ abstract public function release(): bool;
* {@inheritdoc}
*/
#[Override]
public function get(?callable $callback = null)
public function get(?callable $callback = null, int $heartbeat = 0)
{
$result = $this->acquire();

if ($result && $heartbeat > 0){
$this->loop($heartbeat);
}

if ($result && is_callable($callback)) {
try {
return $callback();
Expand All @@ -75,7 +90,7 @@ public function get(?callable $callback = null)
* {@inheritdoc}
*/
#[Override]
public function block(int $seconds, ?callable $callback = null)
public function block(int $seconds, ?callable $callback = null, int $heartbeat = 0)
{
$starting = ((int) now()->format('Uu')) / 1000;
$milliseconds = $seconds * 1000;
Expand All @@ -90,6 +105,10 @@ public function block(int $seconds, ?callable $callback = null)
usleep($this->sleepMilliseconds * 1000);
}

if ($heartbeat > 0){
$this->loop($heartbeat);
}

if (is_callable($callback)) {
try {
return $callback();
Expand Down Expand Up @@ -143,4 +162,24 @@ protected function isOwnedByCurrentProcess(): bool
{
return $this->isOwnedBy($this->owner);
}

protected function loop(int $heartbeat = 5): void
{
Coroutine::create(
function () use ($heartbeat) {
while ($this->isRun) {
sleep($heartbeat);
if (!$this->isRun){
return;
}
try {
$this->set();
} catch (Throwable $throwable){
ApplicationContext::getContainer()->get(StdoutLoggerInterface::class)?->error((string) $throwable);
}
}
}
);
}

}
9 changes: 9 additions & 0 deletions src/lock/src/Driver/CacheLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,20 @@ public function acquire(): bool
return $this->store->set($this->name, $this->owner, $this->seconds);
}

#[Override]
public function set(): bool
{
return $this->store->set($this->name, $this->owner, $this->seconds);
}

/**
* Release the lock.
*/
#[Override]
public function release(): bool
{
$this->isRun = false;

if ($this->isOwnedByCurrentProcess()) {
return $this->store->delete($this->name);
}
Expand All @@ -68,6 +76,7 @@ public function release(): bool
#[Override]
public function forceRelease(): void
{
$this->isRun = false;
$this->store->delete($this->name);
}

Expand Down
11 changes: 11 additions & 0 deletions src/lock/src/Driver/CoroutineLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,21 @@ public function acquire(): bool
return true;
}

#[Override]
protected function set(): bool
{
return false;
}

Comment on lines +88 to +93
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

建议在 set 方法中实现心跳更新逻辑
该方法目前仅返回 false,在长时间持有锁时无法及时更新锁的过期时间,可能会导致锁意外过期。建议在此处实现心跳/续租逻辑,以确保在持锁期间动态刷新过期时间。

是否需要帮您编写一个基于定时器或循环的续租逻辑来更新锁的过期时间?


/**
* Release the lock.
*/
#[Override]
public function release(): bool
{
$this->isRun = false;

if ($this->isOwnedByCurrentProcess()) {
return (self::$channels[$this->name] ?? null)?->pop(0.01) ? true : false;
}
Expand All @@ -104,6 +113,8 @@ public function release(): bool
#[Override]
public function forceRelease(): void
{
$this->isRun = false;

if (! $chan = self::$channels[$this->name] ?? null) {
return;
}
Expand Down
22 changes: 22 additions & 0 deletions src/lock/src/Driver/DatabaseLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,32 @@ public function acquire(): bool
return $acquired;
}

/**
* Set the lock.
*/
#[Override]
protected function set(): bool
{
$updated = $this->connection->table($this->table)
->where('key', $this->name)
->where('owner', $this->owner)
->update(
[
'expiration' => $this->expiresAt()
]
);
return $updated >= 1;
}


/**
* Release the lock.
*/
#[Override]
public function release(): bool
{
$this->isRun = false;

if ($this->isOwnedByCurrentProcess()) {
$this->connection->table($this->table)
->where('key', $this->name)
Expand All @@ -94,6 +114,8 @@ public function release(): bool
#[Override]
public function forceRelease(): void
{
$this->isRun = false;

$this->connection->table($this->table)
->where('key', $this->name)
->delete();
Expand Down
11 changes: 11 additions & 0 deletions src/lock/src/Driver/FileSystemLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,22 @@ public function acquire(): bool
return $this->store->set($this->name, $this->owner, $this->seconds) == true;
}

/**
* Set the lock.
*/
#[Override]
protected function set(): bool
{
return $this->store->set($this->name, $this->owner, $this->seconds) == true;
}

/**
* Release the lock.
*/
#[Override]
public function release(): bool
{
$this->isRun = false;
if ($this->isOwnedByCurrentProcess()) {
return $this->store->delete($this->name);
}
Expand All @@ -66,6 +76,7 @@ public function release(): bool
#[Override]
public function forceRelease(): void
{
$this->isRun = false;
$this->store->delete($this->name);
}

Expand Down
4 changes: 2 additions & 2 deletions src/lock/src/Driver/LockInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface LockInterface
* @param (callable(): T)|null $callback
* @return ($callback is null ? bool : T)
*/
public function get(?callable $callback = null);
public function get(?callable $callback = null, int $heartbeat = 0);

/**
* Attempt to acquire the lock for the given number of seconds.
Expand All @@ -31,7 +31,7 @@ public function get(?callable $callback = null);
* @return ($callback is null ? bool : T)
* @throws LockTimeoutException
*/
public function block(int $seconds, ?callable $callback = null);
public function block(int $seconds, ?callable $callback = null, int $heartbeat = 0);

/**
* Release the lock.
Expand Down
15 changes: 15 additions & 0 deletions src/lock/src/Driver/RedisLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,26 @@ public function acquire(): bool
return $this->store->setNX($this->name, $this->owner) === true;
}

/**
* Set the lock.
*/
#[Override]
protected function set(): bool
{
if ($this->seconds > 0) {
return $this->store->set($this->name, $this->owner, ['EX' => $this->seconds]) == true;
}
return $this->store->setNX($this->name, $this->owner) === true;
}


/**
* Release the lock.
*/
#[Override]
public function release(): bool
{
$this->isRun = false;
return (bool) $this->store->eval(LuaScripts::releaseLock(), [$this->name, $this->owner], 1);
}

Expand All @@ -67,6 +81,7 @@ public function release(): bool
#[Override]
public function forceRelease(): void
{
$this->isRun = false;
$this->store->del($this->name);
}

Expand Down
Loading