Skip to content

Commit 0f46d49

Browse files
committed
debounce instead of batch; check if identity state chagned before
emitting
1 parent bb09b28 commit 0f46d49

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

packages/unraid-api-plugin-connect/src/mothership-proxy/connection.service.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { ConfigService } from '@nestjs/config';
33
import { EventEmitter2 } from '@nestjs/event-emitter';
44
import type { OutgoingHttpHeaders } from 'node:http2';
55

6+
import { isEqual } from 'lodash-es';
67
import { Subscription } from 'rxjs';
7-
import { bufferTime, filter } from 'rxjs/operators';
8+
import { debounceTime, filter } from 'rxjs/operators';
89

910
import { ConnectionMetadata, MinigraphStatus, MyServersConfig } from '../config/connect.config.js';
1011
import { EVENTS } from '../helper/nest-tokens.js';
@@ -58,6 +59,7 @@ export class MothershipConnectionService implements OnModuleInit, OnModuleDestro
5859
};
5960

6061
private identitySubscription: Subscription | null = null;
62+
private lastIdentity: Partial<IdentityState> | null = null;
6163
private metadataChangedSubscription: Subscription | null = null;
6264

6365
constructor(
@@ -83,12 +85,22 @@ export class MothershipConnectionService implements OnModuleInit, OnModuleDestro
8385
this.identitySubscription = this.configService.changes$
8486
.pipe(
8587
filter((change) => Object.values(this.configKeys).includes(change.path)),
86-
bufferTime(25)
88+
// debouncing is necessary here (instead of buffering/batching) to prevent excess emissions
89+
// because the store.* config values will change frequently upon api boot
90+
debounceTime(25)
8791
)
8892
.subscribe({
8993
next: () => {
94+
const { state } = this.getIdentityState();
95+
if (isEqual(state, this.lastIdentity)) {
96+
this.logger.debug('Identity unchanged; skipping event emission');
97+
return;
98+
}
99+
this.lastIdentity = structuredClone(state);
90100
const success = this.eventEmitter.emit(EVENTS.IDENTITY_CHANGED);
91-
if (!success) {
101+
if (success) {
102+
this.logger.debug('Emitted IDENTITY_CHANGED event');
103+
} else {
92104
this.logger.warn('Failed to emit IDENTITY_CHANGED event');
93105
}
94106
},

packages/unraid-api-plugin-connect/src/mothership-proxy/mothership.events.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { isEqual } from 'lodash-es';
1515
export class MothershipHandler implements OnModuleDestroy {
1616
private readonly logger = new Logger(MothershipHandler.name);
1717
private isSettingUp = false;
18-
private lastIdentityHash: object | null = null;
1918
constructor(
2019
private readonly connectionService: MothershipConnectionService,
2120
private readonly clientService: MothershipGraphqlClientService,
@@ -38,10 +37,10 @@ export class MothershipHandler implements OnModuleDestroy {
3837
}
3938

4039
async setup() {
41-
if (this.isSettingUp) {
42-
this.logger.debug('Setup already in progress, skipping');
43-
return;
44-
}
40+
// if (this.isSettingUp) {
41+
// this.logger.debug('Setup already in progress, skipping');
42+
// return;
43+
// }
4544
this.isSettingUp = true;
4645
try {
4746
await this.clear();
@@ -51,9 +50,6 @@ export class MothershipHandler implements OnModuleDestroy {
5150
this.logger.warn('No API key found; cannot setup mothership subscription');
5251
return;
5352
}
54-
// cache identity snapshot to avoid redundant setups
55-
this.lastIdentityHash = structuredClone(state);
56-
5753
await this.clientService.createClientInstance();
5854
await this.subscriptionHandler.subscribeToMothershipEvents();
5955
this.timeoutCheckerJob.start();
@@ -66,12 +62,6 @@ export class MothershipHandler implements OnModuleDestroy {
6662
async onIdentityChanged() {
6763
const { state } = this.connectionService.getIdentityState();
6864
if (state.apiKey) {
69-
const identityHash = structuredClone(state);
70-
71-
if (isEqual(identityHash, this.lastIdentityHash)) {
72-
this.logger.debug('Identity unchanged; skipping mothership setup');
73-
return;
74-
}
7565
this.logger.verbose('Identity changed; setting up mothership subscription');
7666
await this.setup();
7767
}

0 commit comments

Comments
 (0)