Ruby 客户端库中的每项服务都附带默认超时和重试政策,这些政策在相应服务的 client.rb 文件中指定。例如,您可以检查CampaignService 客户端配置中的 self.configure 方法。
替换默认超时时间
在实例化服务实例后,您可以通过设置服务级 config.timeout(以秒为单位)或通过在 config.rpcs 上为各个 RPC 方法配置 timeout 和 retry_policy 来替换这些默认值:
require 'google/ads/google_ads'
client = Google::Ads::GoogleAds::GoogleAdsClient.new
ga_service = client.service.google_ads
ga_service.configure do |config|
# Set the default timeout (in seconds) for all RPCs on this service instance.
config.timeout = 120.0
# Override the timeout or retry policy for specific RPC methods.
config.rpcs.search_stream.timeout = 300.0
config.rpcs.search.timeout = 120.0
config.rpcs.search.retry_policy = {
'initial_delay' => 5.0,
'max_delay' => 60.0
}
end
如需查看完整的可运行脚本,请参阅 GitHub 上的 set_custom_client_timeouts.rb 代码示例。
超时限制和错误
虽然您可以增加客户端超时时长,但仍需遵守服务器端限制。如果请求耗时超过两小时,可能会失败并引发状态为 DEADLINE_EXCEEDED 的 Google::Ads::GoogleAds::Errors::GoogleAdsError 或 GRPC::DeadlineExceeded 异常。