Redis への接続を開くのに非常に長い時間がかかることがあります。接続スレッドの数と、おそらくPCの構成に依存するようです。4 コア CPU を搭載した 2 台のワークステーションで 50 スレッドのテストを実行しましたが、接続を開くのに 70 ~ 100 ミリ秒かかり、8 コア ワークステーションと 8 コア ステージング サーバーでは 1000 ~ 1500 ミリ秒、場合によってはそれ以上かかりました。奇妙な依存関係ですが、再現可能です。IIS アプリケーション プールが再起動し、すべてのスレッドが再接続しようとすると、キャッシュのダウンタイムなどの原因になります。合理的な接続時間を得るために何を変更する必要がありますか?
私は BookSleeve クライアントを使用しています。コード サンプルは次のとおりです。
static void Main(string[] args)
{
for (var i = 0; i < threadCount; i++)
threads.Add(new Thread(RunThread));
foreach (var thread in threads)
thread.Start();
foreach (var thread in threads)
thread.Join();
}
static void RunThread()
{
var connection = TryGetConnection();
while (connection == null)
{
connection = TryGetConnection();
}
}
static RedisConnection TryGetConnection()
{
var connection = currentConnection;
if ((connection != null) && (connection.State == RedisConnectionBase.ConnectionState.Open))
return connection;
lock (syncRoot)
{
if ((currentConnection != null) && (currentConnection.State == RedisConnectionBase.ConnectionState.Open))
return currentConnection;
if ((connectionTask != null) && connectionTask.IsCompleted)
connectionTask = null;
if (connectionTask == null)
{
if ((currentConnection != null) && (currentConnection.State == RedisConnectionBase.ConnectionState.Closed))
{
currentConnection.Dispose();
currentConnection = null;
}
if (currentConnection == null)
{
currentConnection = new RedisConnection(
serverAddress,
serverPort,
ioTimeout: (int) operationTimeout.TotalMilliseconds,
syncTimeout: (int) operationTimeout.TotalMilliseconds);
}
if (currentConnection.State == RedisConnectionBase.ConnectionState.New)
currentConnection.Open();
}
}
return null;
}