Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Google Cloud Function Connection reset by peer

As detailed here: https://issuetracker.google.com/issues/113672049

Cross-posted here: https://github.com/GoogleCloudPlatform/google-cloud-python/issues/5879)

I'm getting a connection reset error when using the Firebase Storage API from a Google Cloud Function in Python.

The deployed function is calling one blob-get i.e.

from firebase_admin import storage

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

The failure is intermittent; the function has around a 90% success rate.

It seems more likely to fail the first time the function is called after it is deployed.

like image 614
Brian M. Hunt Avatar asked Sep 01 '18 15:09

Brian M. Hunt


1 Answers

Cloud functions are stateless, but can re-use global state from previous invocations. This is explained in tips and these docs.

Using global state with retries should give you a more robust function:

from tenacity import retry, stop_after_attempt, wait_random
from firebase_admin import storage

@retry(stop=stop_after_attempt(3), wait=wait_random(min=1, max=2))
def get_bucket(storage):
    return storage.bucket('my-firebase-bucket')

@retry(stop=stop_after_attempt(3), wait=wait_random(min=1, max=2))
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..
like image 100
timvink Avatar answered Oct 02 '22 17:10

timvink