Skip to content

Commit

Permalink
[2.0.44] Fixed promise retries on 404s (#7)
Browse files Browse the repository at this point in the history
* Fixing promise retries on 404s

* Version bump

* README updates
  • Loading branch information
cnguy committed May 14, 2017
1 parent e58830b commit 3fd4ac7
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -30,7 +30,7 @@ Check out [SUMMONER-V3](https://github.com/ChauTNguyen/kindred-api/wiki/SUMMONER
* All standard endpoints covered but tournament endpoints.
* Supports both **callbacks** and **promises**.
* **Burst/Spread** rate limiter that is **enforced per region** and **follows retry headers**.
* Retries on 429 and >= 500. (Doesn't retry on 404)
* **Retries** on 429 and >= 500 **until all calls are successful**. (Doesn't retry on 404)
* Built-in **parameter checks** so you can hopefully refer to documentation less! :)
* Built-in **caching** (in-memory and Redis).
* **Customized expiration timers**. You can set a timer for each endpoint type. Refer to [Caching](https://github.com/ChauTNguyen/kindred-api/wiki/Caching) for more info.
Expand Down Expand Up @@ -278,7 +278,7 @@ The difference between my site and other applications people seem to be working

## ~~Rate Limiter is not as optimized as it should be.~~ (FIXED 2.0.33)

## Promises retry on 404.
## ~~Promises retry on 404.~~ (FIXED 2.0.43)

This is problematic because certain calls such as getCurrentGame, which will hit 404's often, will always retry up to 3 times.

Expand Down
13 changes: 7 additions & 6 deletions dist/kindred-api.js
Expand Up @@ -1011,16 +1011,21 @@
} else {
if (statusCode >= 500) {
if (self.debug) console.log('!!! resending promise request !!!');
setTimeout(function () {
(function () {
return resolve(tryRequest());
});
}, 1000);
setTimeout(function () {
return reject('retry');
}, 1000);
} else if (statusCode === 429) {
if (self.debug) console.log('!!! resending promise request !!!');
setTimeout(function () {
return reject('retry');
return resolve(tryRequest());
}, response.headers['retry-after'] * 1000 + 50);
} else if (error || statusCode >= 400) {
return reject('err:', error, statusCode);
return reject(statusMessage + ' : ' + chalk.yellow(reqUrl));
} else {
if (Number.isInteger(cacheParams.ttl) && cacheParams.ttl > 0) self.cache.set({ key: reqUrl, ttl: cacheParams.ttl }, body);
return resolve(JSON.parse(body));
Expand Down Expand Up @@ -1062,10 +1067,6 @@
});
};

if (!cb) return tryRequest().catch(tryRequest).catch(tryRequest).catch(tryRequest).then(function (data) {
return data;
});

return tryRequest();
}
}, {
Expand Down
4 changes: 2 additions & 2 deletions dist/kindred-api.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "kindred-api",
"version": "2.0.43",
"version": "2.0.44",
"description": "Node.js League of Legends v3 API wrapper with built-in rate-limiting (burst/spread, enforced per region), caching (in-memory, Redis), and parameter checking.",
"main": "index.js",
"jsnext:main": "src/index.js",
Expand Down
13 changes: 7 additions & 6 deletions src/kindred-api.js
Expand Up @@ -554,12 +554,17 @@ class Kindred {
} else {
if (statusCode >= 500) {
if (self.debug) console.log('!!! resending promise request !!!')
setTimeout(() => {
() => resolve(tryRequest())
}, 1000)
setTimeout(() => { return reject('retry') }, 1000)
} else if (statusCode === 429) {
if (self.debug) console.log('!!! resending promise request !!!')
setTimeout(() => { return reject('retry') }, (response.headers['retry-after'] * 1000) + 50)
setTimeout(() => {
return resolve(tryRequest())
}, (response.headers['retry-after'] * 1000) + 50)
} else if (error || statusCode >= 400) {
return reject('err:', error, statusCode)
return reject(statusMessage + ' : ' + chalk.yellow(reqUrl))
} else {
if (Number.isInteger(cacheParams.ttl) && cacheParams.ttl > 0)
self.cache.set({ key: reqUrl, ttl: cacheParams.ttl }, body)
Expand Down Expand Up @@ -605,10 +610,6 @@ class Kindred {
})
}

if (!cb) return tryRequest()
.catch(tryRequest).catch(tryRequest).catch(tryRequest)
.then(data => data)

return tryRequest()
}

Expand Down

0 comments on commit 3fd4ac7

Please sign in to comment.