0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

GoogleCloudFunctionsでのtimeoutについて (Pub/Subトリガ)

Posted at

はじめに

今年の目標にoutputをすると決意して早半年、何もなかったのでさすがにまずいと思い、簡単なことでも公開しようとまとめてみました。

目的

CloudFunctionsで開発をするときGoogleCloudのリソースへのアクセス等に対して、timeout値を設定することがあると思います。
そのやり方としてsocketのtimeout値をいじることで全般的なtimeout値を変更できます。
一方でCloudFunctionsではPub/Subをtriggerに設定することが多く、socketのtimeout値をいじった際にPub/Subとの通信に悪影響を与えて上限までリトライされないか少し気になったので調査してみました。

前提条件

CloudFunctionsの設定は次の通りです。基本デフォルトで設定しています。

項目
Region us-central1
Mem 256MB
Timeout 60s
MinInstance 0
MaxInstance 3000
tigger pub/sub
Retry on failure disable

Pub/Sub(subscription)の設定値も記載します。

項目
Delivery type Push
Acknowledgement deadline 600 s
Exactly once delivery Disabled
Message ordering Disabled
Dead lettering Disabled
Retry policy Retry after exponential backoff delay
Minimum backoff duration 10 s
Maximum backoff duration 600s

検証に使ったコードは以下のものです。
BackendServiceの一覧を取得するコードにtimeoutの設定を追加しています。
timeoutにより例外が発生し、処理が終了するコードです。

import base64
import socket
import google.auth
from googleapiclient import discovery

def hello_pubsub(event, context):
    try:
      print("Start")
      pubsub_message = base64.b64decode(event['data']).decode('utf-8')
      socket.setdefaulttimeout(0.0000001)
      credentials, project = google.auth.default()
      api_svc = discovery.build("compute", "v1", credentials=credentials)
      req = api_svc.backendServices().list(project=project)
      print("start: get backend services.")
      # ここでtimeoutが発生する想定
      res = req.execute(num_retries=4)
    except Exception as e:
      print("Exception")
      print(e)
      return
    print("Finish")

試しにCoudFunctionsのtest機能でtimeoutが発生するか確認してみました。
timeout自体は発生したもののCloudFunctionsのStatusはOKになっていることが確認できました。
image.png

検証手順

実際にPub/Subと連携してみてPub/Subに悪影響が発生しないことを確認してみます。
手順はシンプルにPub/SubのTest機能でメッセージを送信します。
image.png

検証結果

Pub/SubとCloudFunctions側のMetricsを確認してみます。
添付画像のとおり、Pub/Subのメッセージ送信とCloudFunctionsの実行数が1件であることを確認できました。
image.png
image.png

まとめ

今回の検証結果では想定通りtimeout値を設定した場合でもPub/Sub側での再送は発生しませんでした。そのため、結果的にsocketでのtimeout値設定による影響はなさそうです。(結果返却時に影響する可能性はあるかもしれません)
推測としては以下が考えられるでしょうか(詳しい人教えてくださいmm)

  • Pub/Subとの通信がエントリポイントの処理を走らせる前に終わっていて影響を及ぼさなかった

上の場合、Pub/Sub側での結果受信が気になる(Pub/Subが側のリトライが機能しない?)ので違う気もしますが、このページでの検証は以上でした。
最後までありがとうございました!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?