Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: remove individual bids from cache when minBidCacheTTL is set #12535

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/auction.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ import {defer, GreedyPromise} from './utils/promise.js';
import {useMetrics} from './utils/perfMetrics.js';
import {adjustCpm} from './utils/cpm.js';
import {getGlobal} from './prebidGlobal.js';
import {ttlCollection} from './utils/ttlCollection.js';
import {getMinBidCacheTTL, onMinBidCacheTTLChange} from './bidTTL.js';

const { syncUsers } = userSync;

Expand Down Expand Up @@ -153,7 +155,10 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels, a
let _bidsRejected = [];
let _callback = callback;
let _bidderRequests = [];
let _bidsReceived = [];
let _bidsReceived = ttlCollection({
startTime: (bid) => bid.responseTimestamp,
ttl: (bid) => getMinBidCacheTTL() == null ? null : Math.max(getMinBidCacheTTL(), bid.ttl) * 1000
});
let _noBids = [];
let _winningBids = [];
let _auctionStart;
Expand All @@ -162,8 +167,10 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels, a
let _auctionStatus;
let _nonBids = [];

onMinBidCacheTTLChange(() => _bidsReceived.refresh());

function addBidRequests(bidderRequests) { _bidderRequests = _bidderRequests.concat(bidderRequests); }
function addBidReceived(bidsReceived) { _bidsReceived = _bidsReceived.concat(bidsReceived); }
function addBidReceived(bid) { _bidsReceived.add(bid); }
function addBidRejected(bidsRejected) { _bidsRejected = _bidsRejected.concat(bidsRejected); }
function addNoBid(noBid) { _noBids = _noBids.concat(noBid); }
function addNonBids(seatnonbids) { _nonBids = _nonBids.concat(seatnonbids); }
Expand All @@ -179,7 +186,7 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels, a
labels: _labels,
bidderRequests: _bidderRequests,
noBids: _noBids,
bidsReceived: _bidsReceived,
bidsReceived: _bidsReceived.toArray(),
bidsRejected: _bidsRejected,
winningBids: _winningBids,
timeout: _timeout,
Expand Down Expand Up @@ -219,7 +226,7 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels, a
bidsBackCallback(_adUnits, function () {
try {
if (_callback != null) {
const bids = _bidsReceived
const bids = _bidsReceived.toArray()
.filter(bid => _adUnitCodes.includes(bid.adUnitCode))
.reduce(groupByPlacement, {});
_callback.apply(pbjsInstance, [bids, timedOut, _auctionId]);
Expand All @@ -246,7 +253,7 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels, a
function auctionDone() {
config.resetBidder();
// when all bidders have called done callback atleast once it means auction is complete
logInfo(`Bids Received for Auction with id: ${_auctionId}`, _bidsReceived);
logInfo(`Bids Received for Auction with id: ${_auctionId}`, _bidsReceived.toArray());
_auctionStatus = AUCTION_COMPLETED;
executeCallback(false);
}
Expand Down Expand Up @@ -404,7 +411,7 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels, a
getAdUnits: () => _adUnits,
getAdUnitCodes: () => _adUnitCodes,
getBidRequests: () => _bidderRequests,
getBidsReceived: () => _bidsReceived,
getBidsReceived: () => _bidsReceived.toArray(),
getNoBids: () => _noBids,
getNonBids: () => _nonBids,
getFPD: () => ortb2Fragments,
Expand Down
24 changes: 4 additions & 20 deletions src/auctionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ import {AuctionIndex} from './auctionIndex.js';
import { BID_STATUS, JSON_MAPPING } from './constants.js';
import {useMetrics} from './utils/perfMetrics.js';
import {ttlCollection} from './utils/ttlCollection.js';
import {getTTL, onTTLBufferChange} from './bidTTL.js';
import {config} from './config.js';

const CACHE_TTL_SETTING = 'minBidCacheTTL';
import {getMinBidCacheTTL, onMinBidCacheTTLChange} from './bidTTL.js';

/**
* Creates new instance of auctionManager. There will only be one instance of auctionManager but
Expand All @@ -38,27 +35,14 @@ const CACHE_TTL_SETTING = 'minBidCacheTTL';
* @returns {AuctionManager} auctionManagerInstance
*/
export function newAuctionManager() {
let minCacheTTL = null;

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

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

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();
}
})
onMinBidCacheTTLChange(() => _auctions.refresh());

const auctionManager = {
onExpiry: _auctions.onExpiry
Expand Down
24 changes: 17 additions & 7 deletions src/bidTTL.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
import {config} from './config.js';
import {logError} from './utils.js';
const CACHE_TTL_SETTING = 'minBidCacheTTL';
let TTL_BUFFER = 1;

let minCacheTTL = null;
const listeners = [];

config.getConfig('ttlBuffer', (cfg) => {
if (typeof cfg.ttlBuffer === 'number') {
const prev = TTL_BUFFER;
TTL_BUFFER = cfg.ttlBuffer;
if (prev !== TTL_BUFFER) {
listeners.forEach(l => l(TTL_BUFFER))
}
} else {
logError('Invalid value for ttlBuffer', cfg.ttlBuffer);
}
})

export function getTTL(bid) {
export function getBufferedTTL(bid) {
return bid.ttl - (bid.hasOwnProperty('ttlBuffer') ? bid.ttlBuffer : TTL_BUFFER);
}

export function onTTLBufferChange(listener) {
export function getMinBidCacheTTL() {
return minCacheTTL;
}

config.getConfig(CACHE_TTL_SETTING, (cfg) => {
const prev = minCacheTTL;
minCacheTTL = cfg?.[CACHE_TTL_SETTING];
minCacheTTL = typeof minCacheTTL === 'number' ? minCacheTTL : null;
if (prev !== minCacheTTL) {
listeners.forEach(l => l(minCacheTTL))
}
})

export function onMinBidCacheTTLChange(listener) {
listeners.push(listener);
}
4 changes: 2 additions & 2 deletions src/targeting.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { auctionManager } from './auctionManager.js';
import { getTTL } from './bidTTL.js';
import { getBufferedTTL } from './bidTTL.js';
import { bidderSettings } from './bidderSettings.js';
import { config } from './config.js';
import {
Expand Down Expand Up @@ -48,7 +48,7 @@ export const TARGETING_KEYS_ARR = Object.keys(TARGETING_KEYS).map(
);

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

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