Created
May 31, 2021 13:23
-
-
Save anttispitkanen/56f6bd2588b6fbc430e28e2b4607dedf to your computer and use it in GitHub Desktop.
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
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