Design a Resource Loader for a Desktop Client
Design the module that loads textures, sounds and 3D models for a desktop app, from the install or by download from a CDN (content delivery network), and hands each one back to the caller when it is ready, with retries, shared loads for duplicate requests, per-caller cancel, caching and safe freeing of memory.
Problem statement
Design the resource-loading module of a desktop app. Other parts of the app ask for a resource, such as a texture, a sound or a 3D model, by its ID, and get it back, ready in memory, through a callback (a function the caller passes in, which the loader calls when the work is done).
Every request gets exactly one answer: the resource, an error, or cancelled.
Some resources ship with the app's install, and others are downloaded while the app runs from a CDN (content delivery network: servers that keep copies of files close to users). Some resources must also be decompressed or decrypted after they are fetched, before they go into memory for the rest of the app to use.
Treat decompress and decrypt as functions that already exist; say at which step of the flow you call them.
What the loader must handle: - Where a resource comes from. The caller passes only an ID. The loader looks it up in the manifest (the index of all resources) and gets a local path, a CDN URL, or both. When both exist, it can use a valid local version first.
When the local file is missing or has the wrong version, and there is a CDN URL, it downloads the resource instead. - Async loading and results (the call returns at once and the result arrives later). A call such as loadAsync(id, onComplete) registers the callback.
When the resource is ready, the callback hands it to the caller. A failure or a cancel comes back through the same callback with its own result, and each request gets exactly one callback. - Failures and retries.
A network timeout or a broken download can be retried after a wait; when the tries run out, the request gets an error. If the resource does not exist, or the caller cancels, the request ends at once. A downloaded file becomes a usable cache file only once it is complete. - Duplicate requests and cancel.
When the same ID and version are requested at the same time, the requests can share one underlying load. Each caller has its own callback and its own way to cancel. One caller cancelling ends only that caller's wait; the others keep waiting.
When the last waiting caller also cancels, the loader can drop the task if it is still queued, or try to stop a load that has already started and clean up after it. - Cache and memory. How do you reuse resources that are needed again and again?
When memory is tight and something must be freed, how does the loader tell whether a resource is still in use somewhere?
The round is held in pseudocode: be specific about each interface's inputs and return values and how the parts connect. For this exercise, assume the CDN itself, how the manifest is built and shipped, and the inside of the decompress and decrypt functions are out of scope.
What to build
- A caller asks for a resource by ID with a call such as loadAsync(id, onComplete), gets a handle (a small token for this request, used later to cancel it) back at once, and later receives the resource, ready in memory, through its callback.
- The loader finds the resource in the manifest, uses a valid local version when there is one, downloads it from the CDN when the local file is missing or has the wrong version and a CDN URL exists, and decrypts or decompresses it at a defined step before it goes into memory.
- Every request ends with exactly one callback: the resource, an error, or cancelled. Timeouts and broken downloads are retried after a wait until the tries run out; a resource that does not exist, or a cancel, ends the request at once; only a complete download becomes a cache file.
- Requests for the same ID and version at the same time share one underlying load, each with its own callback and cancel; one cancel ends only that caller's wait, and when the last caller cancels, the load is dropped from the queue or stopped and cleaned up.
- Resources that are needed again are reused instead of loaded again, and when memory is tight the loader frees only resources that nothing is still using.
At scale
- For this exercise, assume the manifest lists about 50,000 resources, from a few KB (a small sound) up to about 100 MB (a large 3D model), and that opening a busy scene asks for a few thousand of them within a few seconds, often the same ID from several parts of the app.
- For this exercise, assume loaded resources may use about 2 GB of memory in total, downloaded files may use about 10 GB of disk, and the loader should run at most about 6 downloads at a time.
- For this exercise, assume a timed-out or broken download is tried at most 3 times in all, with a longer wait before each new try.