12
12
SCRIPT = os .path .basename (__file__ )
13
13
ENDPOINT = "https://registry-test.cilogon.org/registry/"
14
14
OSG_CO_ID = 8
15
+ MINTIMEOUT = 5
16
+ MAXTIMEOUT = 625
17
+ TIMEOUTMULTIPLE = 5
15
18
16
19
17
20
_usage = f"""\
26
29
(default = { ENDPOINT } )
27
30
-o outfile specify output file (default: write to stdout)
28
31
-g filter_group filter users by group name (eg, 'ap1-login')
32
+ -t minTimeout set minimum timeout, in seconds, for API call (default to { MINTIMEOUT } )
33
+ -T maxTimeout set maximum timeout, in seconds, for API call (default to { MAXTIMEOUT } )
29
34
-h display this help text
30
35
31
36
PASS for USER is taken from the first of:
@@ -50,6 +55,8 @@ class Options:
50
55
outfile = None
51
56
authstr = None
52
57
filtergrp = None
58
+ min_timeout = MINTIMEOUT
59
+ max_timeout = MAXTIMEOUT
53
60
54
61
55
62
options = Options ()
@@ -87,8 +94,20 @@ def mkrequest(target, **kw):
87
94
88
95
def call_api (target , ** kw ):
89
96
req = mkrequest (target , ** kw )
90
- resp = urllib .request .urlopen (req )
91
- payload = resp .read ()
97
+ trying = True
98
+ currentTimeout = options .min_timeout
99
+ while trying :
100
+ try :
101
+ resp = urllib .request .urlopen (req , timeout = currentTimeout )
102
+ payload = resp .read ()
103
+ trying = False
104
+ except urllib .error .URLError as exception :
105
+ if (currentTimeout < options .max_timeout ):
106
+ currentTimeout *= TIMEOUTMULTIPLE
107
+ else :
108
+ sys .exit (f"Exception raised after maximum timeout { options .max_timeout } seconds reached. "
109
+ + f"Exception reason: { exception .reason } .\n Request: { req .full_url } " )
110
+
92
111
return json .loads (payload ) if payload else None
93
112
94
113
@@ -147,7 +166,7 @@ def get_co_person_osguser(pid):
147
166
148
167
def parse_options (args ):
149
168
try :
150
- ops , args = getopt .getopt (args , 'u:c:d:f:g:e:o:h' )
169
+ ops , args = getopt .getopt (args , 'u:c:d:f:g:e:o:t:T: h' )
151
170
except getopt .GetoptError :
152
171
usage ()
153
172
@@ -159,13 +178,15 @@ def parse_options(args):
159
178
160
179
for op , arg in ops :
161
180
if op == '-h' : usage ()
162
- if op == '-u' : options .user = arg
163
- if op == '-c' : options .osg_co_id = int (arg )
164
- if op == '-d' : passfd = int (arg )
165
- if op == '-f' : passfile = arg
166
- if op == '-e' : options .endpoint = arg
167
- if op == '-o' : options .outfile = arg
168
- if op == '-g' : options .filtergrp = arg
181
+ if op == '-u' : options .user = arg
182
+ if op == '-c' : options .osg_co_id = int (arg )
183
+ if op == '-d' : passfd = int (arg )
184
+ if op == '-f' : passfile = arg
185
+ if op == '-e' : options .endpoint = arg
186
+ if op == '-o' : options .outfile = arg
187
+ if op == '-g' : options .filtergrp = arg
188
+ if op == '-t' : options .min_timeout = float (arg )
189
+ if op == '-T' : options .max_timeout = float (arg )
169
190
170
191
user , passwd = getpw (options .user , passfd , passfile )
171
192
options .authstr = mkauthstr (user , passwd )
0 commit comments