Skip to content

Commit f44c910

Browse files
committed
Added method to update config remotely (update_config).
1 parent 31ecd92 commit f44c910

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

elude/servers/__init__.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ def __init__(self, proxy_gatherer, serialize_func, deserialize_func):
3333
self.serialize = serialize_func
3434
self.deserialize = deserialize_func
3535
self.proxy_gatherer = proxy_gatherer
36+
# make a local copy of the configs
37+
self.config = dict((k, getattr(config, k)) for k in dir(config) if not k.startswith('__'))
3638
proxy_gatherer.new_proxy_callbacks.append(lambda proxy: asyncio.async(self.register_proxy(proxy)))
3739

3840
def put_request(self, request_obj, failing=False):
@@ -54,7 +56,7 @@ def register_proxy(self, proxy):
5456
while True:
5557
# Unhealthy state
5658
with (yield from Proxy.test_semaphore):
57-
r, r_text = yield from fetch_one('get', 'http://myexternalip.com/json', config.PROXY_TEST_TIMEOUT,
59+
r, r_text = yield from fetch_one('get', 'http://myexternalip.com/json', self.config['PROXY_TEST_TIMEOUT'],
5860
proxy.get_connector())
5961
if r is None:
6062
break # Terminate usage of the proxy.
@@ -74,7 +76,7 @@ def register_proxy(self, proxy):
7476
logger.debug('received request: %s' % str(request_obj))
7577
smooth_request = yield from self.process_request(request_obj, proxy)
7678
if not smooth_request:
77-
# This means that the proxy is somehow faulty. Return back the task to the queue and go back to unhealthy state.
79+
# This means that a retry is warranted (e.g. the proxy is somehow faulty). Return back the task to the queue and go back to unhealthy state.
7880
self.put_request(request_obj, True)
7981
break
8082

@@ -83,13 +85,14 @@ def process_request(self, request_obj, proxy):
8385
"""Executes task based on JSON-RPC 2.0 compatible request/response constructs.
8486
task_obj is a Request object dict.
8587
Calls or schedules calls to response callbacks (even for notifications - request id will be None in this case).
86-
Returns False if error is suspected due to proxy, True otherwise.
88+
Returns False if a retry is warranted, True otherwise.
8789
See http://www.jsonrpc.org/specification for specs of the Request and Response objects."""
8890
rid = request_obj.get('id', None) # rid None means it is a notification
91+
retval = True
8992
try:
9093
method_name = '_process_request_%s' % request_obj.get('method', '')
9194
if hasattr(self, method_name):
92-
yield from getattr(self, method_name)(proxy, **request_obj['params'])
95+
retval = yield from getattr(self, method_name)(proxy, **request_obj['params'])
9396
else:
9497
self.process_response({'id': rid, 'error': {'code': -32601, 'message': 'Method not found'}})
9598
except Exception as e:
@@ -98,7 +101,7 @@ def process_request(self, request_obj, proxy):
98101
traceback.print_exc()
99102
self.process_response(
100103
{'id': rid, 'error': {'code': -32000, 'message': '%s: %s' % (type(e).__name__, str(e))}})
101-
return True
104+
return retval
102105

103106
def process_response(self, response):
104107
"""response is a dict with JSON-RPC Response object structure.
@@ -108,16 +111,21 @@ def process_response(self, response):
108111
@asyncio.coroutine
109112
def _process_request_fetch(self, proxy, rid=None, url=''):
110113
logger.debug('processing request: rid = %s url=%s' % (str(rid), url))
111-
r, r_text = yield from fetch_one('get', url, config.FETCHER_TIMEOUT,
114+
r, r_text = yield from fetch_one('get', url, self.config['FETCHER_TIMEOUT'],
112115
proxy.get_connector())
113116
logger.debug('processing request: rid = %s url=%s' % (str(rid), url))
114117
if r is None:
115118
return False
116119
else:
117120
self.process_response({'id': rid, 'result': r_text})
121+
return True
118122

119123
@asyncio.coroutine
120124
def _process_request_prefetch(self, proxy, rid=None, url=''):
121125
# Note difference between this and fetch is just the priority. Priority calculation is done at put_request().
122126
return (yield from self._process_request_fetch(proxy, rid, url))
123127

128+
@asyncio.coroutine
129+
def _process_request_update_config(self, proxy, config, rid=None):
130+
self.config.update(config) # TODO: validate input
131+
return True

test.py

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def process_response(self, response):
1515
asyncio.get_event_loop().stop()
1616

1717
server = MockServer(gatherer, json.dumps, json.loads)
18+
server.put_request({'method': 'update_config', 'params': {'config': {'PROXY_TEST_URL': 'http://finance.google.com'}}, 'id': 'x'})
1819
server.put_request({'method': 'prefetch', 'params': {'url': 'http://leontius.net'}, 'id': 'x'})
1920
server.put_request({'method': 'fetch', 'params': {'url': 'http://leontius.net'}, 'id': 'y'})
2021
asyncio.get_event_loop().run_until_complete(asyncio.wait([gatherer.start_getting_proxies()]))

0 commit comments

Comments
 (0)