false, 'remaining' => 0, 'reset_in' => 0 ); } $bucket_key = 'aipt_rate_limit_' . $scope . '_' . $user_id; return AtomicRateLimiter::checkAndIncrement( $bucket_key, $max_requests, $window_seconds ); } /** * @param array{reset_in:int} $rate_check checkRateLimitStatic()'s result. * @return WP_Error */ protected static function rateLimitErrorStatic( array $rate_check ): WP_Error { return new WP_Error( 'rate_limit_exceeded', sprintf( /* translators: %d: seconds to wait before retrying. */ __( 'Rate limit exceeded. Please wait %d seconds before trying again.', 'ai-product-tools' ), $rate_check['reset_in'] ), array( 'status' => 429, 'retry_after' => $rate_check['reset_in'] ) ); } /** * Attempt to claim $client_request_id for this request, atomically with * the check: at most one request sharing this id across its owner's * whole lifetime (claim -> {@see dedupeStore()} or * {@see dedupeRelease()}) ever reaches the AI provider. * * @param int $user_id Acting user id. * @param string $client_request_id Client-supplied idempotency key; '' always bypasses dedup. * @return array|WP_Error|null `array` = a prior identical request's * cached success envelope to return as-is. `WP_Error` (409) = an * identical request is already in flight. `null` = this call now * owns the id: call {@see dedupeStore()} on success or * {@see dedupeRelease()} on any other outcome. */ protected static function dedupeClaim( int $user_id, string $client_request_id ) { if ( '' === $client_request_id ) { return null; } return RequestClaimStore::claim( self::dedupeKey( $user_id, $client_request_id ) ); } /** * Record $client_request_id's successful result for a short cache * window, releasing the claim {@see dedupeClaim()} granted. * * @param int $user_id Acting user id. * @param string $client_request_id Client-supplied idempotency key. * @param array $data Success envelope data to cache. * @return void */ protected static function dedupeStore( int $user_id, string $client_request_id, array $data ): void { if ( '' === $client_request_id ) { return; } RequestClaimStore::complete( self::dedupeKey( $user_id, $client_request_id ), $data ); } /** * Release a claim {@see dedupeClaim()} granted without caching * anything, so a legitimate retry of the same id is never stuck behind * this attempt. Call whenever an owned claim does not end in a * cacheable success (the rate limit denied the call, or the work itself * failed). * * @param int $user_id Acting user id. * @param string $client_request_id Client-supplied idempotency key. * @return void */ protected static function dedupeRelease( int $user_id, string $client_request_id ): void { if ( '' === $client_request_id ) { return; } RequestClaimStore::release( self::dedupeKey( $user_id, $client_request_id ) ); } /** * @param int $user_id Acting user id. * @param string $client_request_id Client-supplied idempotency key. * @return string */ private static function dedupeKey( int $user_id, string $client_request_id ): string { return 'aipt_cs_dedupe_' . $user_id . '_' . md5( $client_request_id ); } }