Phuks-co / throat

Open Source link aggregator and discussion platform powering Phuks
https://phuks.co
MIT License
73 stars 32 forks source link

Use Werkzeug's FileStorage instead of temp files when uploading #371

Closed happy-river closed 3 years ago

happy-river commented 3 years ago

We've had an ongoing problem in production where gunicorn would restart gevent workers at irregular intervals, and recently identified a case where this was happening when the worker was trying to process the upload of a large file. Uploading and thumbnailing use temp files, and in the gunicorn docs they mention that accessing files on disk-backed storage from docker images on AWS may block for excessive lengths of time.

flask-cloudy and boto3 will accept Werkzeug's FileStorage objects (which are a wrapper around io.BytesIO) instead of file paths, so change uploading and thumbnailing to use FileStorage objects instead of temp files.

I also added some logging to record when files are put in storage, and the reasons for thumbnailing failure, and increased the request timeout for fetching images for thumbnailing, to reduce thumbnailing failures when linking to something on a slow server.