tower
tower copied to clipboard
ServiceExt::short_circuit
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:
- Caching - Return a response immediately (either Ok or Err)
- Validation - Abort processing, but OK response object should be returned which contains error code and message. BoxError is not suitable because structured errors are lost.
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.
How about using https://doc.rust-lang.org/stable/std/ops/enum.ControlFlow.html ?