Try fix work with not connected sockets (#1320)

* Lazy fd_to_replica_state_idx init; Fix work with not connected sockets

* Style

* const
This commit is contained in:
proller 2017-10-04 18:18:05 +03:00 committed by alexey-milovidov
parent 723f36ec6d
commit d498e8e152
2 changed files with 12 additions and 5 deletions

View File

@ -95,11 +95,11 @@ void Connection::disconnect()
{ {
//LOG_TRACE(log_wrapper.get(), "Disconnecting"); //LOG_TRACE(log_wrapper.get(), "Disconnecting");
in = nullptr;
out = nullptr; // can write to socket
if (socket) if (socket)
socket->close(); socket->close();
socket = nullptr; socket = nullptr;
in = nullptr;
out = nullptr;
connected = false; connected = false;
} }

View File

@ -20,7 +20,6 @@ MultiplexedConnections::MultiplexedConnections(Connection & connection, const Se
ReplicaState replica_state; ReplicaState replica_state;
replica_state.connection = &connection; replica_state.connection = &connection;
replica_states.push_back(replica_state); replica_states.push_back(replica_state);
fd_to_replica_state_idx.emplace(connection.socket->impl()->sockfd(), 0);
active_connection_count = 1; active_connection_count = 1;
} }
@ -36,7 +35,6 @@ MultiplexedConnections::MultiplexedConnections(
return; return;
replica_states.reserve(connections.size()); replica_states.reserve(connections.size());
fd_to_replica_state_idx.reserve(connections.size());
for (size_t i = 0; i < connections.size(); ++i) for (size_t i = 0; i < connections.size(); ++i)
{ {
Connection * connection = &(*connections[i]); Connection * connection = &(*connections[i]);
@ -47,7 +45,6 @@ MultiplexedConnections::MultiplexedConnections(
replica_state.connection = connection; replica_state.connection = connection;
replica_states.push_back(std::move(replica_state)); replica_states.push_back(std::move(replica_state));
fd_to_replica_state_idx.emplace(connection->socket->impl()->sockfd(), i);
} }
active_connection_count = connections.size(); active_connection_count = connections.size();
@ -304,6 +301,16 @@ MultiplexedConnections::ReplicaState & MultiplexedConnections::getReplicaForRead
} }
auto & socket = read_list[rand() % read_list.size()]; auto & socket = read_list[rand() % read_list.size()];
if (fd_to_replica_state_idx.empty())
{
fd_to_replica_state_idx.reserve(replica_states.size());
size_t replica_state_number = 0;
for (const auto & replica_state : replica_states)
{
fd_to_replica_state_idx.emplace(replica_state.connection->socket->impl()->sockfd(), replica_state_number);
++replica_state_number;
}
}
return replica_states[fd_to_replica_state_idx.at(socket.impl()->sockfd())]; return replica_states[fd_to_replica_state_idx.at(socket.impl()->sockfd())];
} }