← Back
Editing: class-guzzle-client-factory.php
<?php namespace Smush\Core\Smush; use Smush_Vendor\GuzzleHttp\Client; /** * Builds and remembers the Guzzle clients this plugin sends images with. * * A Guzzle client owns its handler stack and, behind it, the cURL handle pool and the libcurl * connection cache. A client that is thrown away after one request takes all of that with it, so the * next request to the same host pays a fresh DNS lookup, TCP handshake and TLS handshake. Smush * downloads one file per size, so without this the handshakes scale with the number of sizes rather * than with the number of hosts it talks to. * * Each Smusher owns its own factory, so connections live for one batch and close with it. A * process-wide cache kept them open across a whole bulk run, and on small hosts the API side drops * idle connections first: reusing one then fails ("Connection died") and costs more handshakes on * the retries than reuse ever saved. * * Clients are remembered against the *resolved* options rather than the URL. `verify` and `proxy` are * resolved per URL, so two requests may only share a client when they resolve to identical options — * on a site with WP_PROXY_BYPASS_HOSTS the image host may need a different proxy than the API host. * The options are always resolved, even on a cache hit, so filters see every request as before. */ class Guzzle_Client_Factory { /** * How many clients to keep before the oldest one is dropped. Deliberately small: filters that * make the resolved options differ per URL must not grow this once per image in a bulk run. */ const MAX_CLIENTS = 4; /** * @var Guzzle_Client_Config */ private $client_config; /** * @var Client[] */ private $clients = array(); public function __construct( $client_config ) { $this->client_config = $client_config; } /** * The client for these options, building it only when no client matches them yet. * * @param string $url URL the client will talk to. * @param int $timeout Request timeout in seconds. * @param int $connect_timeout Connection timeout in seconds. * * @return Client */ public function get( $url, $timeout, $connect_timeout ) { $config = $this->client_config->get( $url, $timeout, $connect_timeout ); $key = $this->get_cache_key( $config ); if ( is_null( $key ) ) { // Options that cannot be serialized (a closure added by a filter, say) have no stable // key, so this request gets its own client rather than one built for other options. return new Client( $config ); } if ( ! isset( $this->clients[ $key ] ) ) { $this->make_room(); $this->clients[ $key ] = new Client( $config ); } return $this->clients[ $key ]; } /** * Drop every remembered client. * * Tests that assert on which client a request was built with have to start from an empty one. * * @return void */ public function reset() { $this->clients = array(); } /** * @param array $config Resolved Guzzle options. * * @return string|null Cache key, or null when these options cannot be keyed. */ private function get_cache_key( $config ) { try { return md5( serialize( $config ) ); } catch ( \Exception $exception ) { return null; } } /** * Drop the oldest client when the limit has been reached. Insertion ordered, so the first * entry is the oldest one. * * @return void */ private function make_room() { if ( count( $this->clients ) < self::MAX_CLIENTS ) { return; } array_shift( $this->clients ); } }
Save File
Cancel