diff --git a/httprl.module b/httprl.module index 9c550ed..44e02f4 100644 --- a/httprl.module +++ b/httprl.module @@ -139,6 +139,10 @@ function httprl_build_url_self($path = '', $detect_schema = FALSE) { * - version: HTTP Version 1.0 or 1.1. Default is 1.0 for a good reason. * - referrer: TRUE - send current page; FALSE - do not send current * page. Default is FALSE. + * - domain_connections: Maximum number of simultaneous connections to a given + * domain name. Default is 8. + * - global_connections: Maximum number of simultaneous connections that can + * be open on the server. Default is 128. * @return bool * return value from httprl_send_request(). */ @@ -179,6 +183,8 @@ function httprl_request($url, $options = array()) { 'blocking' => TRUE, 'version' => 1.0, 'referrer' => FALSE, + 'domain_connections' => 8, + 'global_connections' => 128, ); // stream_socket_client() requires timeout to be a float. @@ -342,6 +348,10 @@ function httprl_send_request($fp = NULL, $url = '', $request = '', $options = '' static $timeout = 0; static $counter = 0; static $stream_write_count = 0; + static $global_connection_count = 0; + static $global_connection_limit = 0; + static $domain_connection_count = array(); + static $domain_connection_limit = array(); // If given a file pointer, store data in a static and exit. if (!is_null($fp)) { @@ -360,6 +370,8 @@ function httprl_send_request($fp = NULL, $url = '', $request = '', $options = '' $timeout = max($options['timeout'], $timeout); $stream_write_count++; $counter++; + $global_connection_limit = max(1, $options['global_connections']); + $domain_connection_limit[$options['headers']['Host']] = max(1, $options['domain_connections']); return TRUE; } // Connection was never made. @@ -413,9 +425,32 @@ function httprl_send_request($fp = NULL, $url = '', $request = '', $options = '' break; } + // Set connection limits. + $this_run = array(); + $global_connection_count = 0; + $domain_connection_count = array(); + foreach ($streams as $id => $fp) { + // Get the host name. + $host = $responses[$id]->options['headers']['Host']; + + // Count up the number of connections. + $global_connection_count++; + if (empty($domain_connection_count[$host])) { + $domain_connection_count[$host] = 1; + } + else { + $domain_connection_count[$host]++; + } + + // If the condtions are correct, let the stream be ran in this loop. + if ($global_connection_limit >= $global_connection_count && $domain_connection_limit[$host] >= $domain_connection_count[$host]) { + $this_run[$id] = $fp; + } + } + $rw_done = FALSE; // Set the read and write vars to the streams var. - $read = $write = $streams; + $read = $write = $this_run; $except = array(); // Do some voodoo and open all streams at once.