@ -255,11 +255,11 @@ EventEngine::ConnectionHandle PosixEventEngine::ConnectInternal(
MemoryAllocator & & allocator , const PosixTcpOptions & options ,
MemoryAllocator & & allocator , const PosixTcpOptions & options ,
Duration timeout ) {
Duration timeout ) {
int err ;
int err ;
int saved _errno;
int connect _errno;
do {
do {
err = connect ( sock . Fd ( ) , addr . address ( ) , addr . size ( ) ) ;
err = connect ( sock . Fd ( ) , addr . address ( ) , addr . size ( ) ) ;
} while ( err < 0 & & errno = = EINTR ) ;
} while ( err < 0 & & errno = = EINTR ) ;
saved _errno = errno ;
connect _errno = ( err < 0 ) ? errno : 0 ;
auto addr_uri = ResolvedAddressToURI ( addr ) ;
auto addr_uri = ResolvedAddressToURI ( addr ) ;
if ( ! addr_uri . ok ( ) ) {
if ( ! addr_uri . ok ( ) ) {
@ -274,13 +274,8 @@ EventEngine::ConnectionHandle PosixEventEngine::ConnectInternal(
PosixEventPoller * poller = poller_manager_ - > Poller ( ) ;
PosixEventPoller * poller = poller_manager_ - > Poller ( ) ;
EventHandle * handle =
EventHandle * handle =
poller - > CreateHandle ( sock . Fd ( ) , name , poller - > CanTrackErrors ( ) ) ;
poller - > CreateHandle ( sock . Fd ( ) , name , poller - > CanTrackErrors ( ) ) ;
int64_t connection_id = 0 ;
if ( saved_errno = = EWOULDBLOCK | | saved_errno = = EINPROGRESS ) {
// Connection is still in progress.
connection_id = last_connection_id_ . fetch_add ( 1 , std : : memory_order_acq_rel ) ;
}
if ( err > = 0 ) {
if ( connect_errno = = 0 ) {
// Connection already succeded. Return 0 to discourage any cancellation
// Connection already succeded. Return 0 to discourage any cancellation
// attempts.
// attempts.
Run ( [ on_connect = std : : move ( on_connect ) ,
Run ( [ on_connect = std : : move ( on_connect ) ,
@ -290,18 +285,21 @@ EventEngine::ConnectionHandle PosixEventEngine::ConnectInternal(
} ) ;
} ) ;
return EventEngine : : ConnectionHandle : : kInvalid ;
return EventEngine : : ConnectionHandle : : kInvalid ;
}
}
if ( saved _errno ! = EWOULDBLOCK & & saved _errno ! = EINPROGRESS ) {
if ( connect _errno ! = EWOULDBLOCK & & connect _errno ! = EINPROGRESS ) {
// Connection already failed. Return 0 to discourage any cancellation
// Connection already failed. Return 0 to discourage any cancellation
// attempts.
// attempts.
handle - > OrphanHandle ( nullptr , nullptr , " tcp_client_connect_error " ) ;
handle - > OrphanHandle ( nullptr , nullptr , " tcp_client_connect_error " ) ;
Run ( [ on_connect = std : : move ( on_connect ) ,
Run ( [ on_connect = std : : move ( on_connect ) ,
ep = absl : : FailedPreconditionError (
ep = absl : : FailedPreconditionError ( absl : : StrCat (
absl : : StrCat ( " connect failed: " , " addr: " , addr_uri . value ( ) ,
" connect failed: " , " addr: " , addr_uri . value ( ) ,
" error: " , std : : strerror ( saved _errno) ) ) ] ( ) mutable {
" error: " , std : : strerror ( connect _errno) ) ) ] ( ) mutable {
on_connect ( std : : move ( ep ) ) ;
on_connect ( std : : move ( ep ) ) ;
} ) ;
} ) ;
return EventEngine : : ConnectionHandle : : kInvalid ;
return EventEngine : : ConnectionHandle : : kInvalid ;
}
}
// Connection is still in progress.
int64_t connection_id =
last_connection_id_ . fetch_add ( 1 , std : : memory_order_acq_rel ) ;
AsyncConnect * ac = new AsyncConnect (
AsyncConnect * ac = new AsyncConnect (
std : : move ( on_connect ) , shared_from_this ( ) , executor_ . get ( ) , handle ,
std : : move ( on_connect ) , shared_from_this ( ) , executor_ . get ( ) , handle ,
std : : move ( allocator ) , options , addr_uri . value ( ) , connection_id ) ;
std : : move ( allocator ) , options , addr_uri . value ( ) , connection_id ) ;