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/cdfe007cb8782275e14000311f5a30f3 to your computer and use it in GitHub Desktop.
Save anttispitkanen/cdfe007cb8782275e14000311f5a30f3 to your computer and use it in GitHub Desktop.
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