विज्ञापन दिखने से पहले का समय अंतर (टाइम आउट)

सभी Google Ads API सेवाओं की डिफ़ॉल्ट सेटिंग होती हैं, जिनमें ट्रांसपोर्ट की ओर से इस्तेमाल होने वाले टाइम आउट भी शामिल हैं. Google Ads API के किसी भी वर्शन की किसी भी सेवा में एक खास JSON फ़ाइल होती है. इसमें सेवा और तरीके के लेवल पर इन डिफ़ॉल्ट सेटिंग को तय किया जाता है. उदाहरण के लिए, Google Ads API के नए वर्शन से जुड़ी फ़ाइलें यहां देखी जा सकती हैं.

इस्तेमाल के ज़्यादातर मामलों के लिए, डिफ़ॉल्ट सेटिंग ही काफ़ी होती हैं, लेकिन कभी-कभी आपको उन्हें बदलने की ज़रूरत भी पड़ सकती है. PHP की क्लाइंट लाइब्रेरी, सर्वर स्ट्रीमिंग और एकांगी कॉल, दोनों के लिए समय खत्म होने की सेटिंग को बदलने का समर्थन करती है.

टाइम आउट की अवधि को दो घंटे या उससे ज़्यादा पर सेट किया जा सकता है. हालांकि, एपीआई अब भी लंबे समय से चल रहे अनुरोधों का समय खत्म कर सकता है और DEADLINE_EXCEEDED गड़बड़ी दिखा सकता है.

सर्वर स्ट्रीमिंग कॉल के लिए टाइम आउट ओवरराइड करना

इस तरह के कॉल का इस्तेमाल करने वाला, Google Ads API की सेवा का इकलौता तरीका GoogleAdsService.SearchStream है. डिफ़ॉल्ट टाइम आउट को बदलने के लिए, आपको इस तरीके को कॉल करते समय एक और पैरामीटर जोड़ना होगा:

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/V16/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;
    }
}
      

सिंगल कॉल के लिए टाइम आउट ओवरराइड करना

Google Ads API की सेवा के ज़्यादातर तरीकों में, सिंगल कॉल का इस्तेमाल किया जाता है. इसके आम तौर पर, GoogleAdsService.Search और GoogleAdsService.Mutate जैसे तरीकों का इस्तेमाल किया जाता है. डिफ़ॉल्ट टाइम आउट को बदलने के लिए, आपको इस तरीके को कॉल करते समय एक और पैरामीटर जोड़ना होगा:

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/V16/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;
    }
}