Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anttispitkanen/56f6bd2588b6fbc430e28e2b4607dedf to your computer and use it in GitHub Desktop.
Save anttispitkanen/56f6bd2588b6fbc430e28e2b4607dedf to your computer and use it in GitHub Desktop.
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