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

Add utils to create and prepare socket for tcp client #31009

Merged
merged 16 commits into from Sep 22, 2022

Conversation

Vignesh2208
Copy link
Contributor

Forks the grpc_tcp_client_prepare_fd function defined in src/core/lib/iomgr/tcp_client_posix.h

@Vignesh2208 Vignesh2208 added lang/core release notes: no Indicates if PR should not be in release notes labels Sep 16, 2022
…f4b03

Automated fix for refs/heads/ee-tcp-posix-fork
return status;
}

auto is_unix_socket = [](const EventEngine::ResolvedAddress& addr) -> bool {
Copy link
Member

Choose a reason for hiding this comment

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

I don't see why this needs to be a lambda or function, the logic can be inlined here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

close(sock.Fd());
}
});
status = sock.SetSocketNonBlocking(1);
Copy link
Member

Choose a reason for hiding this comment

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

I'd recommend generalizing the RETURN_IF_ERROR macro in src/core/ext/transport/binder/wire_format/wire_reader_impl.cc, it will make this much more readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

Copy link
Member

Choose a reason for hiding this comment

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

No, this just made another copy of it. My recommendation is to put it in some central location (so it exists exactly once somewhere) and reuse it across gRPC.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved it to src/core/lib/gprpp/status_helper.h and modified all use cases.

PosixTcpOptions& options, const EventEngine::ResolvedAddress& target_addr,
EventEngine::ResolvedAddress& output_mapped_target_addr) {
PosixSocketWrapper::DSMode dsmode;
absl::StatusOr<PosixSocketWrapper> status;
Copy link
Member

Choose a reason for hiding this comment

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

Suggest renaming this variable to posix_socket_wrapper. Best practice is to name StatusOr variables as the variable type, not the status. See http://go/totw/labs/statusor-patterns-and-antipatterns

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

// unconnected TCP client fd.
// options: may contain custom tcp settings for the fd.
// target_addr: the destination address.
// output_mapped_target_addr: A out parameter. It is target_addr mapped to an
Copy link
Member

Choose a reason for hiding this comment

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

Can output_mapped_target_addr be replaced by a member of the PosixSocketWrapper? Should it always map to PeerAddress? It would be a cleaner API to have either a return value or output parameters, not both.

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 would like to keep the memory footprint of PosixSocketWrapper very small - currently it has only one member variable i.e the socket descriptor and all the operations it supports are stateless. This allows creating socket wrappers at will and using them to manipulate socket flags. The peer address is requested only once and I dont think we should be storing it in the socket wrapper. We can store it in the endpoint code that requests for it.

Shall I modify this to return an absl::StatusOr<std::tuple<PosixSocketWrapper, ResolvedAddress>> instead ?

Copy link
Member

Choose a reason for hiding this comment

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

I see. In that case, I'd suggest a return struct, per https://abseil.io/tips/176

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

#include "src/core/lib/gprpp/host_port.h"

#ifdef GRPC_HAVE_UNIX_SOCKET
#include <sys/un.h>
#endif

// If expr evaluates to absl Not-OK status, then execute expr2 and return the
// evaluated status.
#define RETURN_IF_ERROR(expr, expr2) \
Copy link
Member

Choose a reason for hiding this comment

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

The single-argument version is more general. I'd recommend keeping the absl::cleanup as you had it before, and standardizing on a general solution that works across gRPC.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved it to src/core/lib/gprpp/status_helper.h file now.

Copy link
Member

@drfloob drfloob left a comment

Choose a reason for hiding this comment

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

This is looking pretty good! Just a few more issues in this latest revision.

sock.TrySetSocketTcpUserTimeout(options, true);
}
RETURN_IF_ERROR(sock.SetSocketNoSigpipeIfPossible());
auto status = sock.ApplySocketMutatorInOptions(
Copy link
Member

Choose a reason for hiding this comment

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

nit: can't the end of this function be simplified to:

RETURN_IF_ERROR(sock.ApplySocketMutatorInOptions(...));
close_fd = false;
return absl::OkStatus();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@@ -301,10 +303,33 @@ class PosixSocketWrapper {
const experimental::EventEngine::ResolvedAddress& addr, int type,
int protocol, DSMode& dsmode);

struct PosixSocketCreateResult;
Copy link
Member

Choose a reason for hiding this comment

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

This does not need to be forward-declared. See the style guide advice here: https://google.github.io/styleguide/cppguide.html#Forward_Declarations

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It wasn't letting me compile otherwise because the PosixSocketWrapper definition is not complete at that point. So I moved the CreateAndPrepareTcpClientSocket function outside the class definition and made it non static.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Re-introduced it again after our discussion.

const PosixTcpOptions& options,
const EventEngine::ResolvedAddress& target_addr) {
PosixSocketWrapper::DSMode dsmode;
EventEngine::ResolvedAddress output_mapped_target_addr;
Copy link
Member

Choose a reason for hiding this comment

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

This argument is no longer output-mapped. Please change the name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to mapped_target_addr

Copy link
Member

@drfloob drfloob left a comment

Choose a reason for hiding this comment

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

Looks good, thanks Vignesh!

@Vignesh2208 Vignesh2208 merged commit 9bcee18 into grpc:master Sep 22, 2022
@copybara-service copybara-service bot added the imported Specifies if the PR has been imported to the internal repository label Sep 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bloat/none imported Specifies if the PR has been imported to the internal repository lang/core per-call-memory/neutral per-channel-memory/neutral release notes: no Indicates if PR should not be in release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants