New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request getting slow after adding retry middleware #2042

Open
Misbah-Ahmad opened this Issue Apr 7, 2018 · 4 comments

Comments

Projects
None yet
5 participants
@Misbah-Ahmad
Copy link

Misbah-Ahmad commented Apr 7, 2018

I'm sending asynchronous request using using guzzlehttp in my laravel app. The requests are working perfectly and returning response from my local api endpoints within less than a second.
But, while using Retry middleware it slows down the requests and take around 20 seconds to return a response. It's occurring even though all API endpoints are live responding perfectly.

Request Code:
trait SearchesFlights
{

	use ParsesJason, RetriesRequest;

	private $endPoints = [
					'v1'=> '127.0.0.1:5354', //post request,  body.date
					'v2' => '127.0.0.1:5939', //post request,  body.date
					'v3' => '127.0.0.1:4949', //post request,  body.date
					'v4' => '127.0.0.1:5051', //get request, no body
					'v5' => '127.0.0.1:5052', //get request, no body
				];



	private $postDate = '02/04/2018';


	public function searchByDate()
	{

        $client = new Client(array('handler' => $this->getRetryHandler()));

		//$client = new Client();


		$promises = $this->getPromiseArray($client);

		$promise = Promise\any($promises)->then(
		    
		    function (PsrResponse $response){

		    	
			    return $this->getAll(json_decode($response->getBody()));


		    },
		    
		    function ($reason) {

		        return $reason;

		    }
		
		);

		$result = $promise->wait();

		return $result;

	}
	



	/*Returns Promise Array with post requests*/

	private function getPromiseArray($client)
	{
		
		$options = ['form_params' => ['date' => $this->postDate]];

		return $promises = [
			'server1' => $client->requestAsync('POST', $this->endPoints['v1'], $options),
			'server2' => $client->requestAsync('POST', $this->endPoints['v2'], $options),
			'server3' => $client->requestAsync('POST', $this->endPoints['v3'], $options),
		];



	}



}

Retry Middleware Code:

trait RetriesRequest {

 
    public function getRetryHandler()
    {
        $handlerStack = HandlerStack::create(new CurlHandler());
        
        $handlerStack->push(Middleware::retry($this->retryDecider(), $this->retryDelay()));

        return $handlerStack;
     
    }

    public function retryDecider()
    {

        return function (
            $retries,
            Request $request,
            Response $response = null,
            RequestException $exception = null
        ) 

        {
            
            
            // Limit the number of retries to 5
            if ($retries >= 5)
            {
            
                return false;
            
            }


            // Retry connection exceptions
            if ($exception instanceof ConnectException)
            {
            
                return true;
            
            }


            if($exception instanceof AggregateException)
            {

                return true;

            }


            if ($response)
            {
                // Retry on server errors
                if ($response->getStatusCode() >= 500 )
                {
                
                    return true;
                
                }
            
            }

            return false;
        };

    }



    /**
     * delay 1s 2s 3s 4s 5s
     *
     * @return Closure
     */

    public function retryDelay()
    {
        
        return function ($numberOfRetries) 
        {
            return 2000;
            // return 1000 * $numberOfRetries;
        };

    }

}
@martendej

This comment has been minimized.

Copy link

martendej commented Sep 18, 2018

I have exactly the same issue, did you resolve this by any chance?

@sagikazarmark

This comment has been minimized.

Copy link
Contributor

sagikazarmark commented Sep 18, 2018

@Misbah-Ahmad can you please compact the code to a single PHP script file?

@Fantus

This comment has been minimized.

Copy link

Fantus commented Nov 8, 2018

I head the same issue, exchanging:
$handlerStack = HandlerStack::create(new CurlHandler());
with:
$handlerStack = HandlerStack::create(new CurlMultiHandler());

solved it

@EtoileFilante

This comment has been minimized.

Copy link

EtoileFilante commented Dec 13, 2018

Thanks <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment