tower-rs / tower

async fn(Request) -> Result<Response, Error>
https://docs.rs/tower
MIT License
3.49k stars 276 forks source link

ServiceExt::short_circuit #647

Open BrynCooke opened 2 years ago

BrynCooke commented 2 years ago

Currently the filter layer allows processing to be halted, but an error must be returned because Result is used to indicate continued processing. It'd be good to be able to have the same functionality but use a dedicated enum to indicate continued processing.

Example uses:

Something like:

builder.short_circuit(|request| {
    ShortCircuit::Continue(request) //Continue processing using the supplied request
    ShortCircuit::Abort(Ok(response)) //Abort processing returning OK
    ShortCircuit::Abort(Err(error)) //Abort processing returning Err
});

Like filter, it would be good to have an async version. I'd be willing to create a PR for this.

LegNeato commented 2 years ago

How about using https://doc.rust-lang.org/stable/std/ops/enum.ControlFlow.html ?