protected async Task WaitForReadyAsync(EndPoint expectedMaster = null, bool waitForReplication = false, TimeSpan?duration = null) { duration ??= TimeSpan.FromSeconds(30); var sw = Stopwatch.StartNew(); // wait until we have 1 master and 1 replica and have verified their roles var master = SentinelServerA.SentinelGetMasterAddressByName(ServiceName); if (expectedMaster != null && expectedMaster.ToString() != master.ToString()) { while (sw.Elapsed < duration.Value) { await Task.Delay(1000).ForAwait(); try { master = SentinelServerA.SentinelGetMasterAddressByName(ServiceName); if (expectedMaster.ToString() == master.ToString()) { break; } } catch (Exception) { // ignore } } } if (expectedMaster != null && expectedMaster.ToString() != master.ToString()) { throw new RedisException($"Master was expected to be {expectedMaster}"); } Log($"Master is {master}"); using var checkConn = Conn.GetSentinelMasterConnection(ServiceOptions); await WaitForRoleAsync(checkConn.GetServer(master), "master", duration.Value.Subtract(sw.Elapsed)).ForAwait(); var replicas = SentinelServerA.SentinelGetReplicaAddresses(ServiceName); if (replicas.Length > 0) { await Task.Delay(1000).ForAwait(); replicas = SentinelServerA.SentinelGetReplicaAddresses(ServiceName); await WaitForRoleAsync(checkConn.GetServer(replicas[0]), "slave", duration.Value.Subtract(sw.Elapsed)).ForAwait(); } if (waitForReplication) { await WaitForReplicationAsync(checkConn.GetServer(master), duration.Value.Subtract(sw.Elapsed)).ForAwait(); } }