limit = $limit; $this->clock = $clock ?? 'microtime'; $this->started = $this->now(); } /** * A budget for the current request: the fixed wall-clock number, capped by * PHP's time limit only on Windows. * * @param callable|null $clock See __construct(). * @return self */ public static function forRequest( ?callable $clock = null ): self { return new self( self::limitFor( (int) ini_get( 'max_execution_time' ), 'Windows' === PHP_OS_FAMILY ), $clock ); } /** * @param int $max_execution_time PHP's `max_execution_time`; 0 or less means no limit. * @param bool $counts_wall_time Whether that limit counts wall time (Windows) rather than CPU time (Linux). * @return float Seconds. */ public static function limitFor( int $max_execution_time, bool $counts_wall_time ): float { $limit = (float) self::WALL_SECONDS; if ( $counts_wall_time && $max_execution_time > 0 ) { $limit = min( $limit, $max_execution_time * self::WALL_TIME_LIMIT_SHARE ); } return $limit; } /** * Whether the next product may start. The first one always may; a later * one only when the time so far plus one more product at the pace of the * ones before it stays within the limit. The time so far counts from when * the loop opened the budget, so the request stays inside its limit; the * pace counts from when the first product started, so loading and checking * products before it is not taken for generating time. A refused product * is remembered. * * @param int $product_id Product about to start. * @return bool */ public function admit( int $product_id ): bool { $now = $this->now(); if ( $this->attempted > 0 ) { $elapsed = max( 0.0, $now - $this->started ); $pace = max( 0.0, $now - (float) $this->paced_from ) / $this->attempted; if ( $elapsed + $pace > $this->limit ) { $this->skipped[] = $product_id; return false; } } else { $this->paced_from = $now; } ++$this->attempted; return true; } /** * Write the one warning of a cut request. Counts only, no product content. * * @param string $generator Generator slug, such as `title`. * @return void */ public function report( string $generator ): void { if ( array() === $this->skipped ) { return; } Logger::warn( 'generation', 'A generate request stopped at its time budget', array( 'type' => $generator, 'attempted' => $this->attempted, 'not_attempted' => count( $this->skipped ), 'limit_seconds' => $this->limit, ) ); } /** * @param int $product_id Product the budget skipped. * @return array{product_id:int,not_attempted:bool} */ public static function notAttemptedRow( int $product_id ): array { return array( 'product_id' => $product_id, self::NOT_ATTEMPTED => true, ); } /** * @param int $product_id Product the image belongs to. * @param int $attachment_id Image the budget or the cap skipped. * @return array{product_id:int,attachment_id:int,not_attempted:bool} */ public static function notAttemptedImageRow( int $product_id, int $attachment_id ): array { return array( 'product_id' => $product_id, 'attachment_id' => $attachment_id, self::NOT_ATTEMPTED => true, ); } /** * Take the markers out of a service's rows. Only a service that opts into * image markers produces them; the product ids returned here are the * product markers alone. * * @param array $rows A service's rows, markers included. * @return array{0:array,1:int[]} The rows without markers, and the skipped product ids. */ public static function separate( array $rows ): array { $separated = self::separateWithImages( $rows ); return array( $separated[0], $separated[1] ); } /** * separate() that also reports the image markers. * * @param array $rows A service's rows, markers included. * @return array{0:array,1:int[],2:array} The rows without markers, the skipped product ids, and the skipped attachment ids by product id. */ public static function separateWithImages( array $rows ): array { $kept = array(); $skipped = array(); $images = array(); foreach ( $rows as $row ) { if ( is_array( $row ) && true === ( $row[ self::NOT_ATTEMPTED ] ?? null ) ) { $product_id = (int) $row['product_id']; if ( isset( $row['attachment_id'] ) ) { $images[ $product_id ][] = (int) $row['attachment_id']; } else { $skipped[] = $product_id; } continue; } $kept[] = $row; } return array( $kept, $skipped, $images ); } private function now(): float { return (float) call_user_func( $this->clock, true ); } }