1212SCRIPT = os .path .basename (__file__ )
1313ENDPOINT = "https://registry-test.cilogon.org/registry/"
1414OSG_CO_ID = 8
15+ MINTIMEOUT = 5
16+ MAXTIMEOUT = 625
17+ TIMEOUTMULTIPLE = 5
1518
1619
1720_usage = f"""\
2629 (default = { ENDPOINT } )
2730 -o outfile specify output file (default: write to stdout)
2831 -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 } )
2934 -h display this help text
3035
3136PASS for USER is taken from the first of:
@@ -50,6 +55,8 @@ class Options:
5055 outfile = None
5156 authstr = None
5257 filtergrp = None
58+ min_timeout = MINTIMEOUT
59+ max_timeout = MAXTIMEOUT
5360
5461
5562options = Options ()
@@ -87,8 +94,20 @@ def mkrequest(target, **kw):
8794
8895def call_api (target , ** kw ):
8996 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+
92111 return json .loads (payload ) if payload else None
93112
94113
@@ -147,7 +166,7 @@ def get_co_person_osguser(pid):
147166
148167def parse_options (args ):
149168 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' )
151170 except getopt .GetoptError :
152171 usage ()
153172
@@ -159,13 +178,15 @@ def parse_options(args):
159178
160179 for op , arg in ops :
161180 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 )
169190
170191 user , passwd = getpw (options .user , passfd , passfile )
171192 options .authstr = mkauthstr (user , passwd )
0 commit comments