想做一个连接池,想通过swoole/http/server去监听unixsocket去实现,好处是这样连接不会造成服务器出现大量time_wait。如果监听tcp/ip那情况会很糟糕的
但是我写了一个代码,虽然可以监听,并且产生出对应的unixsocket文件,但是我用curl库带上CURLOPT_UNIX_SOCKET_PATH参数去请求,发现服务端的on("request")没有触发,也就是没有收到请求
请问我要怎么做?
相关curl请求unixsocket代码跑nginx的unixsocket网站获取数据完全没问题,所以请求的代码我已经测试过了
server的代码
<?php
use Swoole\Http\Response;
include_once("../../init.base.cli.php");
/**
*
*
*/
class HtServer {
public $server;
private $pm_listenAddress = "/home/htserver.sock";//"0.0.0.0";
private $pm_listenPort = 0;
public function __construct() {
if(file_exists($logDir) == false || is_dir($logDir) == false)
{
@mkdir($logDir,0777);
}
$serverConfig = array(
"heartbeat_check_interval" => 5,//表示每5秒遍历一次,一个连接如果60秒内未向服务器发送任何数据,此连接将被强制关闭
"heartbeat_idle_time" => 6000,//一个连接60秒未互相通讯则自动断开
"open_tcp_keepalive" => 1,//开启踢掉死链接
// "daemonize"=>1,//守护进程化
"open_tcp_nodelay"=>1,//开启后TCP连接发送数据时会关闭Nagle合并算法,立即发往客户端连接
// "ssl_key_file" => "",//sslkey
// "ssl_cert_file" => "",//sslcrt
// "task_worker_num"=>10,//每个任务占用一个
"task_enable_coroutine"=>true,
"log_file" => $logDir."/swoole.log"
);
if($this->pm_listenPort >0)
{
$this->server = new Swoole\Http\Server($this->pm_listenAddress, $this->pm_listenPort,SWOOLE_BASE,SWOOLE_SOCK_TCP);
}else
{
$this->server = new Swoole\Http\Server($this->pm_listenAddress,0 ,SWOOLE_BASE,SWOOLE_UNIX_STREAM);
}
//配置信息
$this->server->set($serverConfig);
$this->server->on("request",function(swoole_http_request $request, swoole_http_response $response)
{
echo "on request:\r\n";
print_r($request);
$response->write("get your data");
});
$this->server->on("close", function ($ser, $fd) {
echo "client {$fd} closed\n";
$clientId = $this->pm_clientFdInfoMap[$fd]["clientId"];
if($clientId != "")
{
unset($this->pm_clientInfoMap[$clientId]);
}
unset($this->pm_clientFdInfoMap[$fd]);
});
$this->server->on("start",function(Swoole\Http\Server $server)
{
$logContent = "on start,pid:".getmypid();
echo $logContent."\n";
});
echo "htserver Start\r\n";
$this->server->start();
}
}
new HtServer();
请求页面的代码
function curlPost($url , $fields , $unixSocket = null ,$headers = array(),$useCert = array(),$connectTimeout=2,$requestTimeout=3000)
{
if(strlen(trim($url)) == 0){ return ;}
$isSSL = substr($url, 0, 8) == "https://" ? true : false;
$curl = curl_init();
curl_setopt($curl , CURLOPT_URL , $url);
curl_setopt($curl , CURLOPT_HEADER , false);
curl_setopt($curl , CURLOPT_CONNECTTIMEOUT , $connectTimeout);//连接超时
if($unixSocket)
{
curl_setopt($curl, CURLOPT_UNIX_SOCKET_PATH, $unixSocket);
echo "use unix socket:".$unixSocket."\r\n";
}
if($requestTimeout ==0)
{
curl_setopt($curl, CURLOPT_NOSIGNAL, true); //注意,毫秒超时一定要设置这个
curl_setopt($curl, CURLOPT_TIMEOUT_MS, 10); //注意,毫秒超时一定要设置这个
}else
{
curl_setopt($curl , CURLOPT_TIMEOUT , $requestTimeout);//执行超时
}
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); //302重定向
if(!empty($fields))
{
curl_setopt($curl , CURLOPT_POST , true);
curl_setopt($curl , CURLOPT_POSTFIELDS , $fields);
}
//处理post字符串时特殊的header
if(is_string($fields))
{
for($i=0;$i<count($headers);)
{
if(strpos($headers[$i] , "Content-Length:"))
{
array_splice($headers, $i,1);
}else
{
$i++;
}
}
array_push($headers, 'Content-Length: ' . strlen($fields));
}
if(!empty($headers))
{
for($i=0;$i<count($headers);)
{
if(strpos($headers[$i] , "Expect:"))
{
array_splice($headers, $i,1);
break;
}else
{
$i++;
}
}
$headers[] = "Expect:";
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}else
{
$headers = array("Expect:");
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($curl , CURLOPT_RETURNTRANSFER , true);
if ($isSSL == true)
{
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl , CURLOPT_SSL_VERIFYHOST , false);
}
if(!empty($useCert)){
//设置证书
//使用证书:cert 与 key 分别属于两个.pem文件
curl_setopt($curl,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($curl,CURLOPT_SSLCERT, $useCert["cert"]);
curl_setopt($curl,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($curl,CURLOPT_SSLKEY, $useCert["key"]);
}
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
$rs = curl_exec($curl);
// echo "shit:";print_r(curl_getinfo($curl));
if($requestTimeout >0)
{
$downloadContentLength = curl_getinfo($curl,CURLINFO_CONTENT_LENGTH_DOWNLOAD);
$httpCode = curl_getinfo($curl,CURLINFO_HTTP_CODE);
if($httpCode != 200 && $downloadContentLength < 0)
{
//没任何下载内容
SystemUtil::$s_lastCurlHttpCode = -404;
}else
{
SystemUtil::$s_lastCurlHttpCode = $httpCode;
}
}
curl_close($curl);
return $rs;
}
//echo curlPost("http://fpm2.okguo.com/test.php", array("a"=>"呵呵哒","b"=>'abcde'),"/run/fmp2.okguo.com.sock");
echo curlPost("http://127.0.0.1/test.php", array("a"=>"呵呵哒","b"=>'abcde'),"/home/htserver.sock");
没有人了解嘛?官方人员能帮我解答一下吗