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"

