mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-31 16:40:37 +01:00
Fix the storage persistency bug
This commit is contained in:
parent
afa9c8f8f5
commit
bf1ed601ac
6 changed files with 76 additions and 56 deletions
12
CHANGELOG.md
12
CHANGELOG.md
|
@ -6,6 +6,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
|
|
||||||
## [unreleased]
|
## [unreleased]
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- `Storage::get_dialogue`
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- Do not return a dialogue from `Storage::{remove_dialogue, update_dialogue}`.
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- A storage persistency bug ([issue 304](https://github.com/teloxide/teloxide/issues/304)).
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
- Remove `reqwest` dependency. It's not needed after the [teloxide-core] integration.
|
- Remove `reqwest` dependency. It's not needed after the [teloxide-core] integration.
|
||||||
|
|
|
@ -4,7 +4,7 @@ use crate::dispatching::{
|
||||||
},
|
},
|
||||||
DispatcherHandler, UpdateWithCx,
|
DispatcherHandler, UpdateWithCx,
|
||||||
};
|
};
|
||||||
use std::{convert::Infallible, marker::PhantomData};
|
use std::{convert::Infallible, fmt::Debug, marker::PhantomData};
|
||||||
|
|
||||||
use futures::{future::BoxFuture, FutureExt, StreamExt};
|
use futures::{future::BoxFuture, FutureExt, StreamExt};
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
|
@ -65,7 +65,7 @@ where
|
||||||
Upd: GetChatId + Send + 'static,
|
Upd: GetChatId + Send + 'static,
|
||||||
D: Default + Send + 'static,
|
D: Default + Send + 'static,
|
||||||
S: Storage<D> + Send + Sync + 'static,
|
S: Storage<D> + Send + Sync + 'static,
|
||||||
S::Error: Send + 'static,
|
S::Error: Debug + Send + 'static,
|
||||||
{
|
{
|
||||||
/// Creates a dispatcher with the specified `handler` and `storage`.
|
/// Creates a dispatcher with the specified `handler` and `storage`.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
@ -97,18 +97,13 @@ where
|
||||||
async move {
|
async move {
|
||||||
let chat_id = cx.update.chat_id();
|
let chat_id = cx.update.chat_id();
|
||||||
|
|
||||||
let dialogue = Arc::clone(&storage)
|
let dialogue =
|
||||||
.remove_dialogue(chat_id)
|
Arc::clone(&storage).get_dialogue(chat_id).await.map(Option::unwrap_or_default);
|
||||||
.await
|
|
||||||
.map(Option::unwrap_or_default);
|
|
||||||
|
|
||||||
match handler.handle(DialogueWithCx { cx, dialogue }).await {
|
match handler.handle(DialogueWithCx { cx, dialogue }).await {
|
||||||
DialogueStage::Next(new_dialogue) => {
|
DialogueStage::Next(new_dialogue) => {
|
||||||
if let Ok(Some(_)) = storage.update_dialogue(chat_id, new_dialogue).await {
|
if let Err(e) = storage.update_dialogue(chat_id, new_dialogue).await {
|
||||||
panic!(
|
log::error!("Storage::update_dialogue failed: {:?}", e);
|
||||||
"Oops, you have an bug in your Storage: update_dialogue returns \
|
|
||||||
Some after remove_dialogue"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DialogueStage::Exit => {
|
DialogueStage::Exit => {
|
||||||
|
@ -117,8 +112,9 @@ where
|
||||||
// sender right here:
|
// sender right here:
|
||||||
senders.remove(&chat_id);
|
senders.remove(&chat_id);
|
||||||
|
|
||||||
// We already removed a dialogue from `storage` (see
|
if let Err(e) = storage.remove_dialogue(chat_id).await {
|
||||||
// the beginning of this async block).
|
log::error!("Storage::remove_dialogue failed: {:?}", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -134,7 +130,7 @@ where
|
||||||
Upd: GetChatId + Send + 'static,
|
Upd: GetChatId + Send + 'static,
|
||||||
D: Default + Send + 'static,
|
D: Default + Send + 'static,
|
||||||
S: Storage<D> + Send + Sync + 'static,
|
S: Storage<D> + Send + Sync + 'static,
|
||||||
S::Error: Send + 'static,
|
S::Error: Debug + Send + 'static,
|
||||||
R: Requester + Send,
|
R: Requester + Send,
|
||||||
{
|
{
|
||||||
fn handle(
|
fn handle(
|
||||||
|
|
|
@ -25,27 +25,41 @@ impl<S> InMemStorage<S> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<D> Storage<D> for InMemStorage<D> {
|
impl<D> Storage<D> for InMemStorage<D>
|
||||||
|
where
|
||||||
|
D: ToOwned<Owned = D>,
|
||||||
|
D: Send + 'static,
|
||||||
|
{
|
||||||
type Error = std::convert::Infallible;
|
type Error = std::convert::Infallible;
|
||||||
|
|
||||||
fn remove_dialogue(
|
fn remove_dialogue(self: Arc<Self>, chat_id: i64) -> BoxFuture<'static, Result<(), Self::Error>>
|
||||||
self: Arc<Self>,
|
|
||||||
chat_id: i64,
|
|
||||||
) -> BoxFuture<'static, Result<Option<D>, Self::Error>>
|
|
||||||
where
|
where
|
||||||
D: Send + 'static,
|
D: Send + 'static,
|
||||||
{
|
{
|
||||||
Box::pin(async move { Ok(self.map.lock().await.remove(&chat_id)) })
|
Box::pin(async move {
|
||||||
|
self.map.lock().await.remove(&chat_id);
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_dialogue(
|
fn update_dialogue(
|
||||||
self: Arc<Self>,
|
self: Arc<Self>,
|
||||||
chat_id: i64,
|
chat_id: i64,
|
||||||
dialogue: D,
|
dialogue: D,
|
||||||
) -> BoxFuture<'static, Result<Option<D>, Self::Error>>
|
) -> BoxFuture<'static, Result<(), Self::Error>>
|
||||||
where
|
where
|
||||||
D: Send + 'static,
|
D: Send + 'static,
|
||||||
{
|
{
|
||||||
Box::pin(async move { Ok(self.map.lock().await.insert(chat_id, dialogue)) })
|
Box::pin(async move {
|
||||||
|
self.map.lock().await.insert(chat_id, dialogue);
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_dialogue(
|
||||||
|
self: Arc<Self>,
|
||||||
|
chat_id: i64,
|
||||||
|
) -> BoxFuture<'static, Result<Option<D>, Self::Error>> {
|
||||||
|
Box::pin(async move { Ok(self.map.lock().await.get(&chat_id).map(ToOwned::to_owned)) })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,26 +42,26 @@ pub use sqlite_storage::{SqliteStorage, SqliteStorageError};
|
||||||
pub trait Storage<D> {
|
pub trait Storage<D> {
|
||||||
type Error;
|
type Error;
|
||||||
|
|
||||||
/// Removes a dialogue with the specified `chat_id`.
|
/// Removes a dialogue indexed by `chat_id`.
|
||||||
///
|
|
||||||
/// Returns `None` if there wasn't such a dialogue, `Some(dialogue)` if a
|
|
||||||
/// `dialogue` was deleted.
|
|
||||||
fn remove_dialogue(
|
fn remove_dialogue(
|
||||||
self: Arc<Self>,
|
self: Arc<Self>,
|
||||||
chat_id: i64,
|
chat_id: i64,
|
||||||
) -> BoxFuture<'static, Result<Option<D>, Self::Error>>
|
) -> BoxFuture<'static, Result<(), Self::Error>>
|
||||||
where
|
where
|
||||||
D: Send + 'static;
|
D: Send + 'static;
|
||||||
|
|
||||||
/// Updates a dialogue with the specified `chat_id`.
|
/// Updates a dialogue indexed by `chat_id` with `dialogue`.
|
||||||
///
|
|
||||||
/// Returns `None` if there wasn't such a dialogue, `Some(dialogue)` if a
|
|
||||||
/// `dialogue` was updated.
|
|
||||||
fn update_dialogue(
|
fn update_dialogue(
|
||||||
self: Arc<Self>,
|
self: Arc<Self>,
|
||||||
chat_id: i64,
|
chat_id: i64,
|
||||||
dialogue: D,
|
dialogue: D,
|
||||||
) -> BoxFuture<'static, Result<Option<D>, Self::Error>>
|
) -> BoxFuture<'static, Result<(), Self::Error>>
|
||||||
where
|
where
|
||||||
D: Send + 'static;
|
D: Send + 'static;
|
||||||
|
|
||||||
|
/// Provides a dialogue indexed by `chat_id`.
|
||||||
|
fn get_dialogue(
|
||||||
|
self: Arc<Self>,
|
||||||
|
chat_id: i64,
|
||||||
|
) -> BoxFuture<'static, Result<Option<D>, Self::Error>>;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,14 +5,13 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use futures::future::BoxFuture;
|
use futures::future::BoxFuture;
|
||||||
use log::{log_enabled, trace, Level::Trace};
|
|
||||||
|
|
||||||
use crate::dispatching::dialogue::Storage;
|
use crate::dispatching::dialogue::Storage;
|
||||||
|
|
||||||
/// Storage wrapper for logging purposes
|
/// Storage wrapper for logging purposes.
|
||||||
///
|
///
|
||||||
/// Reports about any dialogue update or removal action on `trace` level
|
/// Reports about any dialogue action using the `trace` level in the `log`
|
||||||
/// using `log` crate.
|
/// crate.
|
||||||
pub struct TraceStorage<S> {
|
pub struct TraceStorage<S> {
|
||||||
inner: Arc<S>,
|
inner: Arc<S>,
|
||||||
}
|
}
|
||||||
|
@ -35,14 +34,11 @@ where
|
||||||
{
|
{
|
||||||
type Error = <S as Storage<D>>::Error;
|
type Error = <S as Storage<D>>::Error;
|
||||||
|
|
||||||
fn remove_dialogue(
|
fn remove_dialogue(self: Arc<Self>, chat_id: i64) -> BoxFuture<'static, Result<(), Self::Error>>
|
||||||
self: Arc<Self>,
|
|
||||||
chat_id: i64,
|
|
||||||
) -> BoxFuture<'static, Result<Option<D>, Self::Error>>
|
|
||||||
where
|
where
|
||||||
D: Send + 'static,
|
D: Send + 'static,
|
||||||
{
|
{
|
||||||
trace!("Removing dialogue with {}", chat_id);
|
log::trace!("Removing dialogue #{}", chat_id);
|
||||||
<S as Storage<D>>::remove_dialogue(self.inner.clone(), chat_id)
|
<S as Storage<D>>::remove_dialogue(self.inner.clone(), chat_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,21 +46,23 @@ where
|
||||||
self: Arc<Self>,
|
self: Arc<Self>,
|
||||||
chat_id: i64,
|
chat_id: i64,
|
||||||
dialogue: D,
|
dialogue: D,
|
||||||
) -> BoxFuture<'static, Result<Option<D>, Self::Error>>
|
) -> BoxFuture<'static, Result<(), Self::Error>>
|
||||||
where
|
where
|
||||||
D: Send + 'static,
|
D: Send + 'static,
|
||||||
{
|
{
|
||||||
if log_enabled!(Trace) {
|
Box::pin(async move {
|
||||||
Box::pin(async move {
|
let to = format!("{:#?}", dialogue);
|
||||||
let to = format!("{:#?}", dialogue);
|
<S as Storage<D>>::update_dialogue(self.inner.clone(), chat_id, dialogue).await?;
|
||||||
let from =
|
log::trace!("Updated a dialogue #{}: {:#?}", chat_id, to);
|
||||||
<S as Storage<D>>::update_dialogue(self.inner.clone(), chat_id, dialogue)
|
Ok(())
|
||||||
.await?;
|
})
|
||||||
trace!("Updated dialogue with {}, {:#?} -> {}", chat_id, from, to);
|
}
|
||||||
Ok(from)
|
|
||||||
})
|
fn get_dialogue(
|
||||||
} else {
|
self: Arc<Self>,
|
||||||
<S as Storage<D>>::update_dialogue(self.inner.clone(), chat_id, dialogue)
|
chat_id: i64,
|
||||||
}
|
) -> BoxFuture<'static, Result<Option<D>, Self::Error>> {
|
||||||
|
log::trace!("Requested a dialogue #{}", chat_id);
|
||||||
|
<S as Storage<D>>::get_dialogue(self.inner.clone(), chat_id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ use teloxide_core::{requests::Requester, types::Message};
|
||||||
pub async fn dialogues_repl<'a, R, H, D, Fut>(requester: R, handler: H)
|
pub async fn dialogues_repl<'a, R, H, D, Fut>(requester: R, handler: H)
|
||||||
where
|
where
|
||||||
H: Fn(UpdateWithCx<R, Message>, D) -> Fut + Send + Sync + 'static,
|
H: Fn(UpdateWithCx<R, Message>, D) -> Fut + Send + Sync + 'static,
|
||||||
D: Default + Send + 'static,
|
D: ToOwned<Owned = D> + Default + Send + 'static,
|
||||||
Fut: Future<Output = DialogueStage<D>> + Send + 'static,
|
Fut: Future<Output = DialogueStage<D>> + Send + 'static,
|
||||||
R: Requester + Send + Clone + 'static,
|
R: Requester + Send + Clone + 'static,
|
||||||
<R as Requester>::GetUpdatesFaultTolerant: Send,
|
<R as Requester>::GetUpdatesFaultTolerant: Send,
|
||||||
|
@ -61,7 +61,7 @@ pub async fn dialogues_repl_with_listener<'a, R, H, D, Fut, L, ListenerE>(
|
||||||
listener: L,
|
listener: L,
|
||||||
) where
|
) where
|
||||||
H: Fn(UpdateWithCx<R, Message>, D) -> Fut + Send + Sync + 'static,
|
H: Fn(UpdateWithCx<R, Message>, D) -> Fut + Send + Sync + 'static,
|
||||||
D: Default + Send + 'static,
|
D: ToOwned<Owned = D> + Default + Send + 'static,
|
||||||
Fut: Future<Output = DialogueStage<D>> + Send + 'static,
|
Fut: Future<Output = DialogueStage<D>> + Send + 'static,
|
||||||
L: UpdateListener<ListenerE> + Send + 'a,
|
L: UpdateListener<ListenerE> + Send + 'a,
|
||||||
ListenerE: Debug + Send + 'a,
|
ListenerE: Debug + Send + 'a,
|
||||||
|
|
Loading…
Reference in a new issue