| @@ -10,8 +10,18 @@ | ||
| 10 | 10 | */ |
| 11 | 11 | class WPConsumer extends WPMedia_ConsumerStrategies_AbstractConsumer { |
| 12 | 12 | |
| 13 | 13 | /** |
| 14 | + * Number of seconds to allow the request to execute. | |
| 15 | + * | |
| 16 | + * Not configurable: analytics must never delay a response. Also the lowest value | |
| 17 | + * WordPress honours, as it clamps the cURL timeout to a minimum of 1 second. | |
| 18 | + * | |
| 19 | + * @var int | |
| 20 | + */ | |
| 21 | + const REQUEST_TIMEOUT = 1; | |
| 22 | + | |
| 23 | + /** | |
| 14 | 24 | * The host to connect to (e.g. api.mixpanel.com) |
| 15 | 25 | * |
| 16 | 26 | * @var string |
| 17 | 27 | */ |
| @@ -24,15 +34,8 @@ | ||
| 24 | 34 | */ |
| 25 | 35 | protected $endpoint; |
| 26 | 36 | |
| 27 | 37 | /** |
| 28 | - * The maximum number of seconds to allow the call to execute. Default is 30 seconds. | |
| 29 | - * | |
| 30 | - * @var int | |
| 31 | - */ | |
| 32 | - protected $timeout; | |
| 33 | - | |
| 34 | - /** | |
| 35 | 38 | * The protocol to use for the cURL connection |
| 36 | 39 | * |
| 37 | 40 | * @var string |
| 38 | 41 | */ |
| @@ -40,9 +43,11 @@ | ||
| 40 | 43 | |
| 41 | 44 | /** |
| 42 | 45 | * Creates a new WPConsumer and assigns properties from the $options array |
| 43 | 46 | * |
| 44 | - * @param array{host:string, endpoint:string, timeout?: int, use_ssl?: bool} $options Options for the consumer. | |
| 47 | + * The request timeout and blocking behaviour are fixed, and cannot be set here. | |
| 48 | + * | |
| 49 | + * @param array{host: string, endpoint: string, use_ssl?: bool} $options Options for the consumer. | |
| 45 | 50 | */ |
| 46 | 51 | public function __construct( $options ) { |
| 47 | 52 | parent::__construct( $options ); |
| 48 | 53 | |
| @@ -47,9 +52,8 @@ | ||
| 47 | 52 | parent::__construct( $options ); |
| 48 | 53 | |
| 49 | 54 | $this->host = $options['host']; |
| 50 | 55 | $this->endpoint = $options['endpoint']; |
| 51 | - $this->timeout = isset( $options['timeout'] ) ? $options['timeout'] : 30; | |
| 52 | 56 | $this->protocol = isset( $options['use_ssl'] ) && ( true === $options['use_ssl'] ) ? 'https' : 'http'; |
| 53 | 57 | } |
| 54 | 58 | |
| 55 | 59 | /** |
| @@ -66,20 +70,29 @@ | ||
| 66 | 70 | |
| 67 | 71 | $url = $this->protocol . '://' . $this->host . $this->endpoint; |
| 68 | 72 | $data = 'data=' . $this->_encode( $batch ); |
| 69 | 73 | |
| 74 | + // Non-blocking still waits up to the timeout, it only discards the response: | |
| 75 | + // the timeout is what bounds the cost of a degraded endpoint. | |
| 70 | 76 | $response = wp_remote_post( |
| 71 | 77 | $url, |
| 72 | 78 | [ |
| 73 | - 'timeout' => $this->timeout, | |
| 74 | - 'body' => $data, | |
| 79 | + 'timeout' => self::REQUEST_TIMEOUT, | |
| 80 | + 'blocking' => false, | |
| 81 | + 'body' => $data, | |
| 75 | 82 | ] |
| 76 | 83 | ); |
| 77 | 84 | |
| 78 | 85 | if ( is_wp_error( $response ) ) { |
| 79 | 86 | $this->_handleError( $response->get_error_code(), $response->get_error_message() ); |
| 80 | - return false; | |
| 81 | 87 | } |
| 82 | 88 | |
| 89 | + /* | |
| 90 | + * Report the batch as consumed even on failure. Returning false would make the | |
| 91 | + * producer re-queue it and retry the flush up to 10 times at shutdown, once per | |
| 92 | + * producer, multiplying the cost of an unreachable endpoint. | |
| 93 | + * | |
| 94 | + * @see \Imagify_WPMedia_Producers_MixpanelBaseProducer::__destruct() | |
| 95 | + */ | |
| 83 | 96 | return true; |
| 84 | 97 | } |
| 85 | 98 | } |