Set a connection timeout for Boto to prevent cloud storage deadlock

This commit is contained in:
Albert Santoni 2015-02-17 14:58:19 -05:00
parent a1436bfebb
commit d8f5e41fca
1 changed files with 6 additions and 0 deletions

View File

@ -1,10 +1,12 @@
import os import os
import logging import logging
import uuid import uuid
import socket
from boto.s3.connection import S3Connection from boto.s3.connection import S3Connection
from boto.s3.key import Key from boto.s3.key import Key
STORAGE_BACKEND_FILE = "file" STORAGE_BACKEND_FILE = "file"
SOCKET_TIMEOUT = 240
class CloudStorageUploader: class CloudStorageUploader:
""" A class that uses Python-Boto SDK to upload objects into Amazon S3. """ A class that uses Python-Boto SDK to upload objects into Amazon S3.
@ -88,6 +90,10 @@ class CloudStorageUploader:
resource_id = "%s/%s/%s_%s%s" % (metadata['file_prefix'], unique_id_prefix, file_name, unique_id, extension) resource_id = "%s/%s/%s_%s%s" % (metadata['file_prefix'], unique_id_prefix, file_name, unique_id, extension)
# Boto uses the "global default timeout" by default, which is infinite! To prevent network problems from
# turning into deadlocks, we explicitly set the global default timeout period here:
socket.setdefaulttimeout(SOCKET_TIMEOUT)
conn = S3Connection(self._api_key, self._api_key_secret, host=self._host) conn = S3Connection(self._api_key, self._api_key_secret, host=self._host)
bucket = conn.get_bucket(self._bucket) bucket = conn.get_bucket(self._bucket)