private void UpsertConnectionToBrokerConnectionIndex(int brokerId, KafkaEndpoint brokerEndpoint, Func<int, IKafkaConnection> connectionFactory)
{
//associate the connection with the broker id, and add or update the reference
_brokerConnectionIndex.AddOrUpdate(brokerId, connectionFactory,
(i, existingConnection) =>
{
//if a connection changes for a broker close old connection and create a new one
if (existingConnection.Endpoint.Equals(brokerEndpoint)) return existingConnection;
_kafkaOptions.Log.WarnFormat("Broker:{0} Uri changed from:{1} to {2}", brokerId, existingConnection.Endpoint, brokerEndpoint);
existingConnection.Dispose();
return connectionFactory(i);
});
}