-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Asset Load Retry Plugin #11349
base: main
Are you sure you want to change the base?
Asset Load Retry Plugin #11349
Conversation
This feels like the right call. Looks super useful, but will require more thorough review (and more specialized expertise on the behalf of reviewers). |
eba36f4
to
e24e13d
Compare
👍 Done. I opened #11369 for the events and reworked this PR to be specifically about the retry plugin. |
Temporarily blocked on #11369: that should be merged first :) |
# Objective This adds events for assets that fail to load along with minor utility methods to make them useful. This paves the way for users writing their own error handling and retry systems, plus Bevy including robust retry handling: #11349. * Addresses #11288 * Needed for #11349 # Solution ```rust /// An event emitted when a specific [`Asset`] fails to load. #[derive(Event, Clone, Debug)] pub struct AssetLoadFailedEvent<A: Asset> { pub id: AssetId<A>, /// The original handle returned when the asset load was requested. pub handle: Option<Handle<A>>, /// The asset path that was attempted. pub path: AssetPath<'static>, /// Why the asset failed to load. pub error: AssetLoadError, } ``` I started implementing `AssetEvent::Failed` like suggested in #11288, but decided it was better as its own type because: * I think it makes sense for `AssetEvent` to only refer to assets that actually exist. * In order to return `AssetLoadError` in the event (which is useful information for error handlers that might attempt a retry) we would have to remove `Copy` from `AssetEvent`. * There are numerous places in the render app that match against `AssetEvent`, and I don't think it's worth introducing extra noise about assets that don't exist. I also introduced `UntypedAssetLoadErrorEvent`, which is very useful in places that need to support type flexibility, like an Asset-agnostic retry plugin. # Changelog * **Added:** `AssetLoadFailedEvent<A>` * **Added**: `UntypedAssetLoadFailedEvent` * **Added:** `AssetReaderError::Http` for status code information on HTTP errors. Before this, status codes were only available by parsing the error message of generic `Io` errors. * **Added:** `asset_server.get_path_id(path)`. This method simply gets the asset id for the path. Without this, one was left using `get_path_handle(path)`, which has the overhead of returning a strong handle. * **Fixed**: Made `AssetServer` loads return the same handle for assets that already exist in a failed state. Now, when you attempt a `load` that's in a `LoadState::Failed` state, it'll re-use the original asset id. The advantage of this is that any dependent assets created using the original handle will "unbreak" if a retry succeeds. --------- Co-authored-by: Alice Cecile <[email protected]>
e24e13d
to
4c14f2a
Compare
One topic for review is the future of |
One suggestion I have is that it would be nice if we could have some configurable random jitter added (see https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/), but otherwise LGTM. |
To be honest, while I feel like adding error types to |
Objective
This PR adds a robust retry mechanism for retrying assets that fail to load, which is extremely useful if you have assets coming in over a network. It's configured for the WASM asset reader by default, but can easily support third-party asset readers (like bevy_web_asset).
Solution
The
AssetLoadRetryPlugin
is added to theDefaultPlugins
, so it will work out-of-the box. To get retries to happen, you must use anAssetReader
that providesget_retry_settings
(likeHttpWasmAssetReader
), or provide your own source or asset-specific settings via theAssetLoadRetrier
resource.Customization Examples
Retry Settings By Source
Retry Settings By Asset Type
Disable Retries for Certain Domains
Other Thoughts
AssetLoadRetryPlugin<A>
originally, but found the ergonomics to be... not good. Also had a few other drawbacks:AssetReaderError::NotFound
should probably be reconsidered/removed (?). It's convenient forAssetReaderError
match expressions and easy to construct, but it overlaps:AssetReaderError::Io(err) => { err.kind() == ErrorKind::NotFound }
AssetReaderError::Http(404)
NotFound
and introduceis_not_found(&self) -> bool
on the enum. Alsofn io_not_found() -> Self
to maintain easy construction perhaps, but that's kinda funky.Changelog
AssetLoadRetryPlugin
which provides a robust retry mechanism for retrying assets that fail to load, which is extremely useful if you have assets coming in over a network. It's configured for the WASM asset reader by default, but can easily support third-party asset readers (like bevy_web_asset).