@@ -33,6 +33,8 @@ def __init__(self, proxy_gatherer, serialize_func, deserialize_func):
33
33
self .serialize = serialize_func
34
34
self .deserialize = deserialize_func
35
35
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 ('__' ))
36
38
proxy_gatherer .new_proxy_callbacks .append (lambda proxy : asyncio .async (self .register_proxy (proxy )))
37
39
38
40
def put_request (self , request_obj , failing = False ):
@@ -54,7 +56,7 @@ def register_proxy(self, proxy):
54
56
while True :
55
57
# Unhealthy state
56
58
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' ] ,
58
60
proxy .get_connector ())
59
61
if r is None :
60
62
break # Terminate usage of the proxy.
@@ -74,7 +76,7 @@ def register_proxy(self, proxy):
74
76
logger .debug ('received request: %s' % str (request_obj ))
75
77
smooth_request = yield from self .process_request (request_obj , proxy )
76
78
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.
78
80
self .put_request (request_obj , True )
79
81
break
80
82
@@ -83,13 +85,14 @@ def process_request(self, request_obj, proxy):
83
85
"""Executes task based on JSON-RPC 2.0 compatible request/response constructs.
84
86
task_obj is a Request object dict.
85
87
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.
87
89
See http://www.jsonrpc.org/specification for specs of the Request and Response objects."""
88
90
rid = request_obj .get ('id' , None ) # rid None means it is a notification
91
+ retval = True
89
92
try :
90
93
method_name = '_process_request_%s' % request_obj .get ('method' , '' )
91
94
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' ])
93
96
else :
94
97
self .process_response ({'id' : rid , 'error' : {'code' : - 32601 , 'message' : 'Method not found' }})
95
98
except Exception as e :
@@ -98,7 +101,7 @@ def process_request(self, request_obj, proxy):
98
101
traceback .print_exc ()
99
102
self .process_response (
100
103
{'id' : rid , 'error' : {'code' : - 32000 , 'message' : '%s: %s' % (type (e ).__name__ , str (e ))}})
101
- return True
104
+ return retval
102
105
103
106
def process_response (self , response ):
104
107
"""response is a dict with JSON-RPC Response object structure.
@@ -108,16 +111,21 @@ def process_response(self, response):
108
111
@asyncio .coroutine
109
112
def _process_request_fetch (self , proxy , rid = None , url = '' ):
110
113
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' ] ,
112
115
proxy .get_connector ())
113
116
logger .debug ('processing request: rid = %s url=%s' % (str (rid ), url ))
114
117
if r is None :
115
118
return False
116
119
else :
117
120
self .process_response ({'id' : rid , 'result' : r_text })
121
+ return True
118
122
119
123
@asyncio .coroutine
120
124
def _process_request_prefetch (self , proxy , rid = None , url = '' ):
121
125
# Note difference between this and fetch is just the priority. Priority calculation is done at put_request().
122
126
return (yield from self ._process_request_fetch (proxy , rid , url ))
123
127
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
0 commit comments