Skip to content

Commit

Permalink
dedupe when queuing assignments as well
Browse files Browse the repository at this point in the history
  • Loading branch information
aarsilv committed Aug 27, 2024
1 parent 8bc8abc commit 78b2932
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
16 changes: 16 additions & 0 deletions src/client/eppo-client-with-bandits.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ describe('EppoClient Bandits E2E test', () => {
});

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

const repeatAssignment = client.getBanditAction(
flagKey,
subjectKey,
subjectAttributes,
actions,
'control',
);

expect(repeatAssignment.variation).toBe('banner_bandit');
expect(repeatAssignment.action).toBe('adidas');

expect(mockLogAssignment).not.toHaveBeenCalled();
expect(mockLogBanditAction).not.toHaveBeenCalled();

client.setAssignmentLogger({ logAssignment: mockLogAssignment });
client.setBanditLogger({ logBanditAction: mockLogBanditAction });

Expand Down
31 changes: 14 additions & 17 deletions src/client/eppo-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,18 +670,15 @@ export default class EppoClient {
return;
}

// If no logger defined, queue up the events (up to a max) to flush if a logger is later defined
if (!this.banditLogger) {
// No bandit logger set; enqueue the event in case a logger is later set
if (this.queuedBanditEvents.length < MAX_EVENT_QUEUE_SIZE) {
this.queuedBanditEvents.push(banditEvent);
}
return;
}

// If here, we have a logger and a new assignment to be logged
try {
this.banditLogger.logBanditAction(banditEvent);
if (this.banditLogger) {
this.banditLogger.logBanditAction(banditEvent);
} else if (this.queuedBanditEvents.length < MAX_EVENT_QUEUE_SIZE) {
// If no logger defined, queue up the events (up to a max) to flush if a logger is later defined
this.queuedBanditEvents.push(banditEvent);
}
// Record in the assignment cache, if active, to deduplicate subsequent repeat assignments
this.banditAssignmentCache?.set(banditAssignmentCacheProperties);
} catch (err) {
logger.warn('Error encountered logging bandit action', err);
Expand Down Expand Up @@ -994,14 +991,14 @@ export default class EppoClient {
}
}

// assignment logger may be null while waiting for initialization
if (!this.assignmentLogger) {
this.queuedAssignmentEvents.length < MAX_EVENT_QUEUE_SIZE &&
this.queuedAssignmentEvents.push(event);
return;
}
try {
this.assignmentLogger.logAssignment(event);
if (this.assignmentLogger) {
this.assignmentLogger.logAssignment(event);
} else if (this.queuedAssignmentEvents.length < MAX_EVENT_QUEUE_SIZE) {
// assignment logger may be null while waiting for initialization, queue up events (up to a max)
// to be flushed when set
this.queuedAssignmentEvents.push(event);
}
this.assignmentCache?.set({
flagKey,
subjectKey,
Expand Down

0 comments on commit 78b2932

Please sign in to comment.