Skip to content

Commit 73b9363

Browse files
dgirardikiho-shige
authored andcommitted
Core: remove individual bids from cache when minBidCacheTTL is set (prebid#12535)
1 parent 6dee0e4 commit 73b9363

File tree

5 files changed

+163
-103
lines changed

5 files changed

+163
-103
lines changed

src/auction.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ import {defer, GreedyPromise} from './utils/promise.js';
9898
import {useMetrics} from './utils/perfMetrics.js';
9999
import {adjustCpm} from './utils/cpm.js';
100100
import {getGlobal} from './prebidGlobal.js';
101+
import {ttlCollection} from './utils/ttlCollection.js';
102+
import {getMinBidCacheTTL, onMinBidCacheTTLChange} from './bidTTL.js';
101103

102104
const { syncUsers } = userSync;
103105

@@ -153,7 +155,10 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels, a
153155
let _bidsRejected = [];
154156
let _callback = callback;
155157
let _bidderRequests = [];
156-
let _bidsReceived = [];
158+
let _bidsReceived = ttlCollection({
159+
startTime: (bid) => bid.responseTimestamp,
160+
ttl: (bid) => getMinBidCacheTTL() == null ? null : Math.max(getMinBidCacheTTL(), bid.ttl) * 1000
161+
});
157162
let _noBids = [];
158163
let _winningBids = [];
159164
let _auctionStart;
@@ -162,8 +167,10 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels, a
162167
let _auctionStatus;
163168
let _nonBids = [];
164169

170+
onMinBidCacheTTLChange(() => _bidsReceived.refresh());
171+
165172
function addBidRequests(bidderRequests) { _bidderRequests = _bidderRequests.concat(bidderRequests); }
166-
function addBidReceived(bidsReceived) { _bidsReceived = _bidsReceived.concat(bidsReceived); }
173+
function addBidReceived(bid) { _bidsReceived.add(bid); }
167174
function addBidRejected(bidsRejected) { _bidsRejected = _bidsRejected.concat(bidsRejected); }
168175
function addNoBid(noBid) { _noBids = _noBids.concat(noBid); }
169176
function addNonBids(seatnonbids) { _nonBids = _nonBids.concat(seatnonbids); }
@@ -179,7 +186,7 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels, a
179186
labels: _labels,
180187
bidderRequests: _bidderRequests,
181188
noBids: _noBids,
182-
bidsReceived: _bidsReceived,
189+
bidsReceived: _bidsReceived.toArray(),
183190
bidsRejected: _bidsRejected,
184191
winningBids: _winningBids,
185192
timeout: _timeout,
@@ -219,7 +226,7 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels, a
219226
bidsBackCallback(_adUnits, function () {
220227
try {
221228
if (_callback != null) {
222-
const bids = _bidsReceived
229+
const bids = _bidsReceived.toArray()
223230
.filter(bid => _adUnitCodes.includes(bid.adUnitCode))
224231
.reduce(groupByPlacement, {});
225232
_callback.apply(pbjsInstance, [bids, timedOut, _auctionId]);
@@ -246,7 +253,7 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels, a
246253
function auctionDone() {
247254
config.resetBidder();
248255
// when all bidders have called done callback atleast once it means auction is complete
249-
logInfo(`Bids Received for Auction with id: ${_auctionId}`, _bidsReceived);
256+
logInfo(`Bids Received for Auction with id: ${_auctionId}`, _bidsReceived.toArray());
250257
_auctionStatus = AUCTION_COMPLETED;
251258
executeCallback(false);
252259
}
@@ -404,7 +411,7 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels, a
404411
getAdUnits: () => _adUnits,
405412
getAdUnitCodes: () => _adUnitCodes,
406413
getBidRequests: () => _bidderRequests,
407-
getBidsReceived: () => _bidsReceived,
414+
getBidsReceived: () => _bidsReceived.toArray(),
408415
getNoBids: () => _noBids,
409416
getNonBids: () => _nonBids,
410417
getFPD: () => ortb2Fragments,

src/auctionManager.js

+4-20
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ import {AuctionIndex} from './auctionIndex.js';
2626
import { BID_STATUS, JSON_MAPPING } from './constants.js';
2727
import {useMetrics} from './utils/perfMetrics.js';
2828
import {ttlCollection} from './utils/ttlCollection.js';
29-
import {getTTL, onTTLBufferChange} from './bidTTL.js';
30-
import {config} from './config.js';
31-
32-
const CACHE_TTL_SETTING = 'minBidCacheTTL';
29+
import {getMinBidCacheTTL, onMinBidCacheTTLChange} from './bidTTL.js';
3330

3431
/**
3532
* Creates new instance of auctionManager. There will only be one instance of auctionManager but
@@ -38,27 +35,14 @@ const CACHE_TTL_SETTING = 'minBidCacheTTL';
3835
* @returns {AuctionManager} auctionManagerInstance
3936
*/
4037
export function newAuctionManager() {
41-
let minCacheTTL = null;
42-
4338
const _auctions = ttlCollection({
4439
startTime: (au) => au.end.then(() => au.getAuctionEnd()),
45-
ttl: (au) => minCacheTTL == null ? null : au.end.then(() => {
46-
return Math.max(minCacheTTL, ...au.getBidsReceived().map(getTTL)) * 1000
40+
ttl: (au) => getMinBidCacheTTL() == null ? null : au.end.then(() => {
41+
return Math.max(getMinBidCacheTTL(), ...au.getBidsReceived().map(bid => bid.ttl)) * 1000
4742
}),
4843
});
4944

50-
onTTLBufferChange(() => {
51-
if (minCacheTTL != null) _auctions.refresh();
52-
})
53-
54-
config.getConfig(CACHE_TTL_SETTING, (cfg) => {
55-
const prev = minCacheTTL;
56-
minCacheTTL = cfg?.[CACHE_TTL_SETTING];
57-
minCacheTTL = typeof minCacheTTL === 'number' ? minCacheTTL : null;
58-
if (prev !== minCacheTTL) {
59-
_auctions.refresh();
60-
}
61-
})
45+
onMinBidCacheTTLChange(() => _auctions.refresh());
6246

6347
const auctionManager = {
6448
onExpiry: _auctions.onExpiry

src/bidTTL.js

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
11
import {config} from './config.js';
22
import {logError} from './utils.js';
3+
const CACHE_TTL_SETTING = 'minBidCacheTTL';
34
let TTL_BUFFER = 1;
4-
5+
let minCacheTTL = null;
56
const listeners = [];
67

78
config.getConfig('ttlBuffer', (cfg) => {
89
if (typeof cfg.ttlBuffer === 'number') {
9-
const prev = TTL_BUFFER;
1010
TTL_BUFFER = cfg.ttlBuffer;
11-
if (prev !== TTL_BUFFER) {
12-
listeners.forEach(l => l(TTL_BUFFER))
13-
}
1411
} else {
1512
logError('Invalid value for ttlBuffer', cfg.ttlBuffer);
1613
}
1714
})
1815

19-
export function getTTL(bid) {
16+
export function getBufferedTTL(bid) {
2017
return bid.ttl - (bid.hasOwnProperty('ttlBuffer') ? bid.ttlBuffer : TTL_BUFFER);
2118
}
2219

23-
export function onTTLBufferChange(listener) {
20+
export function getMinBidCacheTTL() {
21+
return minCacheTTL;
22+
}
23+
24+
config.getConfig(CACHE_TTL_SETTING, (cfg) => {
25+
const prev = minCacheTTL;
26+
minCacheTTL = cfg?.[CACHE_TTL_SETTING];
27+
minCacheTTL = typeof minCacheTTL === 'number' ? minCacheTTL : null;
28+
if (prev !== minCacheTTL) {
29+
listeners.forEach(l => l(minCacheTTL))
30+
}
31+
})
32+
33+
export function onMinBidCacheTTLChange(listener) {
2434
listeners.push(listener);
2535
}

src/targeting.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { auctionManager } from './auctionManager.js';
2-
import { getTTL } from './bidTTL.js';
2+
import { getBufferedTTL } from './bidTTL.js';
33
import { bidderSettings } from './bidderSettings.js';
44
import { config } from './config.js';
55
import {
@@ -48,7 +48,7 @@ export const TARGETING_KEYS_ARR = Object.keys(TARGETING_KEYS).map(
4848
);
4949

5050
// return unexpired bids
51-
const isBidNotExpired = (bid) => (bid.responseTimestamp + getTTL(bid) * 1000) > timestamp();
51+
const isBidNotExpired = (bid) => (bid.responseTimestamp + getBufferedTTL(bid) * 1000) > timestamp();
5252

5353
// return bids whose status is not set. Winning bids can only have a status of `rendered`.
5454
const isUnusedBid = (bid) => bid && ((bid.status && !includes([BID_STATUS.RENDERED], bid.status)) || !bid.status);

0 commit comments

Comments
 (0)