Tất cả dịch vụ Google Ads API đều có chế độ cài đặt mặc định (bao gồm cả thời gian chờ) được giao thức truyền tải sử dụng. Mỗi dịch vụ của một phiên bản Google Ads API nhất định đều có một tệp JSON riêng, trong đó có các chế độ cài đặt mặc định được xác định ở cấp dịch vụ và cấp phương thức. Ví dụ: bạn có thể tìm thấy các tệp cấu hình tài nguyên dịch vụ cho phiên bản Google Ads API mới nhất trên GitHub.
Chế độ cài đặt mặc định phù hợp với hầu hết các trường hợp sử dụng, nhưng có thể có những trường hợp bạn cần ghi đè các chế độ cài đặt đó. Thư viện ứng dụng cho PHP hỗ trợ ghi đè chế độ cài đặt thời gian chờ cho mỗi lệnh gọi (được chỉ định bằng mili giây bằng cách sử dụng tuỳ chọn timeoutMillis) cho cả lệnh gọi một chiều và lệnh gọi truyền trực tuyến từ máy chủ.
Mặc dù bạn có thể định cấu hình giá trị thời gian chờ cao (chẳng hạn như 2 giờ trở lên) trong thư viện ứng dụng, nhưng các proxy mạng cơ bản hoặc máy chủ API vẫn có thể chấm dứt các yêu cầu chạy trong thời gian dài sớm hơn và trả về lỗi trạng thái DEADLINE_EXCEEDED.
Ghi đè thời gian chờ cho lệnh gọi truyền trực tuyến của máy chủ
Phương thức dịch vụ Google Ads API duy nhất sử dụng lệnh gọi truyền trực tuyến từ máy chủ là GoogleAdsService.SearchStream. Để ghi đè thời gian chờ mặc định, hãy truyền timeoutMillis (tính bằng mili giây) vào mảng tuỳ chọn cuộc gọi không bắt buộc khi gọi phương thức:
private static function makeServerStreamingCall( GoogleAdsClient $googleAdsClient, int $customerId ) { $googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient(); // Creates a query that retrieves all campaign IDs. $query = 'SELECT campaign.id FROM campaign'; $output = ''; try { // Issues a search stream request by setting a custom client timeout. /** * @var GoogleAdsServerStreamDecorator $stream */ $stream = $googleAdsServiceClient->searchStream( SearchGoogleAdsStreamRequest::build($customerId, $query), [ // Any server streaming call has a default timeout setting. For this // particular call, the default setting can be found in the following file: // https://github.com/googleads/google-ads-php/blob/master/src/Google/Ads/GoogleAds/V26/Services/resources/google_ads_service_client_config.json. // // When making a server streaming call, an optional argument is provided and can // be used to override the default timeout setting with a given value. 'timeoutMillis' => self::CLIENT_TIMEOUT_MILLIS ] ); // Iterates over all rows in all messages and collects the campaign IDs. foreach ($stream->iterateAllElements() as $googleAdsRow) { /** * @var GoogleAdsRow $googleAdsRow */ $output .= ' ' . $googleAdsRow->getCampaign()->getId(); } print 'The server streaming call completed before the timeout.' . PHP_EOL; } catch (ApiException $exception) { if ($exception->getStatus() === ApiStatus::DEADLINE_EXCEEDED) { print 'The server streaming call did not complete before the timeout.' . PHP_EOL; } else { // Bubbles up if the exception is not about timeout. throw $exception; } } finally { print 'All campaign IDs retrieved:' . ($output ?: ' None') . PHP_EOL; } }
Ghi đè thời gian chờ cho lệnh gọi một ngôi
Hầu hết các phương thức dịch vụ của Google Ads API đều sử dụng lệnh gọi một ngôi; ví dụ điển hình là
GoogleAdsService.Search và
GoogleAdsService.Mutate. Để ghi đè thời gian chờ mặc định, hãy truyền timeoutMillis (tính bằng mili giây) vào mảng tuỳ chọn lệnh gọi không bắt buộc khi gọi phương thức:
private static function makeUnaryCall(GoogleAdsClient $googleAdsClient, int $customerId) { $googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient(); // Creates a query that retrieves all campaign IDs. $query = 'SELECT campaign.id FROM campaign'; $output = ''; try { // Issues a search request by setting a custom client timeout. $response = $googleAdsServiceClient->search( SearchGoogleAdsRequest::build($customerId, $query), [ // Any unary call is retryable and has default retry settings. // Complete information about these settings can be found here: // https://googleapis.github.io/gax-php/master/Google/ApiCore/RetrySettings.html. // For this particular call, the default retry settings can be found in the // following file: // https://github.com/googleads/google-ads-php/blob/master/src/Google/Ads/GoogleAds/V26/Services/resources/google_ads_service_client_config.json. // // When making an unary call, an optional argument is provided and can be // used to override the default retry settings with given values. 'retrySettings' => [ // Sets the maximum accumulative timeout of the call, it includes all tries. 'totalTimeoutMillis' => self::CLIENT_TIMEOUT_MILLIS, // Sets the timeout that is used for the first try to one tenth of the // maximum accumulative timeout of the call. // Note: This overrides the default value and can lead to // RequestError.RPC_DEADLINE_TOO_SHORT errors when too small. We recommend // to do it only if necessary. 'initialRpcTimeoutMillis' => self::CLIENT_TIMEOUT_MILLIS / 10, // Sets the maximum timeout that can be used for any given try to one fifth // of the maximum accumulative timeout of the call (two times greater than // the timeout that is used for the first try). 'maxRpcTimeoutMillis' => self::CLIENT_TIMEOUT_MILLIS / 5 ] ] ); // Iterates over all rows in all messages and collects the campaign IDs. foreach ($response->iterateAllElements() as $googleAdsRow) { /** * @var GoogleAdsRow $googleAdsRow */ $output .= ' ' . $googleAdsRow->getCampaign()->getId(); } print 'The unary call completed before the timeout.' . PHP_EOL; } catch (ApiException $exception) { if ($exception->getStatus() === ApiStatus::DEADLINE_EXCEEDED) { print 'The unary call did not complete before the timeout.' . PHP_EOL; } else { // Bubbles up if the exception is not about timeout. throw $exception; } } finally { print 'All campaign IDs retrieved:' . ($output ?: ' None') . PHP_EOL; } }