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

handle_tunnel_request: small code cleanup #391

Merged
merged 1 commit into from
Jan 6, 2025

Conversation

tsnoam
Copy link
Contributor

@tsnoam tsnoam commented Dec 24, 2024

more idiomatic, less code, better readability

}
};
let remote = RemoteAddr::try_from(jwt.claims)
.inspect_err(|err| warn!("Rejecting connection with bad tunnel info: {err} {}", req.uri()))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a matter of taste, but I don't really like inspect_err followed by map_err. It is less clear than the match, as it splits the error block in 2 for no reason.

}

#[inline]
pub(super) fn extract_path_prefix(req: &Request<Incoming>) -> Result<&str, ()> {
pub(super) fn extract_path_prefix(req: &Request<Incoming>) -> Result<&str, HttpResponse> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those functions should not return HttpResponse as Err variant.
It is leaking abstraction, as this error should be known only from the above/callee and not be a inner detail of those function.

The goal of Result<&str, ()> is to let the callee handle/set the error. It is not of the responsibility of those functions to create the error that will be returned.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see your point.

however, this is a helper function used only by handle_tunnel_request(). and there are two things which should probably moved to the caller: warn logs and the error creation (as you requested).

i'll try to make the helper function do the one thing it is supposed to do: extract_path_prefix and also move the warn to the caller.

@@ -17,7 +17,9 @@ use tracing::{error, info, warn};
use url::Host;
use uuid::Uuid;

pub(super) fn bad_request() -> Response<Either<String, BoxBody<Bytes, anyhow::Error>>> {
pub type HttpResponse = Response<Either<String, BoxBody<Bytes, anyhow::Error>>>;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

more idiomatic, less code, better readability
@tsnoam tsnoam force-pushed the handle_tunnel_request branch from c0d3485 to 9c65248 Compare January 5, 2025 10:35
@tsnoam
Copy link
Contributor Author

tsnoam commented Jan 5, 2025

@erebe
can you please look again?

@erebe erebe merged commit d8e519c into erebe:main Jan 6, 2025
13 checks passed
@erebe
Copy link
Owner

erebe commented Jan 6, 2025

LGTM, thank you ;)

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

Successfully merging this pull request may close these issues.

2 participants