通过对等方重置Python Google Cloud Function Connection

时间:2018-09-01 15:34:02

标签: python google-cloud-storage google-cloud-functions firebase-storage

如此处所述:https://issuetracker.google.com/issues/113672049

交叉发布在这里:https://github.com/GoogleCloudPlatform/google-cloud-python/issues/5879

在Python中通过Google Cloud Function使用Firebase Storage API时,出现连接重置错误。

部署的函数正在调用一个Blob-get,即

from firebase_admin import storage

def fn(request):
  bucket = 'my-firebase-bucket'
  path = '/thing'
  blob = storage.bucket(bucket).get_blob(path)

故障是间歇性的;该功能的成功率约为90%。

在部署函数后,第一次调用该函数似乎更有可能失败。

2 个答案:

答案 0 :(得分:2)

云函数是无状态的,但是可以重用以前的调用中的全局状态。 tipsthese docs中对此进行了说明。

将全局状态与重试一起使用应为您提供更强大的功能:

import retrying
from firebase_admin import storage

@retrying.retry(stop_max_attempt_number=3, wait_random_min=1000, wait_random_max=2000)
def get_bucket(storage):
    return storage.bucket('my-firebase-bucket')

@retrying.retry(stop_max_attempt_number=3, wait_random_min=1000, wait_random_max=2000)
def get_blob(bucket, path):
    return bucket.get_blob(path)

bucket = get_bucket(storage)

def fn(request):
  path = '/thing'
  blob = get_blob(bucket, path)
  # etc..

答案 1 :(得分:0)

您可能要检查要创建多少个客户端。

  

尝试在函数调用之间重用网络连接,如下   在优化网络中进行了介绍。但是,请注意   闲置2分钟后,系统可能会将其关闭,并且   进一步尝试使用封闭的连接将导致   “连接重置”错误。您的代码应该使用一个库   很好地处理封闭的连接,如果使用则显式处理   低级网络结构。

https://cloud.google.com/functions/docs/concepts/exec#network

请参见以下示例,在该示例中,他们仅创建一次客户端并在函数中重用它:

import os
from google.cloud import pubsub_v1

# Create a global Pub/Sub client to avoid unneeded network activity
pubsub = pubsub_v1.PublisherClient()


def gcp_api_call(request):
    """
    HTTP Cloud Function that uses a cached client library instance to
    reduce the number of connections required per function invocation.
    Args:
        request (flask.Request): The request object.
    Returns:
        The response text, or any set of values that can be turned into a
        Response object using `make_response`
        <http://flask.pocoo.org/docs/1.0/api/#flask.Flask.make_response>.
    """

    project = os.getenv('GCP_PROJECT')
    request_json = request.get_json()

    topic_name = request_json['topic']
    topic_path = pubsub.topic_path(project, topic_name)

    # Process the request
    data = 'Test message'.encode('utf-8')
    pubsub.publish(topic_path, data=data)

    return '1 message published'

https://cloud.google.com/functions/docs/bestpractices/networking#accessing_google_apis