Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix timeout error by rate-limiting HTTP requests #254

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ It represents the closest reasonable ESLint configuration to this
project's original TSLint configuration.

We recommend eventually switching this configuration to extend from
the recommended rulesets in typescript-eslint.
the recommended rulesets in typescript-eslint.
https://github.com/typescript-eslint/tslint-to-eslint-config/blob/master/docs/FAQs.md

Happy linting! 💖
Expand All @@ -18,10 +18,10 @@ Happy linting! 💖
"eslint.format.enable": true,
"files.eol": "\n",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"editor.rulers": [
140
],
"eslint.enable": true
}
}
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
[![npm](https://badgen.net/npm/v/homebridge-bond)](https://www.npmjs.com/package/homebridge-bond)
[![npm](https://badgen.net/npm/dt/homebridge-bond)](https://www.npmjs.com/package/homebridge-bond)

# homebridge-bond
# homebridge-bond-searls

**This is a fork of [homebridge-bond](https://github.com/aarons22/homebridge-bond) that I made with some minor rate limit changes that were necessary to get it to work on my
network. You probably don't want to use it.**

Bond plug-in for [Homebridge](https://github.com/nfarina/homebridge) using the [Bond V2 API](http://docs-local.appbond.com).

Expand Down
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "homebridge-bond",
"version": "3.2.11-beta.2",
"description": "A homebridge plugin to control your Bond devices over the v2 API.",
"name": "homebridge-bond-searls",
"version": "30.2.11",
"description": "An experimental fork by @searls of homebridge plugin to control your Bond devices over the v2 API.",
"license": "MIT",
"main": "dist/index.js",
"scripts": {
Expand All @@ -22,10 +22,10 @@
],
"repository": {
"type": "git",
"url": "git://github.com/aarons22/homebridge-bond.git"
"url": "git://github.com/searls/homebridge-bond.git"
},
"bugs": {
"url": "http://github.com/aarons22/homebridge-bond/issues"
"url": "http://github.com/searls/homebridge-bond/issues"
},
"engines": {
"node": ">=10.17.0",
Expand All @@ -51,9 +51,10 @@
"dependencies": {
"axios": "^0.21.4",
"axios-retry": "^3.1.8",
"axios-rate-limit": "^1.3.0",
"biguint-format": "^1.0.2",
"flake-idgen": "^1.4.0"
},
"homepage": "https://github.com/aarons22/homebridge-bond#readme",
"author": "Aaron Sapp"
}
"homepage": "https://github.com/searls/homebridge-bond#readme",
"author": "Justin Searls <[email protected]>"
}
27 changes: 7 additions & 20 deletions src/BondApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Properties } from './interface/Properties';
import { Version } from './interface/Version';
import axios, { AxiosError } from 'axios';
import axiosRetry from 'axios-retry';
import axiosRateLimit, { RateLimitedAxiosInstance } from 'axios-rate-limit';
import FlakeId from 'flake-idgen';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const intformat = require('biguint-format');
Expand All @@ -23,31 +24,17 @@ const flakeIdGen = new FlakeId();
export class BondApi {
private bondToken: string;
private uri: BondUri;
private http: RateLimitedAxiosInstance;

constructor(
private readonly platform: BondPlatform,
bondToken: string,
ipAddress: string) {
this.bondToken = bondToken;
this.uri = new BondUri(ipAddress);
this.http = axiosRateLimit(axios.create(), { maxRequests: 2, perMilliseconds: 1000 });

axiosRetry(axios, {
retries: 10,
retryDelay: axiosRetry.exponentialDelay,
shouldResetTimeout: true,
retryCondition: (error) => {
const shouldRetry = axiosRetry.isNetworkOrIdempotentRequestError(error) || error.code === 'ECONNABORTED';

this.platform.log.debug(`Retrying: ${shouldRetry ? 'YES' : 'NO'}`, {
url: error.config?.url,
method: error.config?.method,
errorCode: error.code,
responseStatus: error.response?.status,
});

return shouldRetry;
},
});
axiosRetry(this.http, { retries: 10, retryDelay: axiosRetry.exponentialDelay });
}

// Bond / Device Info
Expand Down Expand Up @@ -315,7 +302,7 @@ export class BondApi {
ping(): Promise<any> {
const uuid = intformat(flakeIdGen.next(), 'hex', { prefix: '18', padstr: '0', size: 16 }); // avoid duplicate action
const bondUuid = uuid.substring(0, 13) + uuid.substring(15); // remove '00' used for datacenter/worker in flakeIdGen
return axios({
return this.http({
method: HTTPMethod.GET,
url: this.uri.deviceIds(),
headers: {
Expand All @@ -337,7 +324,7 @@ export class BondApi {
this.platform.log.debug(`Request (${bondUuid}) [${method} ${uri}]`);
}

return axios({
return this.http({
method,
url: uri,
headers: {
Expand All @@ -362,7 +349,7 @@ export class BondApi {
this.platform.log.error(`A request error occurred: [status] ${response.status} [statusText] ${response.statusText}`);
}
} else {
this.platform.log.error(`A request error occurred: ${JSON.stringify(error)}`);
this.platform.log.error(`A request error to ${uri} occurred: ${JSON.stringify(error)}`);
}
});
}
Expand Down