{"record":{"id":"4bec0a0036f5f218","repo":"walkor/workerman","slug":"redis-connect-config-host-config-port","errorCode":null,"errorMessage":"Redis connect {$config['host']}:{$config['port']} fail.","messagePattern":"Redis connect (.+?):(.+?) fail\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/Protocols/Http/Session/RedisSessionHandler.php","lineNumber":142,"sourceCode":"                    $obj = new \\stdClass();\n                    Context::set('context.onDestroy', $obj);\n                }\n                DestructionWatcher::watch($obj, $closure);\n            }\n        }\n        return $connection;\n    }\n\n    /**\n     * Create redis connection.\n     * @param array $config\n     * @return Redis\n     */\n    protected function createRedisConnection(array $config): Redis|RedisCluster\n    {\n        $redis = new Redis();\n        if (false === $redis->connect($config['host'], $config['port'], $config['timeout'])) {\n            throw new RuntimeException(\"Redis connect {$config['host']}:{$config['port']} fail.\");\n        }\n        if (!empty($config['auth'])) {\n            $redis->auth($config['auth']);\n        }\n        if (!empty($config['database'])) {\n            $redis->select((int)$config['database']);\n        }\n        if (empty($config['prefix'])) {\n            $config['prefix'] = 'redis_session_';\n        }\n        $redis->setOption(Redis::OPT_PREFIX, $config['prefix']);\n        return $redis;\n    }\n\n    /**\n     * {@inheritdoc}\n     */\n    public function open(string $savePath, string $name): bool","sourceCodeStart":124,"sourceCodeEnd":160,"githubUrl":"https://github.com/walkor/workerman/blob/1391112a61d23020e11e7b89f17050f6cfaea431/src/Protocols/Http/Session/RedisSessionHandler.php#L124-L160","documentation":"RedisSessionHandler::createRedisConnection() calls Redis::connect($config['host'], $config['port'], $config['timeout']) and throws a RuntimeException naming the host:port when the connection cannot be established (older phpredis returns false; newer versions throw RedisException themselves before this check). The config array is whatever you passed to the handler.","triggerScenarios":"Redis server stopped or unreachable; wrong host/port keys in the config array (the handler expects 'host' and 'port'); firewall or security group blocking 6379; timeout too small for the network latency; Redis bound to 127.0.0.1 only while the worker connects to an external address.","commonSituations":"Staging/prod pointing at a Redis that is down; Docker networking mistakes (host 'localhost' instead of the service name); typo'd port; config file using 'server'/'database' style keys instead of 'host'/'port'; cloud Redis requiring TLS so a plain connect fails.","solutions":["Verify reachability from the same host: redis-cli -h <host> -p <port> ping","Check the config array keys passed to RedisSessionHandler are exactly host, port, timeout, auth, database, prefix","Fix environment: start Redis, open the firewall/port, raise 'timeout', use the correct docker network name, or enable TLS settings"],"exampleFix":"// before\n$config = ['address' => '10.0.0.5:6379']; // wrong keys -> host/port missing\nnew RedisSessionHandler($config);\n\n// after\n$config = ['host' => '10.0.0.5', 'port' => 6379, 'timeout' => 2, 'auth' => 'secret', 'database' => 0];\nnew RedisSessionHandler($config);","handlingStrategy":"retry","validationCode":"$config = ['host' => '10.0.0.5', 'port' => 6379, 'timeout' => 2];\n$probe = @fsockopen($config['host'], (int)$config['port'], $errNo, $errStr, 2);\nif ($probe === false) {\n    throw new RuntimeException(\"Redis unreachable at {$config['host']}:{$config['port']}: $errStr\");\n}\nfclose($probe);","typeGuard":null,"tryCatchPattern":"try {\n    $handler = new RedisSessionHandler($config);\n} catch (RuntimeException|RedisException $e) {\n    // transient network failure: back off and retry, then fall back to file sessions\n    sleep(min(2 ** $attempt, 30));\n    return retryOrFallback($e);\n}","preventionTips":["Validate the handler config keys (host, port, timeout) once at startup with a schema check","Monitor Redis availability from the worker hosts and alert before sessions degrade","Keep 'timeout' >= realistic worst-case latency to your Redis"],"tags":["php","workerman","redis","session","connection"],"backgroundTag":"connection-refused","analyzedSha":"1391112a61d23020e11e7b89f17050f6cfaea431","analyzedAt":"2026-08-21T02:05:46.744Z","schemaVersion":2},"datasetVersion":"2026-08-21T03:17:12.404Z"}