Skip to content
This repository was archived by the owner on Aug 4, 2023. It is now read-only.

fix: put [5s, 1d] range guard on the delay before re-fetching central config #185

Merged
merged 4 commits into from
Oct 25, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add missing file
  • Loading branch information
trentm committed Oct 25, 2022
commit dd372e97ce24bfa87e43db87e5b926092c6192a2
35 changes: 35 additions & 0 deletions lib/central-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict'

// Central config-related utilities for the APM http client.

const INTERVAL_DEFAULT_S = 300 // 5 min
const INTERVAL_MIN_S = 5
const INTERVAL_MAX_S = 86400 // 1d

/**
* Determine an appropriate delay until the next fetch of Central Config.
* Default to 5 minutes, minimum 5s, max 1d.
*
* The maximum of 1d ensures we don't get surprised by an overflow value to
* `setTimeout` per https://developer.mozilla.org/en-US/docs/Web/API/setTimeout#maximum_delay_value
*
* @param {Number|undefined} seconds - A number of seconds, typically pulled
* from a `Cache-Control: max-age=${seconds}` header on a previous central
* config request.
* @returns {Number}
*/
function getCentralConfigIntervalS(seconds) {
if (typeof seconds !== 'number' || seconds <= 0) {
return INTERVAL_DEFAULT_S
}
return Math.min(Math.max(seconds, INTERVAL_MIN_S), INTERVAL_MAX_S)
}

module.exports = {
getCentralConfigIntervalS,

// These are exported for testing.
INTERVAL_DEFAULT_S,
INTERVAL_MIN_S,
INTERVAL_MAX_S
}