-
Notifications
You must be signed in to change notification settings - Fork 2k
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: improve and fix bid timeout logic #10379
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4028b9a
Clean up timeout logic: doCallbacksIfTimedOut
dgirardi ae4b2e1
Core: simplify bid timeouts, fix s2s timeout edge case
dgirardi e728b1f
Merge branch 'master' into fix-bid-timeouts
dgirardi 5f6c89c
Merge branch 'master' into fix-bid-timeouts
dgirardi 781e69e
use more descriptive varnames
dgirardi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,7 +62,6 @@ import { | |
adUnitsFilter, | ||
bind, | ||
deepAccess, | ||
flatten, | ||
generateUUID, | ||
getValue, | ||
isEmpty, | ||
|
@@ -142,7 +141,7 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels, a | |
const _adUnitCodes = adUnitCodes; | ||
const _auctionId = auctionId || generateUUID(); | ||
const _timeout = cbTimeout; | ||
const _timelyBidders = new Set(); | ||
const _timelyRequests = new Set(); | ||
let _bidsRejected = []; | ||
let _callback = callback; | ||
let _bidderRequests = []; | ||
|
@@ -151,7 +150,7 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels, a | |
let _winningBids = []; | ||
let _auctionStart; | ||
let _auctionEnd; | ||
let _timer; | ||
let _timeoutTimer; | ||
let _auctionStatus; | ||
let _nonBids = []; | ||
|
||
|
@@ -182,25 +181,21 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels, a | |
} | ||
|
||
function startAuctionTimer() { | ||
const timedOut = true; | ||
const timeoutCallback = executeCallback.bind(null, timedOut); | ||
let timer = setTimeout(timeoutCallback, _timeout); | ||
_timer = timer; | ||
_timeoutTimer = setTimeout(() => executeCallback(true), _timeout); | ||
} | ||
|
||
function executeCallback(timedOut, cleartimer) { | ||
// clear timer when done calls executeCallback | ||
if (cleartimer) { | ||
clearTimeout(_timer); | ||
function executeCallback(timedOut) { | ||
if (!timedOut) { | ||
clearTimeout(_timeoutTimer); | ||
} | ||
|
||
if (_auctionEnd === undefined) { | ||
let timedOutBidders = []; | ||
let timedOutReqs = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. timedOutRequests |
||
if (timedOut) { | ||
logMessage(`Auction ${_auctionId} timedOut`); | ||
timedOutBidders = getTimedOutBids(_bidderRequests, _timelyBidders); | ||
if (timedOutBidders.length) { | ||
events.emit(CONSTANTS.EVENTS.BID_TIMEOUT, timedOutBidders); | ||
timedOutReqs = _bidderRequests.filter(rq => !_timelyRequests.has(rq.bidderRequestId)).flatMap(br => br.bids) | ||
if (timedOutReqs.length) { | ||
events.emit(CONSTANTS.EVENTS.BID_TIMEOUT, timedOutReqs); | ||
} | ||
} | ||
|
||
|
@@ -225,8 +220,8 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels, a | |
logError('Error executing bidsBackHandler', null, e); | ||
} finally { | ||
// Calling timed out bidders | ||
if (timedOutBidders.length) { | ||
adapterManager.callTimedOutBidders(adUnits, timedOutBidders, _timeout); | ||
if (timedOutReqs.length) { | ||
adapterManager.callTimedOutBidders(adUnits, timedOutReqs, _timeout); | ||
} | ||
// Only automatically sync if the publisher has not chosen to "enableOverride" | ||
let userSyncConfig = config.getConfig('userSync') || {}; | ||
|
@@ -244,11 +239,11 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels, a | |
// when all bidders have called done callback atleast once it means auction is complete | ||
logInfo(`Bids Received for Auction with id: ${_auctionId}`, _bidsReceived); | ||
_auctionStatus = AUCTION_COMPLETED; | ||
executeCallback(false, true); | ||
executeCallback(false); | ||
} | ||
|
||
function onTimelyResponse(bidderCode) { | ||
_timelyBidders.add(bidderCode); | ||
function onTimelyResponse(bidderRequestId) { | ||
_timelyRequests.add(bidderRequestId); | ||
} | ||
|
||
function callBids() { | ||
|
@@ -386,7 +381,6 @@ export function newAuction({adUnits, adUnitCodes, callback, cbTimeout, labels, a | |
addBidReceived, | ||
addBidRejected, | ||
addNoBid, | ||
executeCallback, | ||
callBids, | ||
addWinningBid, | ||
setBidTargeting, | ||
|
@@ -554,21 +548,13 @@ export function auctionCallbacks(auctionDone, auctionInstance, {index = auctionM | |
} | ||
} | ||
|
||
export function doCallbacksIfTimedout(auctionInstance, bidResponse) { | ||
if (bidResponse.timeToRespond > auctionInstance.getTimeout() + config.getConfig('timeoutBuffer')) { | ||
auctionInstance.executeCallback(true); | ||
} | ||
} | ||
|
||
// Add a bid to the auction. | ||
export function addBidToAuction(auctionInstance, bidResponse) { | ||
setupBidTargeting(bidResponse); | ||
|
||
useMetrics(bidResponse.metrics).timeSince('addBidResponse', 'addBidResponse.total'); | ||
auctionInstance.addBidReceived(bidResponse); | ||
events.emit(CONSTANTS.EVENTS.BID_RESPONSE, bidResponse); | ||
|
||
doCallbacksIfTimedout(auctionInstance, bidResponse); | ||
} | ||
|
||
// Video bids may fail if the cache is down, or there's trouble on the network. | ||
|
@@ -615,16 +601,11 @@ const _storeInCache = (batch) => { | |
const { auctionInstance, bidResponse, afterBidAdded } = batch[i]; | ||
if (error) { | ||
logWarn(`Failed to save to the video cache: ${error}. Video bid must be discarded.`); | ||
|
||
doCallbacksIfTimedout(auctionInstance, bidResponse); | ||
} else { | ||
if (cacheId.uuid === '') { | ||
logWarn(`Supplied video cache key was already in use by Prebid Cache; caching attempt was rejected. Video bid must be discarded.`); | ||
|
||
doCallbacksIfTimedout(auctionInstance, bidResponse); | ||
} else { | ||
bidResponse.videoCacheKey = cacheId.uuid; | ||
|
||
if (!bidResponse.vastUrl) { | ||
bidResponse.vastUrl = getCacheUrl(bidResponse.videoCacheKey); | ||
} | ||
|
@@ -1014,24 +995,3 @@ function groupByPlacement(bidsByPlacement, bid) { | |
bidsByPlacement[bid.adUnitCode].bids.push(bid); | ||
return bidsByPlacement; | ||
} | ||
|
||
/** | ||
* Returns a list of bids that we haven't received a response yet where the bidder did not call done | ||
* @param {BidRequest[]} bidderRequests List of bids requested for auction instance | ||
* @param {Set} timelyBidders Set of bidders which responded in time | ||
* | ||
* @typedef {Object} TimedOutBid | ||
* @property {string} bidId The id representing the bid | ||
* @property {string} bidder The string name of the bidder | ||
* @property {string} adUnitCode The code used to uniquely identify the ad unit on the publisher's page | ||
* @property {string} auctionId The id representing the auction | ||
* | ||
* @return {Array<TimedOutBid>} List of bids that Prebid hasn't received a response for | ||
*/ | ||
function getTimedOutBids(bidderRequests, timelyBidders) { | ||
const timedOutBids = bidderRequests | ||
.map(bid => (bid.bids || []).filter(bid => !timelyBidders.has(bid.bidder))) | ||
.reduce(flatten, []); | ||
|
||
return timedOutBids; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason we're changing this variable? I find the new name less readable. I'd prefer clientBidderRequests, personally.