Analysis on sphsf-persistent connection in PHP _ PHP Tutorial

Source: Internet
Author: User
Analyze the Sphinx persistent connection problem in PHP. For details about what Sphinx is, I will not describe it here, but Baidu will check it. next I will introduce the analysis of Sphinx persistent connections in PHP. I hope some articles will help you. For details about what Sphinx is, I will not describe it here, but Baidu will check it. next I will introduce the analysis of Sphinx persistent connections in PHP. I hope some articles will help you.

SphinxClient: open
(PECL sphtasks> = 1.0.3)

SphinxClient: open-creates a persistent connection to the search server.

Description
Public bool SphinxClient: open (void)
Establish a persistent connection to the search server.

Parameters
This function has no parameters.

Return value
Returns TRUE if the call succeeds, or FALSE if the call fails.


When optimizing the PHP system code today, some analysis has been made on the persistent connections of sphinx and it is found that the sphinx api of php is not as long as we think it will keep a persistent connection with the searchd process of sphinx in the fastcgi state of php-fpm, the open () method in the sphinx api only provides a single session request to ensure that multiple sphinx calls share a single sphinx tcp connection Channel in a single php process, when php explains that it is running, the connection to the sphinx will be automatically disconnected, instead of staying connected.

This post also proves the idea of the landlord: http://sphinxsearch.com/forum/view.html? Id = 7200


> So it seems that the definition of 'persistent connection' in sphsf-is different from
> Persistent MySql connections when using a PhP API: the persistence is only available SS
> Multiple cballs * in the same php request execution * and not persistence within the client


> Process I. e. processing SS multiple php requests.

We can make such an experiment to prove my point of view:

Add the sphhandler. so extension to php and write the following test code:



$ S = new SphinxClient ();

Var_dump ($ s );
$ S-> setServer ('192. 168.1.108 ', '123 ');
// $ S-> open ();
Var_dump ($ s-> query ('abxx '));
Var_dump ($ s-> query ('abxx '));


Note that $ s-> open () is blocked first. then, we use the strace command in the cli state to trace and execute this php script. if we collect system call information, we will find that:

Two connect-to-192.168.1.108 requests were made in the system call. That is to say, when the open method is not called, the same php runtime will cause two tcp requests to sphashes.


611 fcntl64 (3, F_SETFL, O_RDONLY | O_NONBLOCK) = 0
612 connect (3, {sa_family = AF_INET, sin_port = htons (9312), sin_addr = inet_addr ("192.168.1.108")}, 16) =-1 EINPROGRESS (Operation now in progress)
613 select (4, NULL, [3], NULL, {60, 0}) = 1 (out [3], left {59,999 996 })
614 fcntl64 (3, F_SETFL, O_RDONLY) = 0
615 send (3, "1", 4, MSG_NOSIGNAL) = 4
616 recv (3, "1", 4, 0) = 4
617 send (3, "1312241", 16, MSG_NOSIGNAL) = 16
618 send (3, "246 abxx"..., 140, MSG_NOSIGNAL) = 140
619 recv (3, "131" ', 8, 0) = 8
620 recv (3, "25title4text2"..., 96, 0) = 96
621 close (3)
...................
...................
756 socket (PF_INET, SOCK_STREAM, IPPROTO_IP) = 3
757 fcntl64 (3, F_SETFL, O_RDONLY | O_NONBLOCK) = 0
758 connect (3, {sa_family = AF_INET, sin_port = htons (9312), sin_addr = inet_addr ("192.168.1.108")}, 16) =-1 EINPROGRESS (Operation now in progress)
759 select (4, NULL, [3], NULL, {60, 0}) = 1 (out [3], left {59,999 997 })
760 fcntl64 (3, F_SETFL, O_RDONLY) = 0
761 send (3, "1", 4, MSG_NOSIGNAL) = 4
762 recv (3, "1", 4, 0) = 4
763 send (3, "1312241", 16, MSG_NOSIGNAL) = 16
764 send (3, "246 abxx"..., 140, MSG_NOSIGNAL) = 140
765 recv (3, "131" ', 8, 0) = 8
766 recv (3, "25title4text2"..., 96, 0) = 96
767 close (3) = 0
768 write (1, "array (9) {n", 11 array (9 ){


Then, we cancel the comments of the open call and continue strace. we will find that the query method is still called twice in a row, but after the first query call, the api will not immediately close the tcp connection, instead, it will continue to be used for the second query call.

611 fcntl64 (3, F_SETFL, O_RDONLY | O_NONBLOCK) = 0
612 connect (3, {sa_family = AF_INET, sin_port = htons (9312), sin_addr = inet_addr ("192.168.1.108")}, 16) =-1 EINPROGRESS (Operation now in progress)
613 select (4, NULL, [3], NULL, {60, 0}) = 1 (out [3], left {59,999 996 })
614 fcntl64 (3, F_SETFL, O_RDONLY) = 0
615 send (3, "1", 4, MSG_NOSIGNAL) = 4
616 recv (3, "1", 4, 0) = 4
617 send (3, "441", 12, MSG_NOSIGNAL) = 12
618 select (4, [3], NULL, [3], {0, 0}) = 0 (Timeout)
619 send (3, "1312241", 16, MSG_NOSIGNAL) = 16
620 send (3, "246 abxx"..., 140, MSG_NOSIGNAL) = 140
621 recv (3, "131" ', 8, 0) = 8
622 recv (3, "25title4text2"..., 96, 0) = 96
623 write (1, "array (9) {n", 11 array (9 ){
624) = 11

....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.