Created
May 31, 2021 13:23
-
-
Save anttispitkanen/cdfe007cb8782275e14000311f5a30f3 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 SHADOW_TRAFFIC_ENABLED = process.env.SHADOW_TRAFFIC_ENABLED === 'true'; | |
/** | |
* This function includes the shadow traffic "side channel" in addition to | |
* the "main channel". | |
*/ | |
export const handleDataRequestWithShadowTraffic = ( | |
requestId: string, | |
animal: TAnimal, | |
): Promise<TDataApiData> => { | |
/** | |
* If the runtime configuration says so, we make a shadow request to the New | |
* Data API. Note that this operation isn't blocking! I.e. if the API call | |
* below is faster, this doesn't need to be awaited on. Also, since this | |
* happens within the function scope and no external references are created, | |
* this will be nicely garbage collected, and no memory leaks are created. | |
*/ | |
if (SHADOW_TRAFFIC_ENABLED) { | |
newDataApiClient.getData(requestId, animal); | |
} | |
/** | |
* We get the data from the Old Data API and return that to the client. | |
* This line doesn't really care if the request above is made or not. | |
*/ | |
return oldDataApiClient.getData(requestId, animal); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment