Skip to content

Commit

Permalink
Simplify OutgoingConnectionFactory getConnection C++/Java (#3010)
Browse files Browse the repository at this point in the history
  • Loading branch information
pepone authored Oct 30, 2024
1 parent f52039f commit 26a29db
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 111 deletions.
86 changes: 18 additions & 68 deletions cpp/src/Ice/ConnectionFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,77 +457,35 @@ IceInternal::OutgoingConnectionFactory::getConnection(
bool& compress)
{
{
assert(cb);
unique_lock lock(_mutex);
if (_destroyed)
{
throw Ice::CommunicatorDestroyedException(__FILE__, __LINE__);
}

// Search for an existing connections matching one of the given endpoints.
Ice::ConnectionIPtr connection = findConnection(connectors, compress);
if (connection)
{
return connection;
}

//
// Try to get the connection. We may need to wait for other threads to
// finish if one of them is currently establishing a connection to one
// of our connectors.
// Determine whether another thread/request is currently attempting to connect to
// one of our endpoints; if so we wait until it's done.
//
while (true)
if (addToPending(cb, connectors))
{
if (_destroyed)
{
throw Ice::CommunicatorDestroyedException(__FILE__, __LINE__);
}

//
// Search for a matching connection. If we find one, we're done.
//
Ice::ConnectionIPtr connection = findConnection(connectors, compress);
if (connection)
{
return connection;
}

//
// Determine whether another thread/request is currently attempting to connect to
// one of our endpoints; if so we wait until it's done.
//
if (addToPending(cb, connectors))
{
//
// If a callback is not specified we wait until another thread notifies us about a
// change to the pending list. Otherwise, if a callback is provided we're done:
// when the pending list changes the callback will be notified and will try to
// get the connection again.
//
if (!cb)
{
_conditionVariable.wait(lock);
}
else
{
return nullptr;
}
}
else
{
//
// If no thread is currently establishing a connection to one of our connectors,
// we get out of this loop and start the connection establishment to one of the
// given connectors.
//
break;
}
// A connection to one of our endpoints is pending. The callback will be notified once the connection
// is established. Returning null indicates that the connection is still pending.
return nullptr;
}
}

//
// At this point, we're responsible for establishing the connection to one of
// the given connectors. If it's a non-blocking connect, calling nextConnector
// will start the connection establishment. Otherwise, we return null to get
// the caller to establish the connection.
//
if (cb)
{
cb->nextConnector();
}

// No connection is pending. Call nextConnector to initiate connection establishment. Return null to indicate
// that the connection is still pending.
cb->nextConnector();
return nullptr;
}

Expand Down Expand Up @@ -1578,7 +1536,7 @@ IceInternal::IncomingConnectionFactory::connectionStartCompleted(const Ice::Conn
lock_guard lock(_mutex);

//
// Initialy, connections are in the holding state. If the factory is active
// Initially, connections are in the holding state. If the factory is active
// we activate the connection.
//
if (_state == StateActive)
Expand All @@ -1590,15 +1548,7 @@ IceInternal::IncomingConnectionFactory::connectionStartCompleted(const Ice::Conn
void
IceInternal::IncomingConnectionFactory::connectionStartFailed(const Ice::ConnectionIPtr& /*connection*/, exception_ptr)
{
lock_guard lock(_mutex);
if (_state >= StateClosed)
{
return;
}

//
// Do not warn about connection exceptions here. The connection is not yet validated.
//
}

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,8 @@ public synchronized void connectionStartCompleted(ConnectionI connection) {
}

@Override
public synchronized void connectionStartFailed(ConnectionI connection, LocalException ex) {
if (_state >= StateClosed) {
return;
}
//
public void connectionStartFailed(ConnectionI connection, LocalException ex) {
// Do not warn about connection exceptions here. The connection is not yet validated.
//
}

public IncomingConnectionFactory(Instance instance, EndpointI endpoint, ObjectAdapter adapter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,47 +367,24 @@ private ConnectionI getConnection(
throw new CommunicatorDestroyedException();
}

//
// Try to get the connection. We may need to wait for other threads to
// finish if one of them is currently establishing a connection to one
// of our connectors.
//
while (true) {
if (_destroyed) {
throw new CommunicatorDestroyedException();
}

//
// Search for a matching connection. If we find one, we're done.
//
ConnectionI connection = findConnection(connectors, compress);
if (connection != null) {
return connection;
}

if (addToPending(cb, connectors)) {
return null;
} else {
//
// If no thread is currently establishing a connection to one of our connectors,
// we get out of this loop and start the connection establishment to one of the
// given connectors.
//
break;
}
// Search for an existing connections matching one of the given endpoints.
ConnectionI connection = findConnection(connectors, compress);
if (connection != null) {
return connection;
}
}

//
// At this point, we're responsible for establishing the connection to one of
// the given connectors. If it's a non-blocking connect, calling nextConnector
// will start the connection establishment. Otherwise, we return null to get
// the caller to establish the connection.
//
if (cb != null) {
cb.nextConnector();
if (addToPending(cb, connectors)) {
// A connection to one of our endpoints is pending. The callback will be notified
// once the connection
// is established. Returning null indicates that the connection is still pending.
return null;
}
}

// No connection is pending. Call nextConnector to initiate connection establishment. Return
// null to indicate
// that the connection is still pending.
cb.nextConnector();
return null;
}

Expand Down

0 comments on commit 26a29db

Please sign in to comment.