Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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