SoapClient: concurrent asynchronous requests, reconnect, timeout processing

    Dklab_SoapClient is an extended version of the standard SoapClient PHP class, designed for parallel (asynchronous) remote procedure calls in highly loaded projects.

    Using this library, you can, for example, build a page of your site from blocks, as from a constructor. Each block is requested through SOAP separately and independently of the others, while all requests occur in parallel. If one of the blogs did not meet the allotted time (timeout), then it may not be displayed on the page.

    Compared to the built-in PHP SoapClient, additional features are supported:
    • Simultaneous, parallel execution of requests to several remote procedures is a key feature of the library. If the page on your site is assembled from 5 deleted blocks, each of which is generated by 100ms, you can run them in parallel and get the whole page not for 500ms, but for the same 100ms.
    • Reconnect when communication is not possible. Unfortunately, the world is imperfect, and due to accidental packet loss, the first attempt to connect to a SOAP server may result in a timeout. This is especially common when a project is located in several data centers. Dklab_SoapClient allows you to set a timeout at the time the connection is opened (for example, 1 second) and, in case of failure, retry the specified number of times. In practice, this reduces the likelihood of a final failure by a thousand times, because reconnect almost always helps with packet loss.
    • Support for timeout to receive data. If the page is assembled from deleted blocks, then in the case of “freezing” of one of them, the whole page “freezes”. At the same time, the absence of one of the blocks in the presence of the rest is not such a big trouble. You can specify how long Dklab_SoapClient should wait for a response from the remote procedure; if the time is exceeded, a PHP exception occurs, which you can handle as you like without interrupting the loading of the remaining blocks.
    Why SOAP? At first glance, it might seem that this is a very cumbersome protocol that is difficult to apply. However, if you do not delve into the protocol internals, it turns out that using PHP SOAP is very convenient. If WSDL schemes are not needed, the simplest SOAP server is written like this:

    class MyServer
    {
        public function getComplexData ($ some)
        {
            sleep (1); // type, emulate heavy code
            return array ("obj" => (object) array ("prop" => $ some), "some" => "thing");
        }
    }
    $ soapServer = new SoapServer (null, array ('uri' => 'urn: myschema'));
    $ soapServer-> setObject (new MyServer ());
    $ soapServer-> handle ();
    

    And the corresponding SOAP client is like this:

    require_once "Dklab / SoapClient.php";
    $ client = new Dklab_SoapClient (null, array (
        'location' => "http://example.com/server.php",
        'uri' => 'urn: myschema',
        'timeout' => 3,
    ));
    // Requests are executed in parallel, asynchronously. In total - for 1 s.
    $ query1 = $ client-> async-> getComplexData (array ("abc"));
    $ query2 = $ client-> async-> getComplexData (array ("xyz"));
    print_r ($ query1-> getResult ());
    print_r ($ query2-> getResult ());
    

    What is important, when working with SOAP in PHP, the server can return data in an arbitrary format (for example, an array of arrays of objects), and the client can pass variables of any structure to the server in the parameters (for example, the same array of arrays of objects). In this case, all conversions are performed automatically.

    Because Dklab_SoapClient is an extension of the built-in SoapClient, all the standard features of the latter are supported, including:
    • Work with cookies. One procedure may call setcookie (), and another may read the value of this cooike later.
    • Work with PHP sessions. One procedure writes data to the session, the second reads.
    • Support for WSDL schemes and the transfer of complex business objects.
    • Handling exceptions that occur in a remote procedure.
    If suddenly Apache Thrift for your project is “out of the gun on sparrows”, Dklab_SoapClient can serve as a simple and compact alternative. (By the way, parallel requests are not supported in the PHP module for Thrift - at least I did not find them there.) You can

    see the documentation, examples, autotests and download the library here: http://dklab.ru/lib/Dklab_SoapClient/

    PS
    By the way, the development uses Git and the social network GitHub . Who else has not tried them, I recommend doing this.

    Also popular now: