Skip to content

Commit

Permalink
Rate limit inverter controller to every 1 second
Browse files Browse the repository at this point in the history
Prevent DDOSing inverter
  • Loading branch information
longzheng committed Sep 5, 2024
1 parent bc92c5c commit 3e14ce7
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/coordinator/helpers/inverterController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,20 @@ export class InverterController {
this.inverterConnections = invertersConnections;
this.rampRateHelper = rampRateHelper;
this.limiters = limiters;

void this.startLoop();
}

updateSunSpecInverterData(data: SunSpecInverterData) {
this.logger.debug('Received inverter data, updating inverter controls');
this.cachedSunSpecData = data;

void this.updateInverterControlValues();
}

updateSiteMonitoringSample(siteMonitoringSample: SiteMonitoringSample) {
this.logger.debug(
'Received site monitoring sample, updating inverter controls',
);
this.cachedSiteMonitoringSample = siteMonitoringSample;

void this.updateInverterControlValues();
}

private getActiveInverterControlLimit(): InverterControlLimit {
Expand All @@ -116,6 +114,29 @@ export class InverterController {
return getAggregatedInverterControlLimit(controlLimits);
}

private async startLoop() {
const start = performance.now();

try {
await this.updateInverterControlValues();
} catch (error) {
this.logger.error(
{ error },
'Failed to push inverter control values',
);
} finally {
const end = performance.now();
const duration = end - start;

// update the inverter at most every 1 second
const delay = Math.max(1000 - duration, 0);

setTimeout(() => {
void this.startLoop();
}, delay);
}
}

private async updateInverterControlValues() {
if (!this.cachedSunSpecData) {
this.logger.warn(
Expand Down

0 comments on commit 3e14ce7

Please sign in to comment.