diff --git a/src/StackExchange.Redis/ConnectionMultiplexer.cs b/src/StackExchange.Redis/ConnectionMultiplexer.cs index 824ef5695..42e5996d1 100755 --- a/src/StackExchange.Redis/ConnectionMultiplexer.cs +++ b/src/StackExchange.Redis/ConnectionMultiplexer.cs @@ -1226,6 +1226,25 @@ internal ServerSnapshot Add(ServerEndPoint value) return new ServerSnapshot(arr, _count + 1); } + internal ServerSnapshot Remove(ServerEndPoint value) + { + if (value == null) return this; + var arr = _arr; + var arrCopy = new ServerEndPoint[arr.Length]; + int inserted = 0; + for (int i = 0; i < _count; ++i) + { + if (arr[i] != value) + { + arrCopy[inserted++] = arr[i]; + } + } + + if (inserted == _count) return this; + + return new ServerSnapshot(arrCopy, inserted); + } + internal EndPoint[] GetEndPoints() { if (_count == 0) return Array.Empty(); @@ -1817,6 +1836,28 @@ internal async Task ReconfigureAsync(bool first, bool reconfigureAll, LogP // and try to connect to these other nodes in the next iteration encounteredConnectedClusterServer = true; updatedClusterEndpointCollection = await GetEndpointsFromClusterNodes(server, log).ForAwait(); + + if (updatedClusterEndpointCollection?.Count > 0 && server?.EndPoint != null) + { + // if it's a discovery endpoint close it (i.e. endpoint is not in the list of ips returned by cluster nodes command) + var endpointInClusterNodes = updatedClusterEndpointCollection.FirstOrDefault( endpoint => server.EndPoint.Equals(endpoint)); + if (endpointInClusterNodes == null) + { + // close the initial cluster discovery connection as it's no longer needed + Task[] closeTask = new Task[2]; + closeTask[0] = server.Close(ConnectionType.Interactive); + closeTask[1] = server.Close(ConnectionType.Subscription); + await Task.WhenAll(closeTask); + // server cleanup + lock (this.servers) + { + this.servers.Remove(server.EndPoint); + _serverSnapshot.Remove(server); + } + server.Dispose(); + } + continue; + } } // set the server UnselectableFlags and update masters list