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

关于client_id服务重启后重置 #66

Open
wzfjesun opened this issue Feb 1, 2020 · 6 comments
Open

关于client_id服务重启后重置 #66

wzfjesun opened this issue Feb 1, 2020 · 6 comments

Comments

@wzfjesun
Copy link

wzfjesun commented Feb 1, 2020

你好,每次restart或者reload 服务,client_id都会从00000001 开始重新开始,能否有办法让client_id生成类似uuid的永久不会重复的id呢?即使服务重启也不会重复

@wzfjesun wzfjesun changed the title 关于client_id重复 关于client_id服务重启后重置 Feb 1, 2020
@wzfjesun
Copy link
Author

wzfjesun commented Feb 1, 2020

看了这个issue:walkor/workerman#286
创建了新类,继承了GatewayWorker\Gateway类,自定义了这个方法gateway::generateConnectionId,但是不知道这个新类该如何使用?请指点一下

@walkor
Copy link
Owner

walkor commented Feb 2, 2020

将gatewayworker/src/Lib/Context.php 里 addressToClientId 和 clientIdToAddress 替换成如下代码。这样就加了一个时间戳前缀,避免重启后client_id冲突

    /**
     * 通讯地址到 client_id 的转换
     *
     * @param int $local_ip
     * @param int $local_port
     * @param int $connection_id
     * @return string
     */
    public static function addressToClientId($local_ip, $local_port, $connection_id)
    {
        return bin2hex(pack('NNnN', time(), $local_ip, $local_port, $connection_id));
    }

    /**
     * client_id 到通讯地址的转换
     *
     * @param string $client_id
     * @return array
     * @throws Exception
     */
    public static function clientIdToAddress($client_id)
    {
        if (strlen($client_id) !== 28) {
            echo new Exception("client_id $client_id is invalid");
            return false;
        }
        return unpack('Ntime/Nlocal_ip/nlocal_port/Nconnection_id', pack('H*', $client_id));
    }

@wzfjesun
Copy link
Author

wzfjesun commented Feb 3, 2020

测试了可用,非常谢谢~,我在GatewayClient\Gateway里也做了一样处理。不过这样修改源码不方便升级

@7iv
Copy link

7iv commented Sep 13, 2021

将gatewayworker/src/Lib/Context.php 里 addressToClientId 和 clientIdToAddress 替换成如下代码。这样就加了一个时间戳前缀,避免重启后client_id冲突

    /**
     * 通讯地址到 client_id 的转换
     *
     * @param int $local_ip
     * @param int $local_port
     * @param int $connection_id
     * @return string
     */
    public static function addressToClientId($local_ip, $local_port, $connection_id)
    {
        return bin2hex(pack('NNnN', time(), $local_ip, $local_port, $connection_id));
    }

    /**
     * client_id 到通讯地址的转换
     *
     * @param string $client_id
     * @return array
     * @throws Exception
     */
    public static function clientIdToAddress($client_id)
    {
        if (strlen($client_id) !== 28) {
            echo new Exception("client_id $client_id is invalid");
            return false;
        }
        return unpack('Ntime/Nlocal_ip/nlocal_port/Nconnection_id', pack('H*', $client_id));
    }

替换了之后会出现client_id混乱,onConnect/onMessage/onClose会出现三个不同的client_id
比如
onConnect时,client_id=613f0a567f0000010b5400000001
onMessage时,client_id=613f0a7f7f0000010b5400000001
onClose时,client_id=613f0a997f0000010b5400000001
这是为何?

@walkor
Copy link
Owner

walkor commented Sep 13, 2021

return bin2hex(pack('NNnN', time(), $local_ip, $local_port, $connection_id));

改成

if (!$this->time) {
    $this->time = time();
}
return bin2hex(pack('NNnN', $this->time, $local_ip, $local_port, $connection_id));

试下。

时间戳应该是个定值,只有进程重启了才能变化。

@7iv
Copy link

7iv commented Sep 13, 2021

return bin2hex(pack('NNnN', time(), $local_ip, $local_port, $connection_id));

改成

if (!$this->time) {
    $this->time = time();
}
return bin2hex(pack('NNnN', $this->time, $local_ip, $local_port, $connection_id));

试下。

时间戳应该是个定值,只有进程重启了才能变化。

非常感谢回复!可以了!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants