-
Notifications
You must be signed in to change notification settings - Fork 321
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
Server: add with_shared_state() #642
Conversation
Allows true state sharing for multiple tide server applications without `Arc<Arc<State>>`.
I can't open the discord link so I'm not sure what the motivation is — but guessing it seems like we want to have access to Not opposed to always have require State to be edit: I'm a bit hesitant to add another constructor; it seems like it intends to work around deficiencies in |
Yeah, the idea is to be able to have more than one server share the same state without passing an I think this was the intention to begin with but probably was mistaken? Does |
One downside however is that depending on how State is constructed, |
I think that was part of the stated issue though, that it's annoying to have to deal with an |
I mean in some cases it may very well be an I think |
Lines 551 to 559 in 3778706
|
So @jbr got the above to compile using However I think that |
Alternative to http-rs#642 This approach is more flexible but requires the user ensure that their state implements/derives `Clone`, or is wrapped in an `Arc`. Co-authored-by: Jacob Rothstein <[email protected]>
Alternative to http-rs#642 This approach is more flexible but requires the user ensure that their state implements/derives `Clone`, or is wrapped in an `Arc`. Co-authored-by: Jacob Rothstein <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The outcome I'd like to see is for there to only be a single Server::with_state
method that takes State
where State: Clone
. The Arc
in the function signatures seems off, and I don't think we need multiple methods here.
/// Create a new Tide server with shared application scoped state. | ||
/// | ||
/// Shared application scoped state is useful for storing items, | ||
/// including across multiple tide applications. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// # use async_std::task::block_on; | ||
/// # fn main() -> Result<(), std::io::Error> { block_on(async { | ||
/// # | ||
/// use std::sync::Arc; | ||
/// use tide::Request; | ||
/// | ||
/// /// The shared application state. | ||
/// struct State { | ||
/// name: String, | ||
/// } | ||
/// | ||
/// // Define a new instance of the state. | ||
/// let state = Arc::new(State { | ||
/// name: "Nori".to_string() | ||
/// }); | ||
/// | ||
/// // Initialize the application with state. | ||
/// let mut app1 = tide::with_shared_state(state.clone()); | ||
/// let mut app2 = tide::with_shared_state(state.clone()); | ||
/// app2.at("/name").get(|req: Request<State>| async move { | ||
/// Ok(format!("Hello, {}!", &req.state().name)) | ||
/// }); | ||
/// app1.at("/hello").nest(app2); | ||
/// app1.listen("127.0.0.1:8080/hello/name").await?; | ||
/// # | ||
/// # Ok(()) }) } | ||
/// ``` | ||
pub fn with_shared_state<State>(state: Arc<State>) -> server::Server<State> | ||
where | ||
State: Send + Sync + 'static, | ||
{ | ||
Server::with_shared_state(state) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that State: Clone
this method is no longer needed.
/// Create a new Tide server with shared application scoped state. | |
/// | |
/// Shared application scoped state is useful for storing items, | |
/// including across multiple tide applications. | |
/// | |
/// # Examples | |
/// | |
/// ```no_run | |
/// # use async_std::task::block_on; | |
/// # fn main() -> Result<(), std::io::Error> { block_on(async { | |
/// # | |
/// use std::sync::Arc; | |
/// use tide::Request; | |
/// | |
/// /// The shared application state. | |
/// struct State { | |
/// name: String, | |
/// } | |
/// | |
/// // Define a new instance of the state. | |
/// let state = Arc::new(State { | |
/// name: "Nori".to_string() | |
/// }); | |
/// | |
/// // Initialize the application with state. | |
/// let mut app1 = tide::with_shared_state(state.clone()); | |
/// let mut app2 = tide::with_shared_state(state.clone()); | |
/// app2.at("/name").get(|req: Request<State>| async move { | |
/// Ok(format!("Hello, {}!", &req.state().name)) | |
/// }); | |
/// app1.at("/hello").nest(app2); | |
/// app1.listen("127.0.0.1:8080/hello/name").await?; | |
/// # | |
/// # Ok(()) }) } | |
/// ``` | |
pub fn with_shared_state<State>(state: Arc<State>) -> server::Server<State> | |
where | |
State: Send + Sync + 'static, | |
{ | |
Server::with_shared_state(state) | |
} |
@@ -167,7 +167,7 @@ impl Default for Server<()> { | |||
} | |||
|
|||
impl<State: Send + Sync + 'static> Server<State> { | |||
/// Create a new Tide server with shared application scoped state. | |||
/// Create a new Tide server with application scoped state. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Application state is and always was shared within the application. Now that State: Clone
it's true both locally to applications, and between applications.
/// Create a new Tide server with application scoped state. | |
/// Create a new Tide server shared with application scoped state. |
Closing in favor of #644 |
Alternative to http-rs#642 This approach is more flexible but requires the user ensure that their state implements/derives `Clone`, or is wrapped in an `Arc`. Co-authored-by: Jacob Rothstein <[email protected]>
Alternative to http-rs#642 This approach is more flexible but requires the user ensure that their state implements/derives `Clone`, or is wrapped in an `Arc`. Co-authored-by: Jacob Rothstein <[email protected]>
Allows true state sharing for multiple tide server applications without
Arc<Arc<State>>
.See discord discussion: https://discordapp.com/channels/598880689856970762/649056551835009097/731205374832541746