Fix major pypo freeze
* Set a timeout for HTTP requests in api_client so that they don't hang forever if there's a network interruption. * Prevents pypo from completely freezing up in the network gets jacked.
This commit is contained in:
parent
c84c6d9d22
commit
d53c81a410
|
@ -10,6 +10,7 @@ import sys
|
||||||
import time
|
import time
|
||||||
import urllib
|
import urllib
|
||||||
import urllib2
|
import urllib2
|
||||||
|
import socket
|
||||||
import logging
|
import logging
|
||||||
import json
|
import json
|
||||||
import base64
|
import base64
|
||||||
|
@ -121,6 +122,9 @@ class ApcUrl(object):
|
||||||
else: return self.base_url
|
else: return self.base_url
|
||||||
|
|
||||||
class ApiRequest(object):
|
class ApiRequest(object):
|
||||||
|
|
||||||
|
API_HTTP_REQUEST_TIMEOUT = 30 # 30 second HTTP request timeout
|
||||||
|
|
||||||
def __init__(self, name, url, logger=None):
|
def __init__(self, name, url, logger=None):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.url = url
|
self.url = url
|
||||||
|
@ -134,9 +138,15 @@ class ApiRequest(object):
|
||||||
self.logger.debug(final_url)
|
self.logger.debug(final_url)
|
||||||
try:
|
try:
|
||||||
req = urllib2.Request(final_url, _post_data)
|
req = urllib2.Request(final_url, _post_data)
|
||||||
f = urllib2.urlopen(req)
|
f = urllib2.urlopen(req, timeout=ApiRequest.API_HTTP_REQUEST_TIMEOUT)
|
||||||
content_type = f.info().getheader('Content-Type')
|
content_type = f.info().getheader('Content-Type')
|
||||||
response = f.read()
|
response = f.read()
|
||||||
|
#Everything that calls an ApiRequest should be catching URLError explicitly
|
||||||
|
#(according to the other comments in this file and a cursory grep through the code)
|
||||||
|
#Note that URLError can occur for timeouts as well as socket.timeout
|
||||||
|
except socket.timeout:
|
||||||
|
self.logger.error('HTTP request to %s timed out', final_url)
|
||||||
|
raise
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
#self.logger.error('Exception: %s', e)
|
#self.logger.error('Exception: %s', e)
|
||||||
#self.logger.error("traceback: %s", traceback.format_exc())
|
#self.logger.error("traceback: %s", traceback.format_exc())
|
||||||
|
@ -277,7 +287,7 @@ class AirtimeApiClient(object):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
request = urllib2.Request(url, data, headers)
|
request = urllib2.Request(url, data, headers)
|
||||||
response = urllib2.urlopen(request).read().strip()
|
response = urllib2.urlopen(request, timeout=ApiClient.API_HTTP_REQUEST_TIMEOUT).read().strip()
|
||||||
|
|
||||||
logger.info("uploaded show result %s", response)
|
logger.info("uploaded show result %s", response)
|
||||||
break
|
break
|
||||||
|
|
Loading…
Reference in New Issue