Skip to content

Bootstrap socket location from env variable #73

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

Merged
merged 1 commit into from
Feb 16, 2021

Conversation

ionut-arm
Copy link
Member

This commit adds functionality to create a IPC connector from a URL and
to bootstrap the IPC handler creation from an environment variable using
this new functionality.
Fixes #37

@ionut-arm ionut-arm added the enhancement New feature or request label Feb 15, 2021
@ionut-arm ionut-arm requested a review from hug-dev February 15, 2021 17:46
@ionut-arm ionut-arm self-assigned this Feb 15, 2021
Copy link
Member

@hug-dev hug-dev left a comment

Choose a reason for hiding this comment

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

Thanks! Clean code 👌 That will be useful.
I put two comments and was also wondering if we should add something about it in the README.md?

pub fn new(path: PathBuf, timeout: Option<Duration>) -> Self {
Handler { path, timeout }
pub fn new(path: PathBuf, timeout: Option<Duration>) -> Result<Self> {
if path.exists() {
Copy link
Member

Choose a reason for hiding this comment

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

I think you also need to check if path points to a socket as we do in the service, it might be a folder or a regular file:

if path.exists() && fs::metadata(&path)?.file_type().is_socket() {
    Ok(...)
} else {
    Err(...)
}

Comment on lines 43 to 51
} else {
Ok(RequestClient {
max_body_size: DEFAULT_MAX_BODY_SIZE,
ipc_handler: Box::from(unix_socket::Handler::new(
unix_socket::DEFAULT_SOCKET_PATH.into(),
Some(ipc_handler::DEFAULT_TIMEOUT),
)?),
})
}
Copy link
Member

Choose a reason for hiding this comment

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

instead of this else branch you could leverage the constructor from URL and do:

let url = env::var("PARSEC_SERVICE_ENDPOINT").or(DEFAULT_SERVICE_ENDPOINT);

and only keep the Ok branch.

and have DEFAULT_SERVICE_ENDPOINT as unix:///run/parsec/parsec.sock

Copy link
Member Author

@ionut-arm ionut-arm Feb 16, 2021

Choose a reason for hiding this comment

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

Done. I didn't create a DEFAULT_SERVICE_ENDPOINT mostly because it's not that straight forward to combine two const &str to obtain a new one, so I just stuck to formatting DEFAULT_SOCKET_PATH in

Also, you don't need // after the unix scheme, so unix:/run/parsec/parsec.sock is the corect value.

This commit adds functionality to create a IPC connector from a URL and
to bootstrap the IPC handler creation from an environment variable using
this new functionality.

Signed-off-by: Ionut Mihalcea <[email protected]>
Copy link
Member

@hug-dev hug-dev left a comment

Choose a reason for hiding this comment

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

Perfect!

Comment on lines +44 to +54
pub fn new(path: PathBuf, timeout: Option<Duration>) -> Result<Self> {
if path.exists()
&& std::fs::metadata(&path)
.map_err(|_| Error::Client(ClientErrorKind::InvalidSocketAddress))?
.file_type()
.is_socket()
{
Ok(Handler { path, timeout })
} else {
Err(Error::Client(ClientErrorKind::InvalidSocketAddress))
}
Copy link
Member

Choose a reason for hiding this comment

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

👌

@hug-dev hug-dev merged commit 2d71624 into parallaxsecond:master Feb 16, 2021
@ionut-arm ionut-arm deleted the socket-url branch February 16, 2021 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Resolve service endpoint from a URI environment variable
2 participants