Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const CANARY_THRESHOLD_PERCENT = parseInt(
process.env.CANARY_THRESHOLD_PERCENT || '0',
);
/**
* This function divides the traffic between Old Data API and New Data API
* based on the runtime configuration of CANARY_THRESHOLD_PERCENT.
*/
export const handleDataRequestWithCanaryTraffic = (
requestId: string,
animal: TAnimal,
): Promise<TDataApiData> => {
const randomValue = randomThresholdValue();
console.log(
`requestId: ${requestId} – DEBUG: random value = ${randomValue}, CANARY_THRESHOLD_PERCENT = ${CANARY_THRESHOLD_PERCENT}`,
);
if (CANARY_THRESHOLD_PERCENT > randomValue) {
/**
* If CANARY_THRESHOLD_PERCENT is higher than the random value (0-99), make
* the request to New Data API. Therefore if CANARY_THRESHOLD_PERCENT === 100,
* all request are made to New Data API.
*/
return newDataApiClient.getData(requestId, animal);
} else {
/**
* If CANARY_THRESHOLD_PERCENT is lower than or equal to the random value (0-99),
* make the request to Old Data API. Therefore if CANARY_THRESHOLD_PERCENT === 0,
* all request are made to Old Data API.
*/
return oldDataApiClient.getData(requestId, animal);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment