Skip to content

Commit

Permalink
Expire stale bids directly from auction object to prevent long TTL bi…
Browse files Browse the repository at this point in the history
…ds from exploding memory
  • Loading branch information
fowler446 committed Jun 12, 2024
1 parent 236a8ad commit 33a7ced
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 43 deletions.
8 changes: 7 additions & 1 deletion src/auction.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,17 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels, a
let _nonBids = [];

function addBidRequests(bidderRequests) { _bidderRequests = _bidderRequests.concat(bidderRequests); }
function addBidReceived(bidsReceived) { _bidsReceived = _bidsReceived.concat(bidsReceived); }
function addBidReceived(bidResponse) { _bidsReceived = _bidsReceived.concat(bidResponse); purgeBidOnExpiration(bidResponse); }
function addBidRejected(bidsRejected) { _bidsRejected = _bidsRejected.concat(bidsRejected); }
function addNoBid(noBid) { _noBids = _noBids.concat(noBid); }
function addNonBids(seatnonbids) { _nonBids = _nonBids.concat(seatnonbids); }

function purgeBidOnExpiration(bidResponse) {
setTimeout(() => {
_bidsReceived = _bidsReceived.filter(bid => bid.adId !== bidResponse.adId);
}, bidResponse.ttl * 1000);
}

function getProperties() {
return {
auctionId: _auctionId,
Expand Down
72 changes: 30 additions & 42 deletions test/spec/auctionmanager_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -852,66 +852,54 @@ describe('auctionmanager.js', function () {
config.resetConfig();
});

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

describe('stale bids', () => {
let clock, auction;
beforeEach(() => {
clock = sinon.useFakeTimers();
auction = auctionManager.createAuction({adUnits});
indexAuctions.push(auction);
});
afterEach(() => {
clock.restore();
config.resetConfig();
});

it('are dropped after they expire', () => {
bids = [
{
adId: '1',
adUnitCode: ADUNIT_CODE,
adUnitId: ADUNIT_CODE,
ttl: 10
}, {
adId: '2',
adUnitCode: ADUNIT_CODE,
adUnitId: ADUNIT_CODE,
ttl: 100
}
];
auction.callBids();
return auction.end.then(() => {
clock.tick(50 * 1000);
expect(auctionManager.getBidsReceived().length).to.equal(2);
clock.tick(56 * 1000);
clock.tick(10 * 1000);
expect(auctionManager.getBidsReceived().length).to.equal(1);
clock.tick(90 * 1000);
expect(auctionManager.getBidsReceived()).to.eql([]);
});
});

it('are dropped after `minBidCacheTTL` seconds if they had no bid', () => {
auction.callBids();
config.setConfig({
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,
adUnitId: 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;
})
})
});
})
});
});

describe('addBidResponse #1', function () {
Expand Down

0 comments on commit 33a7ced

Please sign in to comment.