Skip to content

Commit

Permalink
Merge branch 'master' into upgrade-gladys-gateway-js
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Gilles authored Mar 25, 2022
2 parents d96cb5a + aef3afc commit 4f0352e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
13 changes: 12 additions & 1 deletion server/lib/gateway/gateway.forwardDeviceStateToGoogleHome.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
const Bottleneck = require('bottleneck');
const logger = require('../../utils/logger');
const { EVENTS } = require('../../utils/constants');
const { queryDeviceConverter } = require('../../services/google-actions/lib/utils/googleActions.queryDeviceConverter');

// We want to avoid spamming the Gladys Gateway
// So we limit to 2 concurrent calls + max 5 calls/seconds to Gladys Gateway
// @ts-ignore
const limiter = new Bottleneck({
maxConcurrent: 2,
minTime: 200,
});

/**
* @description send a current state to google
* @param {Object} stateManager - The state manager.
Expand Down Expand Up @@ -43,6 +52,8 @@ async function sendCurrentState(stateManager, gladysGatewayClient, deviceFeature
}
}

const sendCurrentStateWithRateLimit = limiter.wrap(sendCurrentState);

/**
* @description Forward websocket message to Gateway.
* @param {Object} event - Websocket event.
Expand All @@ -67,7 +78,7 @@ async function forwardDeviceStateToGoogleHome(event) {
clearTimeout(this.forwardStateToGoogleHomeTimeouts.get(event.device_feature));
}
const newTimeout = setTimeout(() => {
sendCurrentState(this.stateManager, this.gladysGatewayClient, event.device_feature);
sendCurrentStateWithRateLimit(this.stateManager, this.gladysGatewayClient, event.device_feature);
}, this.googleHomeForwardStateTimeout);
this.forwardStateToGoogleHomeTimeouts.set(event.device_feature, newTimeout);
}
Expand Down
5 changes: 5 additions & 0 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"axios": "^0.21.1",
"bcrypt": "^5.0.1",
"bluebird": "^3.5.3",
"bottleneck": "^2.19.5",
"compression": "^1.7.4",
"cross-env": "^5.2.0",
"dayjs": "^1.10.4",
Expand Down

0 comments on commit 4f0352e

Please sign in to comment.