Commit 7e06b342 authored by Nick Craver's avatar Nick Craver

Cleanup: ServerEndPoint

parent 05554dd8
......@@ -19,7 +19,6 @@ internal enum UnselectableFlags
RedundantMaster = 1,
DidNotRespond = 2,
ServerType = 4
}
internal sealed partial class ServerEndPoint : IDisposable
......@@ -30,30 +29,23 @@ internal sealed partial class ServerEndPoint : IDisposable
private static readonly ServerEndPoint[] NoSlaves = new ServerEndPoint[0];
private readonly EndPoint endpoint;
private readonly Hashtable knownScripts = new Hashtable(StringComparer.Ordinal);
private readonly ConnectionMultiplexer multiplexer;
private int databases, writeEverySeconds;
private PhysicalBridge interactive, subscription;
bool isDisposed;
ServerType serverType;
private bool isDisposed;
private ServerType serverType;
private bool slaveReadOnly, isSlave;
private volatile UnselectableFlags unselectableReasons;
private Version version;
internal void ResetNonConnected()
{
interactive?.ResetNonConnected();
subscription?.ResetNonConnected();
}
public ServerEndPoint(ConnectionMultiplexer multiplexer, EndPoint endpoint, TextWriter log)
{
this.multiplexer = multiplexer;
......@@ -83,14 +75,7 @@ public ServerEndPoint(ConnectionMultiplexer multiplexer, EndPoint endpoint, Text
public bool HasDatabases => serverType == ServerType.Standalone;
public bool IsConnected
{
get
{
var tmp = interactive;
return tmp != null && tmp.IsConnected;
}
}
public bool IsConnected => interactive?.IsConnected == true;
internal Exception LastException
{
......@@ -100,13 +85,10 @@ internal Exception LastException
var tmp2 = subscription;
//check if subscription endpoint has a better lastexception
if (tmp2 != null && tmp2.LastException != null)
{
if (tmp2.LastException.Data.Contains("Redis-FailureType") && !tmp2.LastException.Data["Redis-FailureType"].ToString().Equals(ConnectionFailureType.UnableToConnect.ToString()))
if (tmp2?.LastException != null && tmp2.LastException.Data.Contains("Redis-FailureType") && !tmp2.LastException.Data["Redis-FailureType"].ToString().Equals(nameof(ConnectionFailureType.UnableToConnect)))
{
return tmp2.LastException;
}
}
return tmp1?.LastException;
}
}
......@@ -185,6 +167,7 @@ public PhysicalBridge GetBridge(ConnectionType type, bool create = true, TextWri
}
return null;
}
public PhysicalBridge GetBridge(RedisCommand command, bool create = true)
{
if (isDisposed) return null;
......@@ -207,7 +190,6 @@ public RedisFeatures GetFeatures()
public void SetClusterConfiguration(ClusterConfiguration configuration)
{
ClusterConfiguration = configuration;
if (configuration != null)
......@@ -251,16 +233,10 @@ public void SetUnselectable(UnselectableFlags flags)
}
}
}
public override string ToString()
{
return Format.ToString(EndPoint);
}
public bool TryEnqueue(Message message)
{
var bridge = GetBridge(message.Command);
return bridge != null && bridge.TryEnqueue(message, isSlave);
}
public override string ToString() => Format.ToString(EndPoint);
public bool TryEnqueue(Message message) => GetBridge(message.Command)?.TryEnqueue(message, isSlave) == true;
internal void Activate(ConnectionType type, TextWriter log)
{
......@@ -341,23 +317,21 @@ internal void AutoConfigure(PhysicalConnection connection)
}
}
int _nextReplicaOffset;
private int _nextReplicaOffset;
internal uint NextReplicaOffset() // used to round-robin between multiple replicas
=> (uint) System.Threading.Interlocked.Increment(ref _nextReplicaOffset);
internal Task Close()
{
var tmp = interactive;
Task result;
if (tmp == null || !tmp.IsConnected || !multiplexer.CommandMap.IsAvailable(RedisCommand.QUIT))
{
result = CompletedTask<bool>.Default(null);
return CompletedTask<bool>.Default(null);
}
else
{
result = QueueDirectAsync(Message.Create(-1, CommandFlags.None, RedisCommand.QUIT), ResultProcessor.DemandOK, bridge: interactive);
return QueueDirectAsync(Message.Create(-1, CommandFlags.None, RedisCommand.QUIT), ResultProcessor.DemandOK, bridge: interactive);
}
return result;
}
internal void FlushScriptCache()
......@@ -473,7 +447,7 @@ internal Message GetTracerMessage(bool assertIdentity)
internal bool IsSelectable(RedisCommand command)
{
var bridge = unselectableReasons == 0 ? GetBridge(command, false) : null;
return bridge != null && bridge.IsConnected;
return bridge?.IsConnected == true;
}
internal void OnEstablishing(PhysicalConnection connection, TextWriter log)
......@@ -519,7 +493,6 @@ public EndPoint MasterEndPoint
set { SetConfig(ref masterEndPoint, value); }
}
internal bool CheckInfoReplication()
{
lastInfoReplicationCheckTicks = Environment.TickCount;
......@@ -534,6 +507,7 @@ internal bool CheckInfoReplication()
}
return false;
}
private int lastInfoReplicationCheckTicks;
private int _heartBeatActive;
......@@ -544,8 +518,6 @@ internal void OnHeartbeat()
{
try
{
interactive?.OnHeartbeat(false);
subscription?.OnHeartbeat(false);
}
......@@ -601,7 +573,6 @@ internal string Summary()
var sb = new StringBuilder(Format.ToString(endpoint))
.Append(": ").Append(serverType).Append(" v").Append(version).Append(", ").Append(isSlave ? "slave" : "master");
if (databases > 0) sb.Append("; ").Append(databases).Append(" databases");
if (writeEverySeconds > 0)
sb.Append("; keep-alive: ").Append(TimeSpan.FromSeconds(writeEverySeconds));
......@@ -628,6 +599,7 @@ internal string Summary()
}
return sb.ToString();
}
internal void WriteDirectOrQueueFireAndForget<T>(PhysicalConnection connection, Message message, ResultProcessor<T> processor)
{
if (message != null)
......@@ -653,7 +625,8 @@ private PhysicalBridge CreateBridge(ConnectionType type, TextWriter log)
bridge.TryConnect(log);
return bridge;
}
void Handshake(PhysicalConnection connection, TextWriter log)
private void Handshake(PhysicalConnection connection, TextWriter log)
{
multiplexer.LogLocked(log, "Server handshake");
if (connection == null)
......@@ -698,7 +671,6 @@ void Handshake(PhysicalConnection connection, TextWriter log)
tracer = LoggingMessage.Create(log, tracer);
WriteDirectOrQueueFireAndForget(connection, tracer, ResultProcessor.EstablishConnection);
// note: this **must** be the last thing on the subscription handshake, because after this
// we will be in subscriber mode: regular commands cannot be sent
if (connType == ConnectionType.Subscription)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment