Skip to content

Commit

Permalink
添加注释
Browse files Browse the repository at this point in the history
  • Loading branch information
何平 committed Jan 18, 2024
1 parent 2f81119 commit 7b81b90
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 58 deletions.
89 changes: 36 additions & 53 deletions cloud-admin/RedisLock/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,25 @@ final class Lock implements RedisLockInterface
{
/**
* the unique id generated by the current coroutine.
* @phpstan-param string $value
*/
private string $value = '';

/**
* the lock key.
* @phpstan-param string $key
*/
private string $key = '';

/**
* the lock key's lifetime.
* @phpstan-param int $ttl
*/
private int $ttl;

/**
* @var mixed|StdoutLoggerInterface
* @phpstan-param mixed|StdoutLoggerInterface $logger
*/
private readonly StdoutLoggerInterface $logger;

Expand All @@ -60,6 +64,8 @@ final class Lock implements RedisLockInterface
private array $config;

/**
* @phpstan-param Redis|RedisProxy $redis
* @phpstan-param ContainerInterface $container
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
Expand All @@ -69,13 +75,7 @@ public function __construct(private readonly ContainerInterface $container, priv
throw new InvalidArgumentException('StdoutLogger not found#');
}

if (
! (
$config = $this
->container
->get(ConfigInterface::class)
->get('redis_lock')
)
if (! ($config = $this->container->get(ConfigInterface::class)->get('redis_lock'))
) {
throw new InvalidArgumentException('redis lock configuration not found#');
}
Expand All @@ -87,6 +87,9 @@ public function __construct(private readonly ContainerInterface $container, priv
/**
* @throws RedisException
* @throws Throwable
* @phpstan-param int $ttl
* @phpstan-param string $key
* @phpstan-return bool
*/
public function tryLock(string $key, int $ttl = 3): bool
{
Expand All @@ -101,6 +104,11 @@ public function tryLock(string $key, int $ttl = 3): bool
/**
* @throws RedisException
* @throws Throwable
* @phpstan-param string $key
* @phpstan-param int $ttl
* @phpstan-param int $retries
* @phpstan-param int<10000, max> $usleep
* @phpstan-return bool
*/
public function lock(
string $key,
Expand All @@ -116,28 +124,12 @@ public function lock(
$lock = $this->doLock($key, $ttl);

if ($lock) {
$this
->logger
->debug(
sprintf(
'Lock acquired successfully, attempts: %s,Key: %s',
$retryTimes,
$key,
),
);
$this->logger->debug(sprintf('Lock acquired successfully, attempts: %s,Key: %s', $retryTimes, $key));
break;
}

usleep($usleep);
$this
->logger
->debug(
sprintf(
'Try to acquire the lock again, the number of attempts: %s,Key: %s',
$retryTimes,
$key,
),
);
$this->logger->debug(sprintf('Try to acquire the lock again, the number of attempts: %s,Key: %s', $retryTimes, $key));
}

return $lock;
Expand All @@ -146,32 +138,30 @@ public function lock(
/**
* @throws RedisException
* @throws Throwable
* @phpstan-return bool
*/
public function unLock(): bool
{
return (bool) $this->execLuaScript(
Lua::UNLOCK,
[$this->key, $this->value],
1,
);
return (bool) $this->execLuaScript(Lua::UNLOCK, [$this->key, $this->value], 1);
}

/**
* @phpstan-return int
*/
public function lockTtl(): int
{
return $this->ttl;
}

/**
* @throws Throwable
* @phpstan-param int $ttl
* @phpstan-return bool
*/
public function keepAlive(int $ttl = 3): bool
{
try {
$eval = $this->execLuaScript(
Lua::KEEP_ALIVE,
[$this->key, $ttl],
1,
);
$eval = $this->execLuaScript(Lua::KEEP_ALIVE, [$this->key, $ttl], 1);
return $eval !== -2;
} catch (Throwable $e) {
$this->logger->error(formatThrowable($e));
Expand All @@ -182,6 +172,7 @@ public function keepAlive(int $ttl = 3): bool

/**
* @throws Throwable
* @phpstan-return bool
*/
public function isAlive(): bool
{
Expand All @@ -202,6 +193,9 @@ public function isAlive(): bool
/**
* @throws RedisException
* @throws Throwable
* @phpstan-param string $key
* @phpstan-param int $ttl
* @phpstan-return bool
*/
private function doLock(string $key, int $ttl): bool
{
Expand All @@ -213,23 +207,10 @@ private function doLock(string $key, int $ttl): bool
$this->key = $key;

try {
$result = $this->execLuaScript(
Lua::LOCK,
[$key, $this->value, $ttl],
1,
);
$result = $this->execLuaScript(Lua::LOCK, [$key, $this->value, $ttl], 1);

if ($result) {
$this
->logger
->debug(
sprintf(
'coroutine[%s] successfully hold lock[uuid:%s,key:%s], initialize the watchdog',
Coroutine::getCurrent()->getId(),
$this->value,
$this->key,
),
);
$this->logger->debug(sprintf('coroutine[%s] successfully hold lock[uuid:%s,key:%s], initialize the watchdog', Coroutine::getCurrent()->getId(), $this->value, $this->key));
SwowCo::create(function () {
$watchdog = make(WatchDog::class);
$watchdog->sentinel($this, $this->config['watchDogTime'] ?? 60);
Expand All @@ -247,11 +228,13 @@ private function doLock(string $key, int $ttl): bool
/**
* @throws Throwable
* @throws RedisException
* @phpstan-param string $script
* @phpstan-param array $args
* @phpstan-param int $number
* @phpstan-return mixed
*/
private function execLuaScript(string $script, array $args, int $number = 0): mixed
{
return $this
->redis
->evalSha($this->redis->script('load', $script), $args, $number);
return $this->redis->evalSha($this->redis->script('load', $script), $args, $number);
}
}
5 changes: 0 additions & 5 deletions cloud-admin/Vo/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@
final class Collection extends HyperfCollection
{
/**
* @param array $data
* @param string $idKey
* @param string $parentKey
* @param string $nestKey
* @return array
* @phpstan-return array<array-key, mixed>
*/
public static function tree(array $data, string $idKey, string $parentKey, string $nestKey = 'children'): array
Expand Down

0 comments on commit 7b81b90

Please sign in to comment.