I"d like to ask you guys a few questions about tcp
communication.
premise: under the structure of BBUBERS
website, now I have implemented a ES
search on another server, which is currently provided to the original website through an interface. (customers initiate a search on a web page and send it to the php server
, php server
and then initiate a curl
request . Get the data and return it to the front end) but now the requirement is that a keyword search will search more than a dozen business lines (one business line corresponds to an index), and there are several categories below the business line. At present, I still use the interface. After receiving the data, I cycle through
ES
and return it to the php server
, but I think the efficiency will be a little slow.
optimization strategy: at present, what I think of is to build a tcp server
on ES search server
with swoole
extension. php server
no longer accesses the interface, and accessing the tcp server
does not send the search and filter conditions of all business lines at once, but sends the search conditions of one business line at a time, and receives the response of tcp server
at the same time, duplex communication.
but I experimented many times and found some problems:
-
After
stream client
established byphp server
establishes the request data,fclose ()
cannot close the link and will not return the link until it times out, as is the case with the socket function of php. (this happens only if there is data interaction, and the client can be disconnected after numerous data interactions are established.) unless the server immediately disconnects the link after sending the data, theswoole
document warns not to break the link immediately after sending , but I don"t know when the server should break the link (what event callback (try to use theonbufferEmpty
event callback function), but there will be no penalty at all. Or some other.), but the client can"t disconnect, so it gets stuck. -
php server
how to send and receivetcp server
data in a loop? the stream function of, (php is blocked, so how to carry out two-way communication without blocking? - under this architecture, can such an optimization strategy be implemented? Will it work?
php server request ES tcp server partial code
$streamClient = stream_socket_client("tcp://{$tcpHost}:{$tcpPort}", $errno, $errstr);
//
$categoriesFilter = [];
foreach ($categoriesFilter as $categoryFilter) {
fwrite($streamClient, json_encode($categoryFilter);
}
then I don"t know how to write and receive data at the same time. And ensure that the data of each line of business has been sent and received a response.
ES tcp server partial Code
$searchServer = new \swoole_server("0.0.0.0", 9501);
$searchServer->on("connect", function (\Swoole\Server $server, $fd) {
dump($server->connection_info($fd));
});
$searchServer->on("receive", function (\Swoole\Server $server, $fd, $reactor_id, $request) {
global $keyword;
global $elastic;
$requestArray = json_decode($request, true);
$keyword = $requestArray;
// es
$result = $elastic->indexFilterAndSearch($requestArray);
$server->send($fd, json_encode($result));
});
$searchServer->on("WorkerStart", function (\Swoole\Server $server, $work_id) {
global $elastic;
$elastic = new ElasticSearch;
});
$searchServer->on("close", function (\Swoole\Server $server, $fd, $reactorId) {
global $keyword;
$clientConnectionInfo = $server->connection_info($fd);
file_put_contents("swoole_log.log", json_encode([
"connectionTime" => date("Y-m-d H:i:s", $clientConnectionInfo["connect_time"]),
"closeTime" => date("Y-m-d H:i:s"),
"keyword" => $keyword,
"ip" => $clientConnectionInfo["remote_ip"]
]) . PHP_EOL, FILE_APPEND);
});
//
$searchServer->on("bufferEmpty", function (\Swoole\Server $server, $fd) {
dump("send this $fd message is down");
});
$searchServer->start();
ask the bosses to give us some advice, is it all right to optimize in this way? If possible, where should I start to solve the above two problems?