Programming API
BackgroundLoader
The BackgroundLoader class is used to asynchronous load information in the background while minimizing gateway latency using this information. It is ideal to use for critical configuration that might be powering your gateway for smart routing or similar.
Note
This component is in Beta - please use with care and provide feedback to the team if you encounter any issues.
Obviously, you don't want to incur the cost of an async cost load on every request so it's important to cache any config you load into your gateway. However, sometimes this information is critical to keep very up to date so a traditional approach of awaiting cache expiry isn't sufficient. The BackgroundLoader will immediately return a cache entry if available, but also asynchronously load the config in the background to keep the cache up to date.
If the cache has not been refreshed and the current entry exceeds the TTL (Time
to Live) specified in the constructor the get
invocation will block while the
data is loaded.
You get to specify the loading function, which is just a simple async function
that can use fetch
.
The BackgroundLoader will ensure that only one request per 'key' is active at any one time to avoid overloading your destination services.
The BackgroundLoader has the following options. In the above example, we set ttlSeconds:
Caution
You cannot return a Response
created by the BackgroundLoader as a response
from a policy or handler. Responses cannot be re-used in this way - they are
associated with the originating request and results from the BackgroundLoader
can be shared across requests.