BackgroundLoader
The BackgroundLoader class provides asynchronous loading of configuration data while minimizing gateway latency. It's ideal for critical configuration that powers your gateway for smart routing or similar use cases.
Beta Feature
This component is in Beta - please use with care and provide feedback to the team if you encounter any issues.
The BackgroundLoader optimizes performance by:
- Immediately returning cached data when available
- Asynchronously refreshing data in the background
- Only blocking when cache is empty or expired
Constructor
ts
Creates a new background loader instance.
loader
- Async function that loads data for a given keyoptions
- Configuration options including TTL and timeoutT
- The type of data being loaded
Options
ts
Methods
get
Retrieves data for the specified key. Returns immediately if cached, otherwise blocks while loading.
ts
Example
ts
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
:
ts
You can't return a Response
created by the BackgroundLoader as a response from
a policy or handler. Responses can't be re-used in this way - they're associated
with the originating request and results from the BackgroundLoader can be shared
across requests.