* @file
*/
+use Psr\Log\LoggerAwareInterface;
+use Psr\Log\LoggerInterface;
+use Psr\Log\NullLogger;
+
/**
* Class to handle concurrent HTTP requests
*
* @author Aaron Schulz
* @since 1.23
*/
-class MultiHttpClient {
+class MultiHttpClient implements LoggerAwareInterface {
/** @var resource */
protected $multiHandle = null; // curl_multi handle
/** @var string|null SSL certificates path */
protected $proxy;
/** @var string */
protected $userAgent = 'wikimedia/multi-http-client v1.0';
+ /** @var LoggerInterface */
+ protected $logger;
/**
* @param array $options
}
}
static $opts = [
- 'connTimeout', 'reqTimeout', 'usePipelining', 'maxConnsPerHost', 'proxy', 'userAgent'
+ 'connTimeout', 'reqTimeout', 'usePipelining', 'maxConnsPerHost',
+ 'proxy', 'userAgent', 'logger'
];
foreach ( $opts as $key ) {
if ( isset( $options[$key] ) ) {
$this->$key = $options[$key];
}
}
+ if ( $this->logger === null ) {
+ $this->logger = new NullLogger;
+ }
}
/**
} elseif ( !isset( $req['url'] ) ) {
throw new Exception( "Request has no 'url' field set." );
}
+ $this->logger->debug( "{$req['method']}: {$req['url']}" );
$req['query'] = isset( $req['query'] ) ? $req['query'] : [];
$headers = []; // normalized headers
if ( isset( $req['headers'] ) ) {
// Wait (if possible) for available work...
if ( $active > 0 && $mrc == CURLM_OK ) {
if ( curl_multi_select( $chm, 10 ) == -1 ) {
- // PHP bug 63411; http://curl.haxx.se/libcurl/c/curl_multi_fdset.html
+ // PHP bug 63411; https://curl.haxx.se/libcurl/c/curl_multi_fdset.html
usleep( 5000 ); // 5ms
}
}
if ( function_exists( 'curl_strerror' ) ) {
$req['response']['error'] .= " " . curl_strerror( $errno );
}
+ $this->logger->warning( "Error fetching URL \"{$req['url']}\": " .
+ $req['response']['error'] );
}
} else {
$req['response']['error'] = "(curl error: no status set)";
return $this->multiHandle;
}
+ /**
+ * Register a logger
+ *
+ * @param LoggerInterface
+ */
+ public function setLogger( LoggerInterface $logger ) {
+ $this->logger = $logger;
+ }
+
function __destruct() {
if ( $this->multiHandle ) {
curl_multi_close( $this->multiHandle );