Skip to content

Commit 6e0c200

Browse files
Merge pull request #16 from williamnswanson/INF-1310.usermap-script-backoff
Implement usermap exponential backoff (INF-1310)
2 parents 4ceed7f + 587d8a5 commit 6e0c200

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

osg-comanage-project-usermap.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
SCRIPT = os.path.basename(__file__)
1313
ENDPOINT = "https://registry-test.cilogon.org/registry/"
1414
OSG_CO_ID = 8
15+
MINTIMEOUT = 5
16+
MAXTIMEOUT = 625
17+
TIMEOUTMULTIPLE = 5
1518

1619

1720
_usage = f"""\
@@ -26,6 +29,8 @@
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
3136
PASS 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

5562
options = Options()
@@ -87,8 +94,20 @@ def mkrequest(target, **kw):
8794

8895
def 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

148167
def 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

Comments
 (0)