.Net Core 測試 StackExchange.Redis 連線 Redis

環境

.Net 6
StackExchange.Redis Nuget 版本號: 2.6.116

ConfigurationOptions 參數測試

  • AbortOnConnectFail

    Gets or sets whether connect/configuration timeouts should be explicitly notified via a TimeoutException.
    ConnectionMultiplexer.Connect 在連線失敗後是否會觸發 TimeoutException

  • ConnectTimeout

    Specifies the time in milliseconds that should be allowed for connection (defaults to 5 seconds unless SyncTimeout is higher).
    設定 ConnectionMultiplexer 連線時間,預設 5000ms,若 SyncTimeout 高於 5000ms 則改吃 SyncTimeout 的數值
    當後端、Redis Server CPU使用率 很高 or 網路問題延遲,則設定太低的 ConnectTimeout 會導致一直連線失敗

  • SyncTimeout

    Specifies the time in milliseconds that the system should allow for synchronous operations (defaults to 5 seconds).
    呼叫 Redis 命令的連線時間

  • ConnectRetry

    The number of times to repeat the initial connect cycle if no servers respond promptly.
    斷線重連次數

Sample

try
{
   var configuration = new ConfigurationOptions()
   {
      EndPoints = {
         { "10.240.0.11:6379" }
      },
      AbortOnConnectFail = true,
      ConnectTimeout = 5000,
      SyncTimeout = 3000,
      ConnectRetry = 3,
      
   };

   using (TextWriter log = File.CreateText("D:\\redis_log.txt"))
   {
      Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " Connect Start");
      var redisConnectionManager = ConnectionMultiplexer.Connect(configuration, log);
      Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " Connect End");

      try
      {
            var key1 = redisConnectionManager.GetDatabase().StringGet("Key1");
            Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + $" Key1:{key1}" + "\n");
            Thread.Sleep(1000);
      }
      catch (Exception ex)
      {
            Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + $" Error {ex.Message}");
      }
   }
}
catch (Exception ex)
{
   Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} {ex.Message}");
}

測試一 (連線到錯誤的 EndPoint)

AbortOnConnectFail = true
ConnectTimeout = 5000
SyncTimeout = 3000
ConnectRetry = 3

# 輸出
22:19:54.145 Connect Start
22:19:58.853 Error It was not possible to connect to the redis server(s). Error connecting right now. To allow this multiplexer to continue retrying until it's able to connect, use abortConnect=false in your connection string or AbortOnConnectFail=false; in your code.

程式流程:

  1. 執行 ConnectionMultiplexer.Connect 失敗時會 retry 3 (ConnectRetry 設定值) 次,重試次數結束仍然連線失敗時,則直接噴出 TimeoutException
    備註: 可透過觀察 redis_log.txt 來發現確實有嘗試連線 3 次 Redis EndPoint

測試二 (連線到錯誤的 EndPoint)

AbortOnConnectFail = false
ConnectTimeout = 5000
SyncTimeout = 3000
ConnectRetry = 3

# 輸出
22:25:17.968 Connect Start
22:25:22.859 Connect End
22:25:25.945 GetKey Error The message timed out in the backlog attempting to send because no connection became available (3000ms) - Last Connection Exception: UnableToConnect on 10.240.0.11:63790/Interactive, Initializing/NotStarted, last: NONE, origin: BeginConnectAsync, outstanding: 0, last-read: 2s ago, last-write: 2s ago, keep-alive: 60s, state: Connecting, mgr: 10 of 10 available, last-heartbeat: never, global: 2s ago, v: 2.6.116.40240, command=GET, timeout: 3000, inst: 0, qu: 1, qs: 0, aw: False, bw: SpinningDown, rs: NotStarted, ws: Idle, in: 0, last-in: 0, cur-in: 0, sync-ops: 1, async-ops: 0, serverEndpoint: 10.240.0.11:63790, conn-sec: n/a, aoc: 0, mc: 1/1/0, mgr: 10 of 10 available, clientName: DESKTOP-BL95MDV(SE.Redis-v2.6.116.40240), IOCP: (Busy=0,Free=1000,Min=12,Max=1000), WORKER: (Busy=1,Free=32766,Min=12,Max=32767), POOL: (Threads=10,QueuedItems=0,CompletedItems=37), v: 2.6.116.40240 (Please take a look at this article for some common client-side issues that can cause timeouts: https://stackexchange.github.io/StackExchange.Redis/Timeouts)

程式流程:

  1. 執行 ConnectionMultiplexer.Connect 失敗時會 retry 3 (ConnectRetry 設定值) 次,重試次數結束仍然連線失敗時,不會噴出 TimeoutException
  2. 接續往下執行,直到 執行 StringGet,並且 3000 ms Timeout 才噴異常

測試三 (連線到正確的 EndPoint)

AbortOnConnectFail = false
ConnectTimeout = 5000
SyncTimeout = 3000
ConnectRetry = 3

# 輸出
22:32:38.248 Connect Start
22:32:43.456 Connect End
22:32:43.497 Key1:1

程式流程:

  1. 執行 ConnectionMultiplexer.Connect 耗時 5000ms (ConnectTimeout 設定值)
  2. 執行 StringGet 呼叫成功

測試三 (連線到正確的 EndPoint,但 SyneTimeout = 1ms => 測試執行操作命令時觸發 Timeout )

AbortOnConnectFail = false
ConnectTimeout = 5000
SyncTimeout = 1
ConnectRetry = 3

# 輸出
22:49:42.280 Connect Start
22:49:47.502 Connect End
22:49:47.549 GetKey Error Timeout performing GET (1ms), next: GET Key1, inst: 15, qu: 0, qs: 1, aw: False, bw: Inactive, rs: ReadAsync, ws: Idle, in: 0, in-pipe: 0, out-pipe: 0, last-in: 759, cur-in: 0, sync-ops: 1, async-ops: 1, serverEndpoint: 10.240.0.11:6379, conn-sec: 4.93, aoc: 0, mc: 1/1/0, mgr: 10 of 10 available, clientName: DESKTOP-BL95MDV(SE.Redis-v2.6.116.40240), PerfCounterHelperkeyHashSlot: 5291, IOCP: (Busy=0,Free=1000,Min=12,Max=1000), WORKER: (Busy=1,Free=32766,Min=12,Max=32767), POOL: (Threads=8,QueuedItems=0,CompletedItems=21), v: 2.6.116.40240 (Please take a look at this article for some common client-side issues that can cause timeouts: https://stackexchange.github.io/StackExchange.Redis/Timeouts)

程式流程:

  1. 執行 ConnectionMultiplexer.Connect 耗時 5000ms (ConnectTimeout 設定值)
  2. 執行 StringGet 因超過 Timeout 1ms (SyncTimeout 設定值) 時間導致觸發 RedisTimeoutException

轉載請註明來源,若有任何錯誤或表達不清楚的地方,歡迎在下方評論區留言,也可以來信至 leozheng0621@gmail.com
如果文章對您有幫助,歡迎斗內(donate),請我喝杯咖啡

斗內💰

×

歡迎斗內

github