From e76abaadf74b11d9f66f6f010c28fe9a9a7beebc Mon Sep 17 00:00:00 2001 From: srz_zumix Date: Thu, 21 May 2020 19:15:22 +0900 Subject: [PATCH] wandbox request timeout (#439) * wandbox request timeout * update version --- tools/wandbox/iuwandbox.py | 24 ++----------------- tools/wandbox/wandbox.py | 48 ++++++++++++++++++++++++++++++++++---- 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/tools/wandbox/iuwandbox.py b/tools/wandbox/iuwandbox.py index 1e0cb79960..b03b48f911 100644 --- a/tools/wandbox/iuwandbox.py +++ b/tools/wandbox/iuwandbox.py @@ -44,7 +44,7 @@ def parse_command_line(): '-v', '--version', action='version', - version=u'%(prog)s version 6.2' + version=u'%(prog)s version 6.3' ) parser.add_argument( '--list-compiler', @@ -446,27 +446,7 @@ def expand_wandbox_options(w, compiler, options): def wandbox_api_call(callback, retries, retry_wait): - try: - return callback() - except (HTTPError, ConnectionError) as e: - - def is_retry(e): - if not e.response: - return True - return e.response.status_code in [504] - - if is_retry(e) and retries > 0: - try: - print(e.message) - except: - pass - print('wait {0}sec...'.format(retry_wait)) - sleep(retry_wait) - return wandbox_api_call(callback, retries - 1, retry_wait) - else: - raise - except: - raise + return Wandbox.Call(callback, retries, retry_wait) def wandbox_get_compilerlist(): diff --git a/tools/wandbox/wandbox.py b/tools/wandbox/wandbox.py index 9b803ffedb..38e62919fd 100644 --- a/tools/wandbox/wandbox.py +++ b/tools/wandbox/wandbox.py @@ -10,6 +10,10 @@ import requests import json +from time import sleep +from requests.exceptions import HTTPError as RHTTPError +from requests.exceptions import ConnectionError as RConnectionError +from requests.exceptions import ConnectTimeout as RConnectTimeout # # @@ -18,6 +22,7 @@ class Wandbox: #api_url = 'http://melpon.org/wandbox/api' api_url = 'https://wandbox.org/api' + timeout_ = (3.0, 60.0 * 5) def __init__(self): self.reset() @@ -34,7 +39,7 @@ def GetCompilerList(): """ get compiler list """ - response = requests.get(Wandbox.api_url + '/list.json') + response = requests.get(Wandbox.api_url + '/list.json', timeout=3.0) response.raise_for_status() return response.json() @@ -50,10 +55,18 @@ def GetPermlink(link): """ get wandbox permanet link """ - response = requests.get(Wandbox.api_url + '/permlink/' + link) + response = requests.get(Wandbox.api_url + '/permlink/' + link, timeout=3.0) response.raise_for_status() return response.json() + @property + def timeout(self): + return self.timeout_ + + @timeout.setter + def timeout(self, v): + self.timeout_ = v + def get_permlink(self, link): """ get wandbox permanet link @@ -67,7 +80,7 @@ def run(self): """ headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} payload = json.dumps(self.parameter) - response = requests.post(self.api_url + '/compile.json', data=payload, headers=headers) + response = requests.post(self.api_url + '/compile.json', data=payload, headers=headers, timeout=self.timeout_) response.raise_for_status() try: return response.json() @@ -149,11 +162,38 @@ def reset(self): """ self.parameter = {'code': ''} + @staticmethod + def Call(action, retries, retry_wait): + try: + return action() + except (RHTTPError, RConnectionError, RConnectTimeout) as e: + + def is_retry(e): + if e is None: + return False + if e.response is None: + return False + return e.response.status_code in [500, 502, 503, 504] + + retries -= 1 + if is_retry(e) and retries > 0: + try: + print(e.message) + except: + pass + print('wait {0}sec...'.format(retry_wait)) + sleep(retry_wait) + return Wandbox.Call(action, retries, retry_wait) + else: + raise + except: + raise + if __name__ == '__main__': with Wandbox() as w: w.compiler('gcc-head') w.options('warning,gnu++1y') w.compiler_options('-Dx=hogefuga\n-O3') - w.code('#include \nint main() { int x = 0; ::std::cout << "hoge" << ::std::endl; }') + w.code('#include \nint main() { int x = 0; std::cout << "hoge" << std::endl; }') print(w.run())