【问题标题】:How to prevent Connection reset by peer in a http python request in GCP如何防止 GCP 中的 http python 请求中的对等方重置连接
【发布时间】:2019-11-25 17:13:17
【问题描述】:

以下请求导致连接重置错误。这不会每次都失败,但偶尔会发生。对如何防止这种情况的任何想法表示赞赏。

http.request("https://dataflow.googleapis.com/v1b3/projects/%s/templates:launch?gcsPath=%s&location=us-central1" % (project, DATAFLOW_SPANNER_EXPORT),
        method="POST",
        headers={'Accept': 'application/json', 'Content-Type': 'application/json'},
        body=body )

这是收到的错误。我们的函数名为 create_spanner_export,它正在启动一个数据流作业。这不会每次都失败,但偶尔会发生。失败时,不会启动数据流作业。

 File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 383, in run_background_function
    _function_handler.invoke_user_function(event_object)
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 217, in invoke_user_function
    return call_user_function(request_or_event)
  File "/env/local/lib/python3.7/site-packages/google/cloud/functions/worker.py", line 214, in call_user_function
    event_context.Context(**request_or_event.context))
  File "/user_code/main.py", line 678, in database_export_main
    handle_export(logger, attrib, max_exports, output_dir)
  File "/user_code/main.py", line 526, in handle_export
    spanner_export(logger, attrib, max_exports, output_dir)
  File "/user_code/main.py", line 342, in spanner_export
    for d in database_list[:max_exports]]
  File "/user_code/main.py", line 342, in <listcomp>
    for d in database_list[:max_exports]]
  File "/user_code/main.py", line 274, in create_spanner_export
    body=body )
  File "/env/local/lib/python3.7/site-packages/oauth2client/transport.py", line 175, in new_request
    redirections, connection_type)
  File "/env/local/lib/python3.7/site-packages/oauth2client/transport.py", line 282, in request
    connection_type=connection_type)
  File "/env/local/lib/python3.7/site-packages/httplib2/__init__.py", line 1953, in request
    cachekey,
  File "/env/local/lib/python3.7/site-packages/httplib2/__init__.py", line 1618, in _request
    conn, request_uri, method, body, headers
  File "/env/local/lib/python3.7/site-packages/httplib2/__init__.py", line 1525, in _conn_request
    conn.request(method, request_uri, body, headers)
  File "/opt/python3.7/lib/python3.7/http/client.py", line 1229, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/opt/python3.7/lib/python3.7/http/client.py", line 1275, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/opt/python3.7/lib/python3.7/http/client.py", line 1224, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/opt/python3.7/lib/python3.7/http/client.py", line 1055, in _send_output
    self.send(chunk)
  File "/opt/python3.7/lib/python3.7/http/client.py", line 977, in send
    self.sock.sendall(data)
  File "/opt/python3.7/lib/python3.7/ssl.py", line 1015, in sendall
    v = self.send(byte_view[count:])
  File "/opt/python3.7/lib/python3.7/ssl.py", line 984, in send
    return self._sslobj.write(data)
ConnectionResetError: [Errno 104] Connection reset by peer

【问题讨论】:

  • 有关应用程序的更多信息...应用程序代码正在通过云功能调用/创建数据流作业以导出扳手数据库。因此,此代码位于云函数中。每天备份一次的数据库少于 15 个,每次调用此备份数据流例程之间有 5 分钟的时间间隔。数据库的大小相对非常小——每个不到 5 MB。似乎最多有两个导出数据流作业同时运行一段时间。云功能不会等待导出数据流作业完成。

标签: python-3.x http google-cloud-platform google-cloud-functions


【解决方案1】:

使用重试应该可以工作:

@retry(stop=stop_after_attempt(3), wait=wait_random(min=1, max=2))
def sendReq(...):
    http.request("https://dataflow.googleapis.com/v1b3/projects/%s/templates:launch?gcsPath=%s&location=us-central1" % (project, DATAFLOW_SPANNER_EXPORT),
    method="POST",
    headers={'Accept': 'application/json', 'Content-Type': 'application/json'},
            body=body )

在此处查看更多信息:https://cloud.google.com/functions/docs/bestpractices/retries

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 2018-01-02
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多