Skip to content

Commit

Permalink
minBidCacheTTL instead of preserveBidCache
Browse files Browse the repository at this point in the history
  • Loading branch information
dgirardi committed Aug 10, 2023
1 parent ce0a832 commit 214a9be
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
21 changes: 10 additions & 11 deletions src/auctionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {ttlCollection} from './utils/ttlCollection.js';
import {getTTL, onTTLBufferChange} from './bidTTL.js';
import {config} from './config.js';

const HISTORY_SETTING = 'preserveBidCache';
const CACHE_TTL_SETTING = 'minBidCacheTTL';

/**
* Creates new instance of auctionManager. There will only be one instance of auctionManager but
Expand All @@ -37,25 +37,24 @@ const HISTORY_SETTING = 'preserveBidCache';
* @returns {AuctionManager} auctionManagerInstance
*/
export function newAuctionManager() {
let staleTTL = null;
let minCacheTTL = null;

const _auctions = ttlCollection({
startTime: (au) => au.end.then(() => au.getAuctionEnd()),
ttl: (au) => staleTTL == null ? null : au.end.then(() => {
const bidTTLs = au.getBidsReceived().map(getTTL);
return (bidTTLs.length > 0 ? Math.max(...bidTTLs) : 0) * 1000 + staleTTL
ttl: (au) => minCacheTTL == null ? null : au.end.then(() => {
return Math.max(minCacheTTL, ...au.getBidsReceived().map(getTTL)) * 1000
}),
});

onTTLBufferChange(() => {
if (staleTTL != null) _auctions.refresh();
if (minCacheTTL != null) _auctions.refresh();
})

config.getConfig(HISTORY_SETTING, (cfg) => {
const prev = staleTTL;
staleTTL = cfg?.[HISTORY_SETTING];
staleTTL = typeof staleTTL === 'number' ? staleTTL * 1000 : null;
if (prev !== staleTTL) {
config.getConfig(CACHE_TTL_SETTING, (cfg) => {
const prev = minCacheTTL;
minCacheTTL = cfg?.[CACHE_TTL_SETTING];
minCacheTTL = typeof minCacheTTL === 'number' ? minCacheTTL : null;
if (prev !== minCacheTTL) {
_auctions.refresh();
}
})
Expand Down
38 changes: 30 additions & 8 deletions test/spec/auctionmanager_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -830,9 +830,9 @@ describe('auctionmanager.js', function () {
config.resetConfig();
});

it('are dropped after `preserveBidCache` seconds have passed since their last bid becomes stale', () => {
it('are dropped after their last bid becomes stale (if minBidCacheTTL is set)', () => {
config.setConfig({
preserveBidCache: 10
minBidCacheTTL: 0
});
bids = [
{
Expand All @@ -849,24 +849,46 @@ describe('auctionmanager.js', function () {
return auction.end.then(() => {
clock.tick(50 * 1000);
expect(auctionManager.getBidsReceived().length).to.equal(2);
clock.tick(60 * 1000);
expect(auctionManager.getBidsReceived().length).to.equal(2);
clock.tick(5 * 1000);
clock.tick(56 * 1000);
expect(auctionManager.getBidsReceived()).to.eql([]);
});
});

it('are dropped after `preserveBidCache` seconds if they had no bid', () => {
it('are dropped after `minBidCacheTTL` seconds if they had no bid', () => {
auction.callBids();
config.setConfig({
preserveBidCache: 2
minBidCacheTTL: 2
});
return auction.end.then(() => {
expect(auctionManager.getNoBids().length).to.eql(1);
clock.tick(10 * 10000);
expect(auctionManager.getNoBids().length).to.eql(0);
})
})
});

Object.entries({
'bids': {
bd: [{
adUnitCode: ADUNIT_CODE,
transactionId: ADUNIT_CODE,
ttl: 10
}],
entries: () => auctionManager.getBidsReceived()
},
'no bids': {
bd: [],
entries: () => auctionManager.getNoBids()
}
}).forEach(([t, {bd, entries}]) => {
it(`with ${t} are never dropped if minBidCacheTTL is not set`, () => {
bids = bd;
auction.callBids();
return auction.end.then(() => {
clock.tick(100 * 1000);
expect(entries().length > 0).to.be.true;
})
})
});
})
});

Expand Down

0 comments on commit 214a9be

Please sign in to comment.