From 365174adc9e57ea6eaa18e42836cbffde3796545 Mon Sep 17 00:00:00 2001 From: LasterAlex Date: Sun, 20 Oct 2024 23:46:41 +0300 Subject: [PATCH] Fixed persistent state with outdated data + updated Bincode docs --- CHANGELOG.md | 1 + crates/teloxide/src/dispatching/dialogue.rs | 5 +++-- .../teloxide/src/dispatching/dialogue/storage/serializer.rs | 6 ++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d4d5ed8..b3e05cf6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Now Vec in requests serializes into [number] instead of [ {message_id: number} ], `forward_messages`, `copy_messages` and `delete_messages` now work properly - Now `InlineQueryResultsButton` serializes properly ([issue 1181](https://github.com/teloxide/teloxide/issues/1181)) - Now `ThreadId` is able to serialize in multipart requests ([PR 1179](https://github.com/teloxide/teloxide/pull/1179)) +- Now persistent state storage won't break if the data is outdated ([issue 1156](https://github.com/teloxide/teloxide/issues/1156)) ## 0.13.0 - 2024-08-16 diff --git a/crates/teloxide/src/dispatching/dialogue.rs b/crates/teloxide/src/dispatching/dialogue.rs index f519e44a..a53b6b58 100644 --- a/crates/teloxide/src/dispatching/dialogue.rs +++ b/crates/teloxide/src/dispatching/dialogue.rs @@ -232,8 +232,9 @@ where match dialogue.get_or_default().await { Ok(dialogue) => Some(dialogue), Err(err) => { - log::error!("dialogue.get_or_default() failed: {:?}", err); - None + log::error!("dialogue.get_or_default() failed: {:?}, falling back to default", err); + dialogue.update(D::default()).await.ok()?; + dialogue.get_or_default().await.ok() } } }) diff --git a/crates/teloxide/src/dispatching/dialogue/storage/serializer.rs b/crates/teloxide/src/dispatching/dialogue/storage/serializer.rs index 12bd74d1..08d4db4f 100644 --- a/crates/teloxide/src/dispatching/dialogue/storage/serializer.rs +++ b/crates/teloxide/src/dispatching/dialogue/storage/serializer.rs @@ -52,7 +52,13 @@ where /// The [Bincode] serializer for memory storages. /// +/// Can't serialize if the length is unknown, [see this issue](https://github.com/bincode-org/bincode/issues/167). +/// Use [`Cbor`] or [`Json`] if you are storing structs like [`Message`]. +/// /// [Bincode]: https://github.com/servo/bincode +/// [`Cbor`]: crate::dispatching::dialogue::serializer::Cbor +/// [`Json`]: crate::dispatching::dialogue::serializer::Json +/// [`Message`]: crate::types::Message #[cfg(feature = "bincode-serializer")] pub struct Bincode;