imiphp / imiphp/imi

关于优化Lock::lock的建议

Open
#516 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

2.1 3.0 enhancement
Dominant language
PHP
Stars
1.2k
Forks
169
PR merge metrics
No merged PRs in 30d

Description

  • 你遇到了什么问题、建议:

目前Lock::lock的第一个参数ID 是给予static::getInstance的lockConfigId
这样的操作有一定误导性。
在实际业务中,可能为存在很多动态的lockId,不可能为大量动态的lockId来写对应的Lock实现类。

因此提出2中修改意见:
第一种:建议修改将id参数给与static::getInstance的lockId参数,并给Lock::lock增加第四个参数$lockConfigId。

image image

第二种: 修改getInstance逻辑,将lockConfigId不存在时的逻辑进行优化,服于默认实现
image

image

实际业务使用示例:
image

  • 请执行下面的命令获取环境信息。

php -v & php --ri swoole & composer info | grep -a imi

# 粘贴到这里

  • 如果可以,提供最小可复现代码:
    第一种修改代码实现:
 /**
     * 加锁,会挂起协程.
     *
     * @param callable|null $taskCallable      加锁后执行的任务,可为空;如果不为空,则执行完后自动解锁
     * @param callable|null $afterLockCallable 当获得锁后执行的回调,只有当 $taskCallable 不为 null 时有效。该回调返回 true 则不执行 $taskCallable
     */
    public static function lock(?string $id = null, ?callable $taskCallable = null, ?callable $afterLockCallable = null, ?string $lockConfigId = null): bool
    {
        return static::getInstance($lockConfigId, $id)->lock($taskCallable, $afterLockCallable);
    }

第二种修改代码实现:

/**
     * 获取锁对象
     */
    public static function getInstance(?string $lockConfigId = null, ?string $lockId = null): ILockHandler
    {
        if (!self::$inited) {
            self::init();
        }
        if (!$lockConfigId) {
            $lockConfigId = static::getDefaultId();
        }
        $instances = &self::$instances;
        if (null === $lockId && isset($instances[$lockConfigId])) {
            return $instances[$lockConfigId];
        }
        $options = &self::$options;
        if (!isset($options[$lockConfigId])) {
            $defaultId = static::getDefaultId();
            if (!isset($options[$defaultId])) {
                throw new \RuntimeException(sprintf('Lock %s does not exists, has no default lock config', $lockConfigId));
            }
            $lockId = $lockConfigId;
            $option = $options[$defaultId];
        } else {
            $option = $options[$lockConfigId];
        }

        if (null === $lockId) {
            return $instances[$lockConfigId] = App::newInstance($option->class, $lockConfigId, $option->options);
        } else {
            return App::newInstance($option->class, $lockId, $option->options);
        }
    }

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the Lock::lock and getInstance entry points and compare the two proposed API changes in the issue. Determine how dynamic lock IDs and lockConfigId should interact, including the default configuration path; the work is done when one behavior is selected and consistently implemented.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.