Skip to content
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

Idea: Preventing Inappropriate Service Invocation #724

Closed
byte-sourcerer opened this issue Mar 14, 2023 · 1 comment
Closed

Idea: Preventing Inappropriate Service Invocation #724

byte-sourcerer opened this issue Mar 14, 2023 · 1 comment

Comments

@byte-sourcerer
Copy link

The Service trait requires users to call poll_ready before invoking the call function in order to check if the service is ready or not. However, users are not obligated to do this and may forget to call poll_ready.

Therefore, it is worth considering whether it would be beneficial to use the type system to prevent users from invoking call if the service is not ready.

One suggestion is to introduce a ready token with type Service::Ready as follows:

pub trait Service<Request> {
    type Response;
    type Error;
    type Future: Future<Output = Result<Self::Response, Self::Error>>;
    type Ready;

    fn poll_ready(
        &mut self,
        cx: &mut Context<'_>
    ) -> Poll<Result<Self::Ready, Self::Error>>;

    fn call(&mut self, token: Self::Ready, req: Request) -> Self::Future;
}

With this approach, users must obtain a token: Self::Ready before invoking call, which can only be extracted from Poll::Ready(Ok(Self::Ready)). This follows the "Parse, not validate" paradigm.

Incorporating the ready token in the implementation of Service would help eliminate the need for checking the invalid state that may occur while invoking call:

State::Limited => panic!("service not ready; poll_ready must be called first"),

@davidpdrsn
Copy link
Member

This has been discussed before. See

@davidpdrsn davidpdrsn closed this as not planned Won't fix, can't repro, duplicate, stale Mar 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants