Timeout reading a post request containing heavy dictionnary

I am receiving a POST request containing a 3 mB Json dictionary. When i receive de POST, my view tries to read the args in the request but that causes a timeout on the Heroku server. When i try it locally, it works fine though. Here is the code:

def send_request():
    with open(file) as f:
        data = json.loads(f.read())
    data = json.dumps(data)
    response = requests.post(url=url_recette, data=data)
    return response
    
@csrf_exempt
def view(request):
    print('the server gets here')
    _args = json.loads(request.body.decode('utf-8'))
    print('the server never gets here so the timeout occurs when i load the args')

error on Heroku =>

Jan 13 09:00:03 gestion-ugip-recette app[web] INFO [2023-01-13 09:00:03 +0100] [18] [INFO] Worker exiting (pid: 18)
Jan 13 09:00:03 gestion-ugip-recette heroku[router] error at=error code=H13 desc="Connection closed without response" method=POST path="/resiliation/api_get_requete_from_ak" host=gestion-ugip-recette.herokuapp.com request_id=ccd0a85b-8454-458d-90f8-ab9a59a203fb fwd="83.205.21.231" dyno=web.1 connect=0ms service=30826ms status=503 bytes=0 protocol=https
Jan 13 09:00:04 gestion-ugip-recette app[web] INFO [2023-01-13 09:00:04 +0100] [26] [INFO] Booting worker with pid: 26```

Has anyone an idea of what could be the problem? thx in advance.

This could be due to a number of reasons such as limited resources on the Heroku server or a slow connection.

One possible solution is to increase the timeout limit on the Heroku server. This can be done by setting the timeout parameter in the gunicorn command in the Procfile.

Another potential solution is to optimize your code to reduce the amount of time it takes to load the JSON data. This could involve using a different method to read the JSON data or reducing the size of the data being sent.

It could also be a problem with memory consumption, you may want to try to optimize your data to reduce the size of the JSON dictionary and also check for any memory leaks in your code.

It might also be worth checking the error logs for the Heroku app to see if there is any additional information about the cause of the timeout.

Back to Top