Apache/PHP server does not respond while making a SOAP request

Asked

Viewed 1,991 times

3

I am working on a PHP application that consumes a third-party Web Service through SOAP requests. But something that’s getting in the way is that while one page is making a SOAP request no other page responds. There is a way to make the request recovering possible exceptions but without locking the application?

2 answers

14


You’re probably using it session_start, the PHP locks the file (LOCK) session, even if you change page the session is still the same and the session file too.

Note: Other visitors to the page are not affected by "this" (unless you share the session) as each visitor has their own session file.

How this occurs

  • You requested the page http://localhost/sync-webservice.php, she uses session and her response time is an average of 10 seconds.

  • Before this 10 seconds (average time) finish the file used by the session will be locked.

  • When accessing http://localhost/profile.php for example (which also uses the session), the function session_start will wait for the session file to be unlocked.

  • When the page http://localhost/sync-webservice.php ends the session execution is released for other requests to access the session file then the page http://localhost/profile.php will detect that the session is unlocked and will go.

What are session files?

Session files are used to save the data of a specific session, they are generated at the time of use of the function session_start(); and are usually saved in the folder /tmp as /tmp/sess_2ognrumtg8pri1prd098r2vij0 for example.

In the session file contents sess_2ognrumtg8pri1prd098r2vij0 for example, it contains the data that will be used in the variable $_SESSION, an example of content:

nome-da-sessao|a:5:{...}

Each time we start a session, if there is no file, it is generated, if there is it reads, passes the data from the file to the variable $_SESSION and locks the file for recording, the moment the page completes the response to the request the file is released so that other requests can use it.

Solution

There are several methods we can use, but I will make reference to the simplest one to use session_write_close. Note that we should only use this when we will no longer set any variable $_SESSION[...] = ...; or use other functions session_*.

Example of use:

  • sync-webservice.php

    <?php
    session_start();
    
    //Fazemos todas gravações necessárias
    $_SESSION['A'] = 1;
    
    //Liberamos a sessão do travamento de escrita
    session_write_close();
    
    /*
     A partir daqui não poderemos mais gravar nada na
     sessão, porém a leitura da variável `$_SESSION`
     ainda é acessível, pois ela é uma *super global*
     e já está setado os valores neste ponto
    */
    
    //Simulamos páginas "lentas", 15 segundos de espera (delay)
    sleep(15);
    
  • profile.php

    <?php
    session_start();
    
    //Fazemos todas gravações necessárias
    $_SESSION['B'] = 1;
    
    print_r($_SESSION);
    

This way the other page will not need to wait for the page Sync-webservice.php finish processing so that they can complete their execution.

0

You can try thread, however, thread in PHP is difficult, and depends on other libs and the hosting. I suggest creating a subdomain just to make the Soap requests.
I believe this will be enough not to block other pages.
If it continues, try a cheaper server only for external Soap requests.

  • Jonathan the application is on a dedicated Windows Server machine, it is not hosting. As every application is structured around these requests it would not be feasible to migrate "only" the requests to another application.

  • Try this library, it’s Curl non-blocking as you need it. I’ve never used it, but I think it’s worth a try: https://github.com/stil/curl-easy

Browser other questions tagged

You are not signed in. Login or sign up in order to post.