Python POST request doesn't send request until after waiting for duration of timeout period on google app engine

Viewed 391

I am making a python POST request using the python-requests library

import requests

# Set token, headers, data variables here

r = requests.post(f'https://api.popularwebsite.com/endpoint?access_token={token}', headers=headers, data=data)

timetaken = r.elapsed.total_seconds()

This code is taking a long time to run, and at the end, the timetaken variable is some value between 127 and 128, however the request is successful. I can add some logging and in the logs, I can see the request isn't sent until after 127 seconds

Logging code:

import logging

import http.client
http.client.HTTPConnection.debuglevel = 1

logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True

Logs:

2021-03-15 (04:02:01.522) DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): api.popularwebsite.com:443
2021-03-15 (04:04:08.646) send: b'{"myJsonPayload": 1234}'

Additionally, I can pass a timeout argument of n seconds to the post function. This changes how long the request waits, but the program will still sit idle for n seconds before making the request. At the end, timetaken > n and timetaken < n+1.

This is in the context of a python flask application deployed on Google App Engine. Oddly, it seems the conditions that cause this issue are the combination of my server and this api. I can make requests to other endpoints without issue from my server. I can also run the python code locally and make requests to the problematic api from my PC without issue. Some local environment issue on the server must be messing up requests for this particular url. This app has been making requests to this endpoint without issue for 2 years, and spontaneously started experiencing this issue a few days ago

I have tried redeploying my app onto my server, and this might fix the issue for the first request or two, but then the issue will return. I have also attempted deploying a previous version of the code, to no avail. This confirms the issue is not due to a code change.

Any ideas what could be causing this, or how I should go about troubleshooting?

Edit: The issue seems sporadic. It will start up and go away unpredictably

Edit: Here is my app.yaml file

runtime: python37
entrypoint: gunicorn -b :$PORT --chdir src main:app --timeout 300
instance_class: F1

env_variables:
    KEY1: "value1"

Sequence diagrams

3 Answers

It seems to be a caching issue performed by the client, Please try to clear your browser cache as I suppose that the browser was the one doing the cache-ing.

Please note re-deploying a new version of the app will not remove the cache. So you can use the default_expiration element of Google App Engine to configure the time a static resource will be cached in web proxies and browsers.

In most cases, the default 10-minute expiration time is appropriate.”

If you would like caching to be refreshed quicker, then you can override it to a lower time (down to 0) by adding the following .yaml element:

default_expiration: 0s. 

That will override the global expiration time, or if you wish to only set it for certain handlers, you can use the following.

I wonder which app engine are you using. The app engine flexible or standard

If it's standard and if there is no load then it Scales to zero and after your next call, it took time. But if it's flexible then a minimum of 1 instance would be there. If that is not the case you have to check other reasons.

If that is the case: One workaround you can set min instance for app engine standard

Comparing high-level features

I gave up trying to troubleshoot, and created a new GCP project in a different region and migrated. No more issues

Related