Skip to content

Commit 78b2932

Browse files
committed
dedupe when queuing assignments as well
1 parent 8bc8abc commit 78b2932

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

src/client/eppo-client-with-bandits.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ describe('EppoClient Bandits E2E test', () => {
205205
});
206206

207207
it('Flushed queued logging events when a logger is set', () => {
208+
client.useLRUInMemoryAssignmentCache(5);
209+
client.useLRUInMemoryBanditAssignmentCache(5);
208210
client.setAssignmentLogger(null as unknown as IAssignmentLogger);
209211
client.setBanditLogger(null as unknown as IBanditLogger);
210212
const banditAssignment = client.getBanditAction(
@@ -221,6 +223,20 @@ describe('EppoClient Bandits E2E test', () => {
221223
expect(mockLogAssignment).not.toHaveBeenCalled();
222224
expect(mockLogBanditAction).not.toHaveBeenCalled();
223225

226+
const repeatAssignment = client.getBanditAction(
227+
flagKey,
228+
subjectKey,
229+
subjectAttributes,
230+
actions,
231+
'control',
232+
);
233+
234+
expect(repeatAssignment.variation).toBe('banner_bandit');
235+
expect(repeatAssignment.action).toBe('adidas');
236+
237+
expect(mockLogAssignment).not.toHaveBeenCalled();
238+
expect(mockLogBanditAction).not.toHaveBeenCalled();
239+
224240
client.setAssignmentLogger({ logAssignment: mockLogAssignment });
225241
client.setBanditLogger({ logBanditAction: mockLogBanditAction });
226242

src/client/eppo-client.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -670,18 +670,15 @@ export default class EppoClient {
670670
return;
671671
}
672672

673-
// If no logger defined, queue up the events (up to a max) to flush if a logger is later defined
674-
if (!this.banditLogger) {
675-
// No bandit logger set; enqueue the event in case a logger is later set
676-
if (this.queuedBanditEvents.length < MAX_EVENT_QUEUE_SIZE) {
677-
this.queuedBanditEvents.push(banditEvent);
678-
}
679-
return;
680-
}
681-
682673
// If here, we have a logger and a new assignment to be logged
683674
try {
684-
this.banditLogger.logBanditAction(banditEvent);
675+
if (this.banditLogger) {
676+
this.banditLogger.logBanditAction(banditEvent);
677+
} else if (this.queuedBanditEvents.length < MAX_EVENT_QUEUE_SIZE) {
678+
// If no logger defined, queue up the events (up to a max) to flush if a logger is later defined
679+
this.queuedBanditEvents.push(banditEvent);
680+
}
681+
// Record in the assignment cache, if active, to deduplicate subsequent repeat assignments
685682
this.banditAssignmentCache?.set(banditAssignmentCacheProperties);
686683
} catch (err) {
687684
logger.warn('Error encountered logging bandit action', err);
@@ -994,14 +991,14 @@ export default class EppoClient {
994991
}
995992
}
996993

997-
// assignment logger may be null while waiting for initialization
998-
if (!this.assignmentLogger) {
999-
this.queuedAssignmentEvents.length < MAX_EVENT_QUEUE_SIZE &&
1000-
this.queuedAssignmentEvents.push(event);
1001-
return;
1002-
}
1003994
try {
1004-
this.assignmentLogger.logAssignment(event);
995+
if (this.assignmentLogger) {
996+
this.assignmentLogger.logAssignment(event);
997+
} else if (this.queuedAssignmentEvents.length < MAX_EVENT_QUEUE_SIZE) {
998+
// assignment logger may be null while waiting for initialization, queue up events (up to a max)
999+
// to be flushed when set
1000+
this.queuedAssignmentEvents.push(event);
1001+
}
10051002
this.assignmentCache?.set({
10061003
flagKey,
10071004
subjectKey,

0 commit comments

Comments
 (0)