diff --git a/httprl.module b/httprl.module index a279e01..02a3345 100644 --- a/httprl.module +++ b/httprl.module @@ -6,6 +6,16 @@ */ /** + * Default maximum number of seconds a single request call may take. + */ +define('HTTPRL_TIMEOUT', 30.0); + +/** + * Default maximum number of seconds a function call may take. + */ +define('HTTPRL_GLOBAL_TIMEOUT', 120.0); + +/** * Error code indicating that the request made by httprl_request() exceeded * the maximum allowed redirects without reaching the final target. */ @@ -268,14 +278,14 @@ function httprl_set_default_options(&$options) { 'method' => 'GET', 'data' => NULL, 'max_redirects' => 3, - 'timeout' => 30.0, + 'timeout' => HTTPRL_TIMEOUT, 'context' => NULL, 'blocking' => TRUE, 'version' => '1.0', 'referrer' => FALSE, 'domain_connections' => 8, 'global_connections' => 128, - 'global_timeout' => 120.0, + 'global_timeout' => HTTPRL_GLOBAL_TIMEOUT, 'chunk_size_read' => 32768, 'chunk_size_write' => 1024, 'async_connect' => TRUE, @@ -560,9 +570,8 @@ function httprl_build_request_string($uri, $options) { * - global_connections: Maximum number of simultaneous connections that can * be open on the server. Default is 128. * - global_timeout: A float representing the maximum number of seconds the - * function call may take. The default is 30 seconds. If a timeout occurs, - * the error code is set to the HTTPRL_FUNCTION_TIMEOUT constant. Default is - * 120 seconds. + * function call may take. If a timeout occurs,the error code is set to the + * HTTPRL_FUNCTION_TIMEOUT constant. Default is 120 seconds. * - chunk_size_write: max size of what will be written in fwrite(). * - chunk_size_read: max size of what will be read from fread(). * - async_connect: default is TRUE. FALSE may give more info on errors but is @@ -1503,12 +1512,23 @@ function httprl_queue_background_callback(&$args, &$result = NULL) { $callback_options['function'] = ''; } + // Get the maximum amount of time this could take. + $times = array(HTTPRL_TIMEOUT, HTTPRL_GLOBAL_TIMEOUT); + if (isset($callback_options['options']['timeout'])) { + $times[] = $callback_options['options']['timeout']; + } + if (isset($callback_options['options']['global_timeout'])) { + $times[] = $callback_options['options']['global_timeout']; + } + // Acquire lock for this run. $locked = FALSE; - $counter = 0; - while (!$locked && $counter < 20) { + $lock_counter = 0; + while (!$locked && $lock_counter < 20) { $id = 'httprl_' . hash('sha512', mt_rand() . time()); - $locked = lock_acquire($id); + // Set lock to maximum amount of time. + $locked = lock_acquire($id, max($times)); + $lock_counter++; } // Make sure lock exists after this process is dead. @@ -1562,7 +1582,7 @@ function httprl_queue_background_callback(&$args, &$result = NULL) { $options += $callback_options['options']; } // Send Request. - httprl_request($url, $options); + return httprl_request($url, $options); } /**