-
Notifications
You must be signed in to change notification settings - Fork 330
add ServiceBuilder::boxed_clone_sync helper #804
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -789,6 +789,68 @@ impl<L> ServiceBuilder<L> { | |
| { | ||
| self.layer(crate::util::BoxCloneService::layer()) | ||
| } | ||
|
|
||
| /// This wraps the inner service with the [`Layer`] returned by [`BoxCloneSyncServiceLayer`]. | ||
| /// | ||
| /// This is similar to the [`boxed_clone`] method, but it requires that `Self` implement | ||
| /// [`Sync`], and the returned boxed service implements [`Sync`]. | ||
| /// | ||
| /// See [`BoxCloneSyncService`] for more details. | ||
| /// | ||
| /// # Example | ||
| /// | ||
| /// ``` | ||
| /// use tower::{Service, ServiceBuilder, BoxError, util::BoxCloneSyncService}; | ||
| /// use std::time::Duration; | ||
| /// # | ||
| /// # struct Request; | ||
| /// # struct Response; | ||
| /// # impl Response { | ||
| /// # fn new() -> Self { Self } | ||
| /// # } | ||
| /// | ||
| /// let service: BoxCloneSyncService<Request, Response, BoxError> = ServiceBuilder::new() | ||
| /// .load_shed() | ||
| /// .concurrency_limit(64) | ||
| /// .timeout(Duration::from_secs(10)) | ||
| /// .boxed_clone_sync() | ||
| /// .service_fn(|req: Request| async { | ||
| /// Ok::<_, BoxError>(Response::new()) | ||
| /// }); | ||
| /// # let service = assert_service(service); | ||
| /// | ||
| /// // The boxed service can still be cloned. | ||
| /// service.clone(); | ||
| /// # fn assert_service<S, R>(svc: S) -> S | ||
| /// # where S: Service<R> { svc } | ||
| /// ``` | ||
| /// | ||
| /// [`BoxCloneSyncServiceLayer`]: crate::util::BoxCloneSyncServiceLayer | ||
| /// [`BoxCloneSyncService`]: crate::util::BoxCloneSyncService | ||
| /// [`boxed_clone`]: Self::boxed_clone | ||
| #[cfg(feature = "util")] | ||
| pub fn boxed_clone_sync<S, R>( | ||
| self, | ||
| ) -> ServiceBuilder< | ||
| Stack< | ||
| crate::util::BoxCloneSyncServiceLayer< | ||
| S, | ||
| R, | ||
| <L::Service as Service<R>>::Response, | ||
| <L::Service as Service<R>>::Error, | ||
| >, | ||
| Identity, | ||
| >, | ||
| > | ||
| where | ||
| L: Layer<S> + Send + Sync + 'static, | ||
| L::Service: Service<R> + Clone + Send + Sync + 'static, | ||
| <L::Service as Service<R>>::Future: Send + Sync + 'static, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jlizen Before this arrived in Note: The future doesn't need to be I'd like to switch to use this I'm happy to stay with my current approach, but I thought I'd ask if there's a reason why this helper requires the Future to be
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see a compelling reason to keep the tight bounds, given that the service itself doesn't put the Sync bounds on the future. Seems worth cutting a PR to loosen.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, please send a PR. Also please use the issues or discussions section for bringing up stuff like this, comments on old PRs are much less likely to be seen. |
||
| { | ||
| let layer = self.into_inner(); | ||
|
|
||
| ServiceBuilder::new().layer(crate::util::BoxCloneSyncServiceLayer::new(layer)) | ||
| } | ||
| } | ||
|
|
||
| impl<L: fmt::Debug> fmt::Debug for ServiceBuilder<L> { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.