Commit cdaccebf authored by mikeytown2's avatar mikeytown2

Issue #1530636 by mikeytown2: Exit httprl_send_request() once all fwrites have...

Issue #1530636 by mikeytown2: Exit httprl_send_request() once all fwrites have been done. Do all freads at a later time in the script.
parent 97acb7bf
......@@ -365,7 +365,7 @@ instead of returning a value.
**More Advanced HTTP Operations**
Hit 4 different URLs, Using at least 2 that has a status code of 200 and
Hit 5 different URLs, Using at least 2 that has a status code of 200 and
erroring out the others that didn't return fast. Using the Range header so only
the first and last 128 bytes are returned.
......@@ -396,8 +396,8 @@ the first and last 128 bytes are returned.
function need_two_good_results($id, &$responses) {
static $counter = 0;
foreach ($responses as $id => &$result) {
// Skip if we got a 200.
if ($result->code == 200) {
// Skip if we got a 200 or 206.
if ($result->code == 200 || $result->code == 206) {
$counter += 1;
continue;
}
......@@ -451,6 +451,59 @@ Send 2 files in one field via a POST request.
?>
Send out 8 requests. In this example we are going to stall the call to fread()
`'stall_fread' => TRUE,`. By doing this we can issue a bunch of requests, do
some other stuff and then get the results later on in the PHP process. A useful
example would be for ESI emulation. Issue a bunch of requests at the start of
the request, generate the main content and then add in the ESI-ed components at
the end of the request.
<?php
// Queue up the request.
$urls = array(
'http://www.google.com/',
'http://www.bing.com/',
'http://www.yahoo.com/',
'http://duckduckgo.com/',
'http://drupal.org/',
'http://drupal.org/robots.txt',
'http://drupal.org/CHANGELOG.txt',
'http://drupal.org/MAINTAINERS.txt',
);
$options = array(
// Do fread in a second request.
'stall_fread' => TRUE,
'headers' => array(
// Only grab the last 128 bytes of the request.
'Range' => 'bytes=-128',
// Accept Compression.
'Accept-Encoding' => 'gzip, deflate',
),
// Increase the read chunk size to 512KB.
'chunk_size_read' => 524288,
// Increase domain_connections to 4 (drupal.org).
'domain_connections' => 4,
// If we can't connect quick (0.5 seconds), bail out.
'connect_timeout' => 0.5,
'dns_timeout' => 0.5,
);
httprl_request($urls, $options);
// Execute request.
echo round(timer_read('page')/1000, 3) . " - Time taken to get requests ready.<br> \n";
$request = httprl_send_request();
echo strtoupper(var_export($request, TRUE)) . " - Output from first httprl_send_request() call<br> \n";
echo round(timer_read('page')/1000, 3) . " - Time taken to send out all fwrites().<br> \n";
sleep(2);
echo round(timer_read('page')/1000, 3) . " - Time taken for sleep(2).<br> \n";
$request = httprl_send_request();
echo round(timer_read('page')/1000, 3) . " - Time taken for all freads().<br> \n";
echo "Output from second httprl_send_request() below:<br> \n<br> \n";
echo httprl_pr($request);
?>
**Threading Examples**
Use 2 threads to load up 4 different nodes.
......
......@@ -898,6 +898,9 @@ function httprl_establish_stream_connection(&$result) {
* $output variables inside of httprl_send_request. Defined function
* should have the following parameters:
* ($id, &$responses).
* - stall_fread: TRUE or FALSE. If true once all fwrites have been done
* httprl_send_request() will return. You will need to call
* httprl_send_request() a second time to read the responses back.
*
* @return array
* Array where key is the URL and the value is the return value from
......@@ -1012,6 +1015,8 @@ function httprl_request($urls, $options = array()) {
function httprl_send_request($results = NULL) {
static $responses = array();
static $counter = 0;
static $output = array();
static $static_stall_freads = FALSE;
if (!is_null($results)) {
// Put the connection information into the responses array.
......@@ -1035,11 +1040,10 @@ function httprl_send_request($results = NULL) {
$t = 'httprl_pr';
}
// Create output array.
$output = array();
// Remove errors from responses array and set the global timeout.
$global_timeout = 1;
$global_connection_limit = 1;
$stall_freads = FALSE;
foreach ($responses as $id => &$result) {
if (!empty($result->error)) {
$result->status = 'Connection not made.';
......@@ -1057,7 +1061,14 @@ function httprl_send_request($results = NULL) {
$domain_connection_limit[$result->options['headers']['Host']] = max($domain_connection_limit[$result->options['headers']['Host']], $result->options['domain_connections']);
}
// Set global timeout.
$global_timeout = max($global_timeout, $result->options['global_timeout']);
// Issue fwrite, return. Run fread later on in the script.
if ($result->options['stall_fread'] && !$static_stall_freads) {
$static_stall_freads = TRUE;
$stall_freads = TRUE;
}
}
// Record start time.
......@@ -1428,6 +1439,9 @@ function httprl_send_request($results = NULL) {
}
}
}
elseif ($stall_freads) {
return;
}
}
if (!$rw_done) {
// Wait 5ms for data buffers.
......@@ -1435,11 +1449,16 @@ function httprl_send_request($results = NULL) {
}
}
// Copy output.
$return = $output;
// Free memory/reset static variables.
$responses = array();
$counter = 0;
$output = array();
$static_stall_freads = FALSE;
return $output;
return $return;
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment