使用 google 云 python 库指定自定义重试时出错

Error when specifying custom retry with the google cloud python lib

我正在尝试使用 python SDK 通过 GCP PUB/SUB 创建订阅者。根据文档,您可以在 create_subscription 上指定一个 retry arg 来自定义重试延迟,就像您在 UI 中通过网络仪表板所做的那样。但是,当我这样做时,出现错误

Unknown field for Subscription: retry

https://googleapis.dev/python/pubsub/latest/subscriber/api/client.html#google.cloud.pubsub_v1.subscriber.client.Client.create_subscription

subscriber = pubsub.SubscriberClient()
    subscription_path = subscriber.subscription_path(env.GCP_PROJECT_ID, details['name'])
    push_config = pubsub.types.PushConfig(push_endpoint=details['endpoint'])

    custom_retry = retry.Retry(
        initial=60.0,
        maximum=600.0,
        multiplier=1.45,
    )

    # Wrap the subscriber in a 'with' block to automatically call close() to
    # close the underlying gRPC channel when done.
    with subscriber:

        subscription = subscriber.create_subscription(
            request={
                "name": subscription_path,
                "topic": topic_path,
                "push_config": push_config,
                "ack_deadline_seconds": 30,
                "retry": custom_retry
            }
        )

您使用的 retry 代码是通用代码,可用于其他 google api 的代码。

您可以使用 retry_policy 创建订阅,并且可以专门用于 Pub/Sub。这将使您能够自定义Return策略-指数退避延迟后重试参数,其中包括最小退避 最大回退 .

当我使用 GCP Pub/Sub 用户界面比较我的代码中指定的参数和创建的订阅中指定的参数时,请查看下面的代码并为我工作。

from google.cloud import pubsub_v1 as pubsub
from google.protobuf.duration_pb2 import Duration

subscriber = pubsub.SubscriberClient()
subscription_path = subscriber.subscription_path('<you_project>', '<your_subscription>')
push_config = pubsub.types.PushConfig(push_endpoint='<your_endpoint>')

#This is where you custom your Minimum Backoff and Maximum Backoff
minimum_backoff = Duration(seconds = 15, nanos = 0)
maximum_backoff = Duration(seconds = 550, nanos = 0)
retry_policy = pubsub.types.RetryPolicy(
      minimum_backoff=minimum_backoff,
      maximum_backoff=maximum_backoff
)

create_request=pubsub.types.Subscription(
    name=subscription_path,
    topic='projects/<your_project>/topics/<your_topic>',
    push_config=push_config,
    retry_policy=retry_policy
) 

with subscriber:

    subscription_resource = subscriber.create_subscription(
        request=create_request        
)