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

How to asynchronously process a request in the middleware? #666

Closed
kpears201 opened this issue Mar 5, 2022 · 0 comments
Closed

How to asynchronously process a request in the middleware? #666

kpears201 opened this issue Mar 5, 2022 · 0 comments

Comments

@kpears201
Copy link

Boiled down my code to something simple. I cannot figure out how I can have an asynchronous decision on whether to return a response or continue on to rpc handlers using next

pub struct SampleMiddleware;
impl Middleware<Meta> for SampleMiddleware {
    type Future = FutureResponse;
    type CallFuture = middleware::NoopCallFuture;

    fn on_request<F, X>(&self, request: Request, meta: Meta, next: F) -> Either<Self::Future, X>
    where
        F: FnOnce(Request, Meta) -> X + Send,
        X: Future<Output = Option<Response>> + Send + 'static,
    {
        Either::Left(Box::pin(SampleMiddleware::process_async(request, meta, next)))
    }
}

impl SampleMiddleware {
    async fn process_async<F, X>(req: Request, meta: Meta, next: F) -> Option<Response>
    where
        F: FnOnce(Request, Meta) -> X + Send,
        X: Future<Output = Option<Response>> + Send + 'static,
    {
        let result = SampleMiddleware::async_fn(&req, &meta).await;
        match result {
            Some(r) => {
                Some(r)
            },
            None => {
                next(req, meta).await
            }
        }
    }

    async fn async_fn(req: &Request, meta: &Meta) -> Option<Response>
    {
        return None
    }
}

On this line: Either::Left(Box::pin(SampleMiddleware::process_async(request, meta, next))), I get:

the parameter type `F` may not live long enough
...so that the type `impl futures::Future<Output = std::option::Option<jsonrpc_core::Response>>` will meet its required lifetime bounds
consider adding an explicit lifetime bound...: `F: 'static`
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

1 participant