From cf205e59a6f29e447fcda3fb49dedcc76d7b8f41 Mon Sep 17 00:00:00 2001 From: Andrey Kostyuchenko Date: Mon, 25 Jan 2021 10:03:01 +0300 Subject: [PATCH] PDKIO-1761: refactoring to isolate 429 error --- src/authenticators/client.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/authenticators/client.js b/src/authenticators/client.js index 11d667c..02c6e0f 100644 --- a/src/authenticators/client.js +++ b/src/authenticators/client.js @@ -52,17 +52,22 @@ async () => { if(!outstanding) { debug(`Getting token with client credentials`); - outstanding = client.grant({ grant_type: 'client_credentials' }) - + let grantWrapFunc = async () => { + let result = client.grant({ grant_type: 'client_credentials' }) + try { + return await result; + } catch(err) { + if (err && err.statusCode === 429) { + await _sleep(1000); + return grantWrapFunc + } + } + } + outstanding = grantWrapFunc(); try { token_set = await outstanding + } finally { outstanding = undefined - } catch(err) { - outstanding = undefined - if (err && err.statusCode === 429) { - await _sleep(1000); - await oauthtoken_set.refresh(); - } } debug(`Got fresh token: ${JSON.stringify(token_set)}`);