From 11231655c222510a21792ccdfa51d36fa4c0f5fe Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Thu, 21 Jul 2022 12:36:57 +0400 Subject: [PATCH] Move ctrlc handler enable function to `DispatcherBuilder` This helps with consistency -- every setting is changed in builder. Also `Self -> Self` function sometimes plays more nicely with borrowck. Former-commit-id: dd4af30727caa2dfbd81464e0a6f8e87f056a2d3 --- CHANGELOG.md | 2 + MIGRATION_GUIDE.md | 4 +- README.md | 2 +- examples/buttons.rs | 2 +- examples/db_remember.rs | 2 +- examples/dialogue.rs | 2 +- examples/dispatching_features.rs | 2 +- examples/inline.rs | 2 +- examples/purchase.rs | 2 +- examples/shared_state.rs | 2 +- src/dispatching.rs | 2 +- src/dispatching/dispatcher.rs | 51 ++++++++++++++++++++------ src/dispatching/repls/commands_repl.rs | 2 +- src/dispatching/repls/repl.rs | 2 +- src/features.md | 40 ++++++++++---------- 15 files changed, 76 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8681abe3..db915866 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Security checks based on `secret_token` param of `set_webhook` to built-in webhooks. - `dispatching::update_listeners::{PollingBuilder, Polling, PollingStream}`. + - `DispatcherBuilder::enable_ctrlc_handler` method. ### Fixed @@ -27,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Deprecated - The `dispatching::update_listeners::polling` function. +- `Dispatcher::setup_ctrlc_handler` method. ## 0.9.2 - 2022-06-07 diff --git a/MIGRATION_GUIDE.md b/MIGRATION_GUIDE.md index dd1af6eb..78003f48 100644 --- a/MIGRATION_GUIDE.md +++ b/MIGRATION_GUIDE.md @@ -38,7 +38,9 @@ Some places now use `FileMeta` instead of `File`, you may need to change types. ### teloxide Teloxide itself doesn't have any major API changes. -Note however that `dispatching::update_listeners::polling` function was deprecated, use `polling_builder` instead. +Note however that some function were deprecated: +- Instead of `dispatching::update_listeners::polling` use `polling_builder` +- Instead of `Dispatcher::setup_ctrlc_handler` use `DispatcherBuilder::enable_ctrlc_handler` ## 0.7 -> 0.8 diff --git a/README.md b/README.md index 12663bea..b05f48f8 100644 --- a/README.md +++ b/README.md @@ -223,8 +223,8 @@ async fn main() { ), ) .dependencies(dptree::deps![InMemStorage::::new()]) + .enable_ctrlc_handler() .build() - .setup_ctrlc_handler() .dispatch() .await; } diff --git a/examples/buttons.rs b/examples/buttons.rs index 5b16fe35..b510e24a 100644 --- a/examples/buttons.rs +++ b/examples/buttons.rs @@ -30,7 +30,7 @@ async fn main() -> Result<(), Box> { .branch(Update::filter_callback_query().endpoint(callback_handler)) .branch(Update::filter_inline_query().endpoint(inline_query_handler)); - Dispatcher::builder(bot, handler).build().setup_ctrlc_handler().dispatch().await; + Dispatcher::builder(bot, handler).enable_ctrlc_handler().build().dispatch().await; Ok(()) } diff --git a/examples/db_remember.rs b/examples/db_remember.rs index 206768d9..1aed2808 100644 --- a/examples/db_remember.rs +++ b/examples/db_remember.rs @@ -54,8 +54,8 @@ async fn main() { Dispatcher::builder(bot, handler) .dependencies(dptree::deps![storage]) + .enable_ctrlc_handler() .build() - .setup_ctrlc_handler() .dispatch() .await; } diff --git a/examples/dialogue.rs b/examples/dialogue.rs index 30636924..8e30219d 100644 --- a/examples/dialogue.rs +++ b/examples/dialogue.rs @@ -51,8 +51,8 @@ async fn main() { ), ) .dependencies(dptree::deps![InMemStorage::::new()]) + .enable_ctrlc_handler() .build() - .setup_ctrlc_handler() .dispatch() .await; } diff --git a/examples/dispatching_features.rs b/examples/dispatching_features.rs index 91ef0808..dcaf4fcc 100644 --- a/examples/dispatching_features.rs +++ b/examples/dispatching_features.rs @@ -87,8 +87,8 @@ async fn main() { .error_handler(LoggingErrorHandler::with_custom_text( "An error has occurred in the dispatcher", )) + .enable_ctrlc_handler() .build() - .setup_ctrlc_handler() .dispatch() .await; } diff --git a/examples/inline.rs b/examples/inline.rs index a85de2c2..837fa30d 100644 --- a/examples/inline.rs +++ b/examples/inline.rs @@ -60,5 +60,5 @@ async fn main() { }, )); - Dispatcher::builder(bot, handler).build().setup_ctrlc_handler().dispatch().await; + Dispatcher::builder(bot, handler).enable_ctrlc_handler().build().dispatch().await; } diff --git a/examples/purchase.rs b/examples/purchase.rs index cbe4bfe6..f2f68729 100644 --- a/examples/purchase.rs +++ b/examples/purchase.rs @@ -52,8 +52,8 @@ async fn main() { Dispatcher::builder(bot, schema()) .dependencies(dptree::deps![InMemStorage::::new()]) + .enable_ctrlc_handler() .build() - .setup_ctrlc_handler() .dispatch() .await; } diff --git a/examples/shared_state.rs b/examples/shared_state.rs index c09fab93..21a0fcc0 100644 --- a/examples/shared_state.rs +++ b/examples/shared_state.rs @@ -27,8 +27,8 @@ async fn main() { Dispatcher::builder(bot, handler) // Pass the shared state to the handler as a dependency. .dependencies(dptree::deps![messages_total]) + .enable_ctrlc_handler() .build() - .setup_ctrlc_handler() .dispatch() .await; } diff --git a/src/dispatching.rs b/src/dispatching.rs index ce939b28..f8fb3bdf 100644 --- a/src/dispatching.rs +++ b/src/dispatching.rs @@ -140,8 +140,8 @@ //! //! Dispatcher::builder(bot, schema()) //! .dependencies(dptree::deps![InMemStorage::::new()]) +//! .enable_ctrlc_handler() //! .build() -//! .setup_ctrlc_handler() //! .dispatch() //! .await; //! } diff --git a/src/dispatching/dispatcher.rs b/src/dispatching/dispatcher.rs index 74eace82..3926792d 100644 --- a/src/dispatching/dispatcher.rs +++ b/src/dispatching/dispatcher.rs @@ -33,6 +33,7 @@ pub struct DispatcherBuilder { handler: Arc>, default_handler: DefaultHandler, error_handler: Arc + Send + Sync>, + ctrlc_handler: bool, distribution_f: fn(&Update) -> Option, worker_queue_size: usize, } @@ -78,6 +79,14 @@ where Self { dependencies, ..self } } + /// Enables the `^C` handler that [`shutdown`]s dispatching. + /// + /// [`shutdown`]: ShutdownToken::shutdown + #[cfg(feature = "ctrlc_handler")] + pub fn enable_ctrlc_handler(self) -> Self { + Self { ctrlc_handler: true, ..self } + } + /// Specifies size of the queue for workers. /// /// By default it's 64. @@ -101,6 +110,7 @@ where handler, default_handler, error_handler, + ctrlc_handler, distribution_f: _, worker_queue_size, } = self; @@ -111,6 +121,7 @@ where handler, default_handler, error_handler, + ctrlc_handler, distribution_f: f, worker_queue_size, } @@ -127,9 +138,10 @@ where error_handler, distribution_f, worker_queue_size, + ctrlc_handler, } = self; - Dispatcher { + let dp = Dispatcher { bot, dependencies, handler, @@ -142,7 +154,18 @@ where default_worker: None, current_number_of_active_workers: Default::default(), max_number_of_active_workers: Default::default(), + }; + + #[cfg(feature = "ctrlc_handler")] + { + if ctrlc_handler { + let mut dp = dp; + dp.setup_ctrlc_handler_inner(); + return dp; + } } + + dp } } @@ -212,6 +235,7 @@ where Box::pin(async {}) }), error_handler: LoggingErrorHandler::new(), + ctrlc_handler: false, worker_queue_size: DEFAULT_WORKER_QUEUE_SIZE, distribution_f: default_distribution_function, } @@ -238,7 +262,6 @@ where /// - [`crate::types::Me`] (can be used in [`HandlerExt::filter_command`]). /// /// [`shutdown`]: ShutdownToken::shutdown - /// [a ctrlc signal]: Dispatcher::setup_ctrlc_handler /// [`HandlerExt::filter_command`]: crate::dispatching::HandlerExt::filter_command pub async fn dispatch(&mut self) where @@ -258,7 +281,6 @@ where /// This method adds the same dependencies as [`Dispatcher::dispatch`]. /// /// [`shutdown`]: ShutdownToken::shutdown - /// [a ctrlc signal]: Dispatcher::setup_ctrlc_handler pub async fn dispatch_with_listener<'a, UListener, ListenerE, Eh>( &'a mut self, mut update_listener: UListener, @@ -425,7 +447,22 @@ where /// /// [`shutdown`]: ShutdownToken::shutdown #[cfg(feature = "ctrlc_handler")] + #[deprecated(since = "0.10", note = "use `enable_ctrlc_handler` on builder instead")] pub fn setup_ctrlc_handler(&mut self) -> &mut Self { + self.setup_ctrlc_handler_inner(); + self + } + + /// Returns a shutdown token, which can later be used to shutdown + /// dispatching. + pub fn shutdown_token(&self) -> ShutdownToken { + self.state.clone() + } +} + +impl Dispatcher { + #[cfg(feature = "ctrlc_handler")] + fn setup_ctrlc_handler_inner(&mut self) { let token = self.state.clone(); tokio::spawn(async move { loop { @@ -443,14 +480,6 @@ where } } }); - - self - } - - /// Returns a shutdown token, which can later be used to shutdown - /// dispatching. - pub fn shutdown_token(&self) -> ShutdownToken { - self.state.clone() } } diff --git a/src/dispatching/repls/commands_repl.rs b/src/dispatching/repls/commands_repl.rs index c5669e29..7f7db10f 100644 --- a/src/dispatching/repls/commands_repl.rs +++ b/src/dispatching/repls/commands_repl.rs @@ -96,8 +96,8 @@ pub async fn commands_repl_with_listener<'a, R, Cmd, H, L, ListenerE, E, Args>( Update::filter_message().filter_command::().chain(dptree::endpoint(handler)), ) .default_handler(ignore_update) + .enable_ctrlc_handler() .build() - .setup_ctrlc_handler() .dispatch_with_listener( listener, LoggingErrorHandler::with_custom_text("An error from the update listener"), diff --git a/src/dispatching/repls/repl.rs b/src/dispatching/repls/repl.rs index 509525d4..cecf90ad 100644 --- a/src/dispatching/repls/repl.rs +++ b/src/dispatching/repls/repl.rs @@ -71,8 +71,8 @@ where Dispatcher::builder(bot, Update::filter_message().chain(dptree::endpoint(handler))) .default_handler(ignore_update) + .enable_ctrlc_handler() .build() - .setup_ctrlc_handler() .dispatch_with_listener( listener, LoggingErrorHandler::with_custom_text("An error from the update listener"), diff --git a/src/features.md b/src/features.md index 1d199a80..2c4b2951 100644 --- a/src/features.md +++ b/src/features.md @@ -1,24 +1,24 @@ ## Cargo features -| Feature | Description | -|----------------------|------------------------------------------------------------------------------------| -| `webhooks` | Enables general webhook utilities (almost useless on its own) | -| `webhooks-axum` | Enables webhook implementation based on axum framework | -| `macros` | Re-exports macros from [`teloxide-macros`]. | -| `ctrlc_handler` | Enables the [`Dispatcher::setup_ctrlc_handler`] function (**enabled by default**). | -| `auto-send` | Enables the [`AutoSend`](adaptors::AutoSend) bot adaptor (**enabled by default**). | -| `throttle` | Enables the [`Throttle`](adaptors::Throttle) bot adaptor. | -| `cache-me` | Enables the [`CacheMe`](adaptors::CacheMe) bot adaptor. | -| `trace-adaptor` | Enables the [`Trace`](adaptors::Trace) bot adaptor. | -| `erased` | Enables the [`ErasedRequester`](adaptors::ErasedRequester) bot adaptor. | -| `full` | Enables all the features except `nightly`. | -| `nightly` | Enables nightly-only features (see the [teloxide-core features]). | -| `native-tls` | Enables the [`native-tls`] TLS implementation (**enabled by default**). | -| `rustls` | Enables the [`rustls`] TLS implementation. | -| `redis-storage` | Enables the [Redis] storage support for dialogues. | -| `sqlite-storage` | Enables the [Sqlite] storage support for dialogues. | -| `cbor-serializer` | Enables the [CBOR] serializer for dialogues. | -| `bincode-serializer` | Enables the [Bincode] serializer for dialogues. | +| Feature | Description | +|----------------------|--------------------------------------------------------------------------------------------| +| `webhooks` | Enables general webhook utilities (almost useless on its own) | +| `webhooks-axum` | Enables webhook implementation based on axum framework | +| `macros` | Re-exports macros from [`teloxide-macros`]. | +| `ctrlc_handler` | Enables the [`DispatcherBuilder::enable_ctrlc_handler`] function (**enabled by default**). | +| `auto-send` | Enables the [`AutoSend`](adaptors::AutoSend) bot adaptor (**enabled by default**). | +| `throttle` | Enables the [`Throttle`](adaptors::Throttle) bot adaptor. | +| `cache-me` | Enables the [`CacheMe`](adaptors::CacheMe) bot adaptor. | +| `trace-adaptor` | Enables the [`Trace`](adaptors::Trace) bot adaptor. | +| `erased` | Enables the [`ErasedRequester`](adaptors::ErasedRequester) bot adaptor. | +| `full` | Enables all the features except `nightly`. | +| `nightly` | Enables nightly-only features (see the [teloxide-core features]). | +| `native-tls` | Enables the [`native-tls`] TLS implementation (**enabled by default**). | +| `rustls` | Enables the [`rustls`] TLS implementation. | +| `redis-storage` | Enables the [Redis] storage support for dialogues. | +| `sqlite-storage` | Enables the [Sqlite] storage support for dialogues. | +| `cbor-serializer` | Enables the [CBOR] serializer for dialogues. | +| `bincode-serializer` | Enables the [Bincode] serializer for dialogues. | [Redis]: https://redis.io/ @@ -31,4 +31,4 @@ [`teloxide::utils::UpState`]: utils::UpState [teloxide-core features]: https://docs.rs/teloxide-core/latest/teloxide_core/#cargo-features -[`Dispatcher::setup_ctrlc_handler`]: dispatching::Dispatcher::setup_ctrlc_handler \ No newline at end of file +[`DispatcherBuilder::enable_ctrlc_handler`]: dispatching::DispatcherBuilder::enable_ctrlc_handler \ No newline at end of file