ZhiYeJianKang_PeiXun/cyqdata-master/Cache/MemRedis/HostNode.cs

232 lines
8.1 KiB
C#
Raw Normal View History

2025-02-20 15:41:53 +08:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Threading;
namespace CYQ.Data.Cache
{
/// <summary>
/// The SocketPool encapsulates the list of PooledSockets against one specific host, and contains methods for
/// acquiring or returning PooledSockets.
/// </summary>
internal class HostNode : IDisposable
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤Ȩ<D6A4>޵<EFBFBD>ί<EFBFBD><CEAF><EFBFBD>¼<EFBFBD><C2BC><EFBFBD>
internal delegate bool OnAfterSocketCreateDelegate(MSocket socket);
internal event OnAfterSocketCreateDelegate OnAfterSocketCreateEvent;
private static LogAdapter logger = LogAdapter.GetLogger(typeof(HostNode));
#region <EFBFBD>ɶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//Public variables and properties
public readonly string Host;
/// <summary>
/// <20><>չ<EFBFBD><D5B9><EFBFBD>ԣ<EFBFBD><D4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӻ<EFBFBD><D3BA><EFBFBD>Ҫ<EFBFBD><D2AA>֤<EFBFBD><D6A4><EFBFBD><EFBFBD><EBA3A8>Redis<69><73><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public string Password;
//Debug variables and properties
public int NewSockets = 0;
public int FailedNewSockets = 0;
public int ReusedSockets = 0;
public int DeadSocketsInPool = 0;
public int DeadSocketsOnReturn = 0;
public int Acquired = 0;
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2>
/// </summary>
public string Error;
/// <summary>
/// <20><>ǰ Socket <20>صĿ<D8B5><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public int Poolsize { get { return socketQueue.Count; } }
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5>Dz<EFBFBD><C7B2>ǹ<EFBFBD><C7B9>ˡ<EFBFBD>
/// </summary>
public bool IsEndPointDead = false;
public DateTime DeadEndPointRetryTime;
#endregion
/// <summary>
/// <20><><EFBFBD>ݵ<EFBFBD>Socket<65>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><C4B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD><CBA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˱<EFBFBD><CBB1>ݵ<EFBFBD><DDB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD><C2A3><EFBFBD><EFBFBD>ɱ<EFBFBD><C9B1><EFBFBD>Socket<65><74><EFBFBD><EFBFBD><E1B9A9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public HostNode HostNodeBak;
/// <summary>
/// Socket<65>Ĺҿ<C4B9>ʱ<EFBFBD>
/// </summary>
private DateTime socketDeadTime = DateTime.MinValue;
private int maxQueue = 128;
private int minQueue = 2;
/// <summary>
/// If the host stops responding, we mark it as dead for this amount of seconds,
/// and we double this for each consecutive failed retry. If the host comes alive
/// again, we reset this to 1 again.
/// </summary>
private int deadEndPointSecondsUntilRetry = 1;
private const int maxDeadEndPointSecondsUntilRetry = 60 * 10; //10 minutes
private HostServer hostServer;
private Queue<MSocket> socketQueue = new Queue<MSocket>(16);
internal HostNode(HostServer hostServer, string host)
{
this.hostServer = hostServer;
string[] items = host.Split('-');
Host = items[0].Trim();
if (items.Length > 1)
{
Password = items[1].Trim();
}
}
/// <summary>
/// Gets a socket from the pool.
/// If there are no free sockets, a new one will be created. If something goes
/// wrong while creating the new socket, this pool's endpoint will be marked as dead
/// and all subsequent calls to this method will return null until the retry interval
/// has passed.
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>չ<EFBFBD><D5B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳأ<D3B3>
/// </summary>
internal MSocket Acquire()
{
//<2F><><EFBFBD>⵱ǰ<E2B5B1>Ƿ<EFBFBD><C7B7>ҿƣ<D2BF><C6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(15<31><35><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)<29><><EFBFBD>ɱ<EFBFBD><C9B1>ݷ<EFBFBD><DDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E1B9A9><EFBFBD><EFBFBD>
if (socketDeadTime.AddMinutes(15) >= DateTime.Now && HostNodeBak != null)
{
return HostNodeBak.Acquire();
}
//Do we have free sockets in the pool?
//if so - return the first working one.
//if not - create a new one.
Interlocked.Increment(ref Acquired);
lock (socketQueue)
{
while (socketQueue.Count > 0)
{
MSocket socket = socketQueue.Dequeue();
if (socket != null && socket.IsAlive)
{
Interlocked.Increment(ref ReusedSockets);
return socket;
}
Interlocked.Increment(ref DeadSocketsInPool);
}
}
//If we know the endpoint is dead, check if it is time for a retry, otherwise return null.
if (IsEndPointDead)
{
if (DateTime.Now > DeadEndPointRetryTime)
{
//Retry
IsEndPointDead = false;
}
else
{
//Still dead
return null;
}
}
//Try to create a new socket. On failure, mark endpoint as dead and return null.
try
{
MSocket socket = new MSocket(this, Host);
Interlocked.Increment(ref NewSockets);
//Reset retry timer on success.
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECB3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD>
if (OnAfterSocketCreateEvent != null)
{
OnAfterSocketCreateEvent(socket);
}
deadEndPointSecondsUntilRetry = 1;
return socket;
}
catch (Exception e)
{
Interlocked.Increment(ref FailedNewSockets);
logger.Error("Error connecting to: " + Host, e);
//Mark endpoint as dead
IsEndPointDead = true;
//Retry in 2 minutes
DeadEndPointRetryTime = DateTime.Now.AddSeconds(deadEndPointSecondsUntilRetry);
if (deadEndPointSecondsUntilRetry < maxDeadEndPointSecondsUntilRetry)
{
deadEndPointSecondsUntilRetry = deadEndPointSecondsUntilRetry * 2; //Double retry interval until next time
}
socketDeadTime = DateTime.Now;
//<2F><><EFBFBD>ر<EFBFBD><D8B1>ݵij<DDB5>
if (HostNodeBak != null)
{
return HostNodeBak.Acquire();
}
return null;
}
}
/// <summary>
/// Returns a socket to the pool.
/// If the socket is dead, it will be destroyed.
/// If there are more than MaxPoolSize sockets in the pool, it will be destroyed.
/// If there are less than MinPoolSize sockets in the pool, it will always be put back.
/// If there are something inbetween those values, the age of the socket is checked.
/// If it is older than the SocketRecycleAge, it is destroyed, otherwise it will be
/// put back in the pool.
/// </summary>
internal void Return(MSocket socket)
{
//If the socket is dead, destroy it.
if (!socket.IsAlive || hasDisponse)
{
Interlocked.Increment(ref DeadSocketsOnReturn);
socket.Close();
}
else
{
//Clean up socket
socket.Reset();
//Check pool size.
if (socketQueue.Count >= maxQueue)
{
//If the pool is full, destroy the socket.
socket.Close();
}
else if (socketQueue.Count > minQueue && socket.CreateTime.AddMinutes(30) < DateTime.Now)
{
//socket <20><><EFBFBD>񳬹<EFBFBD><F1B3ACB9><EFBFBD>Сʱ<D0A1>ģ<EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD>ˣ<EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׸<EFBFBD><D7B8><EFBFBD><EFBFBD><EFBFBD>
//If we have more than the minimum amount of sockets, but less than the max, and the socket is older than the recycle age, we destroy it.
socket.Close();
}
else
{
//Put the socket back in the pool.
lock (socketQueue)
{
socketQueue.Enqueue(socket);
}
}
}
}
#region IDisposable <EFBFBD><EFBFBD>Ա
bool hasDisponse = false;
public void Dispose()
{
hasDisponse = true;
while (socketQueue.Count > 0)
{
socketQueue.Dequeue().Close();
}
}
#endregion
}
}