Skip to content
This repository was archived by the owner on Mar 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1 from Getsidecar/backoff
Browse files Browse the repository at this point in the history
Adds an exponential backoff to opening a connection to the R server
  • Loading branch information
chriscannon authored Aug 21, 2017
2 parents 9857300 + 65e92e4 commit 7775117
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 6 deletions.
30 changes: 24 additions & 6 deletions Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Rserve client for PHP
* Supports Rserve protocol 0103 only (used by Rserve 0.5 and higher)
* $Revision$
* @author Clément TURBELIN
* @author Clément TURBELIN
* Developped using code from Simple Rserve client for PHP by Simon Urbanek Licensed under GPL v2 or at your option v3
* $Id$
*/
Expand All @@ -12,7 +12,7 @@

/**
* Handle Connection and communicating with Rserve instance (QAP1 protocol)
* @author Clément Turbelin
* @author Clément Turbelin
*
*/
class Rserve_Connection {
Expand Down Expand Up @@ -92,6 +92,9 @@ class Rserve_Connection {

private $ascync;

private $backoffOptions;
private $backoffMaxAttempts = 3;

/**
* initialization of the library
*/
Expand Down Expand Up @@ -141,6 +144,8 @@ public function __construct($host='127.0.0.1', $port = 6311, $params=array()) {
}
$this->debug = isset($params['debug']) ? (bool)$params['debug'] : FALSE;
$this->async = isset($params['async']) ? (bool)$params['async'] : FALSE;
$this->backoffOptions = \Yriveiro\Backoff\Backoff::getDefaultOptions();
$this->backoffOptions['maxAttempts'] = $this->backoffMaxAttempts;
$this->openSocket($session);
}

Expand All @@ -159,9 +164,22 @@ private function openSocket($session_key = NULL) {
}
//socket_set_option($socket, SOL_TCP, SO_DEBUG,2);
$ok = socket_connect($socket, $this->host, $this->port);
if( !$ok ) {
throw new Rserve_Exception('Unable to connect ['.socket_strerror(socket_last_error()).']');

if (!$ok) {
$backoff = new \Yriveiro\Backoff\Backoff($this->backoffOptions);
$attempt = 1;
try {
while (!$ok) {
$time = $backoff->fullJitter($attempt);
$attempt++;
usleep($time);
$ok = socket_connect($socket, $this->host, $this->port);
}
} catch (\Yriveiro\Backoff\BackoffException $e) {
throw new Rserve_Exception('Unable to connect ['.socket_strerror(socket_last_error()).']');
}
}

$this->socket = $socket;
if( !is_null($session_key) ) {
// Try to resume session
Expand Down Expand Up @@ -512,7 +530,7 @@ public function getErrorMessage($code) {

/**
* R Session wrapper
* @author Clément Turbelin
* @author Clément Turbelin
*
*/
class Rserve_Session {
Expand Down Expand Up @@ -546,7 +564,7 @@ public function __toString() {

/**
* RServe Exception
* @author Clément Turbelin
* @author Clément Turbelin
*
*/
class Rserve_Exception extends Exception {
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
"./",
"REXP/"
]
},
"require": {
"yriveiro/php-backoff": "^1.1"
}
}
64 changes: 64 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7775117

Please sign in to comment.