国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

請教ws斷開后redis鏈接如何刪除?

m15800825437@163.com

使用workerman實現(xiàn)了websocket服務(wù)端

業(yè)務(wù)中使用了
use Workerman\RedisQueue\Client;

在onClose內(nèi)部關(guān)閉connection

$ws->onClose = function($connection)
{
    $connection->close();
    unset($connection);
};

但是運行 php demo.php connections時
redis鏈接并沒有被關(guān)閉掉
截圖

請教下如何關(guān)閉它們。否則多次鏈接之后,redis進程越來越多了

1406 3 2
3個回答

walkor 打賞

Workerman\RedisQueue\Client本身是復(fù)用連接的,不會建立很多連接。Workerman\RedisQueue\Client目前不支持關(guān)閉,

$ws->onClose = function($connection)
{
    $connection->close();
    unset($connection);
};

另外這個代碼是關(guān)閉客戶端ws連接,并不是關(guān)閉Workerman\RedisQueue\Clientunset($connection);這句沒有必要,$connection在函數(shù)里是臨時變量,出了函數(shù)作用域自然就釋放了.

“否則多次鏈接之后,redis進程越來越多了”
這個也是錯誤說法,多次連接不會影響進程數(shù),進程和連接是有區(qū)別的。進程數(shù)是固定的,啟動后就不會變動了。
客戶端多次連接,不影響Workerman\RedisQueue\Client連接數(shù),Workerman\RedisQueue\Client會復(fù)用連接。開發(fā)過程中不用關(guān)注Workerman\RedisQueue\Client的連接數(shù)。

  • m15800825437@163.com 2022-05-04

    感謝回復(fù),也就是說我這邊其實不用關(guān)注 redis的連接數(shù),它內(nèi)部自己會復(fù)用

  • walkor 2022-05-04

  • walkor 2022-05-04

    Workerman\RedisQueue\Client 一般是放在onWorkerStart進程啟動時初始化的,一個進程只會執(zhí)行一次。不要重復(fù)初始化就行。

請教大佬,如下這樣使用是否正確哦?

$ws->onWorkerStart = function() {
    global $asyncRedisQueue;
    $asyncRedisQueue = new AsyncRedisQueue('redis://127.0.0.1:6379');
};

$ws->onConnect = function($connection)
{
    // WebSocket連接成功
    $connection->onWebSocketConnect = function($connection)
    {
        global $asyncRedisQueue;
        // 訂閱并推送消息
        $asyncRedisQueue->subscribe('Message:Center:Queue:' . $connection->id, function($info_json) use ($connection) {
            $connection->send($info_json);
        });
    };
};

$ws->onClose = function($connection)
{
    $connection->close();
};
  • walkor 2022-05-04

    連接關(guān)閉的時候取消對應(yīng)的訂閱,不然訂閱越來越多,占用內(nèi)存越來越大,這種算內(nèi)存泄漏

謝謝大佬提點,已經(jīng)改正

$ws->onWorkerStart = function() {
    global $asyncRedisQueue;
    $asyncRedisQueue = new AsyncRedisQueue('redis://127.0.0.1:6379');
};

$ws->onConnect = function($connection)
{
    // WebSocket連接成功
    $connection->onWebSocketConnect = function($connection)
    {
        global $asyncRedisQueue;
        // 訂閱并推送消息
        $asyncRedisQueue->subscribe('Message:Center:Queue:' . $connection->id, function($info_json) use ($connection) {
            $connection->send($info_json);
        });
    };
};

$ws->onClose = function($connection)
{
    global $asyncRedisQueue;
    // 取消訂閱
    $asyncRedisQueue->unsubscribe('Message:Center:Queue:' . $connection->id);
    $connection->close();
};
  • 暫無評論
年代過于久遠,無法發(fā)表回答
??