forked from ServiceStack/ServiceStack.Redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedisState.cs
More file actions
100 lines (83 loc) · 3.45 KB
/
RedisState.cs
File metadata and controls
100 lines (83 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using ServiceStack.Logging;
namespace ServiceStack.Redis
{
/// <summary>
/// Don't immediately kill connections of active clients after failover to give them a chance to dispose gracefully.
/// Deactivating clients are automatically cleared from the pool.
/// </summary>
internal static class RedisState
{
private static ILog log = LogManager.GetLogger(typeof(RedisState));
internal static long TotalFailovers = 0;
internal static long TotalDeactivatedClients = 0;
internal static long TotalFailedSentinelWorkers = 0;
internal static long TotalForcedMasterFailovers = 0;
internal static long TotalInvalidMasters = 0;
internal static long TotalNoMastersFound = 0;
internal static long TotalClientsCreated = 0;
internal static long TotalClientsCreatedOutsidePool = 0;
internal static long TotalSubjectiveServersDown = 0;
internal static long TotalObjectiveServersDown = 0;
internal static readonly ConcurrentDictionary<RedisClient, DateTime> DeactivatedClients = new ConcurrentDictionary<RedisClient, DateTime>();
internal static void DeactivateClient(RedisClient client)
{
Interlocked.Increment(ref TotalDeactivatedClients);
if (RedisConfig.DeactivatedClientsExpiry == TimeSpan.Zero)
{
client.DisposeConnection();
return;
}
var deactivatedAt = client.DeactivatedAt ?? DateTime.UtcNow;
client.DeactivatedAt = deactivatedAt;
if (!DeactivatedClients.TryAdd(client, deactivatedAt))
client.DisposeConnection();
}
internal static void DisposeExpiredClients()
{
if (RedisConfig.DeactivatedClientsExpiry == TimeSpan.Zero || DeactivatedClients.Count == 0)
return;
var now = DateTime.UtcNow;
var removeDisposed = new List<RedisClient>();
foreach (var entry in DeactivatedClients)
{
try
{
if (now - entry.Value <= RedisConfig.DeactivatedClientsExpiry)
continue;
if (log.IsDebugEnabled)
log.Debug("Disposed Deactivated Client: {0}".Fmt(entry.Key.GetHostString()));
entry.Key.DisposeConnection();
removeDisposed.Add(entry.Key);
}
catch
{
removeDisposed.Add(entry.Key);
}
}
if (removeDisposed.Count == 0)
return;
var dict = ((IDictionary<RedisClient, DateTime>)DeactivatedClients);
foreach (var client in removeDisposed)
{
dict.Remove(client);
}
}
internal static void DisposeAllDeactivatedClients()
{
if (RedisConfig.DeactivatedClientsExpiry == TimeSpan.Zero)
return;
var allClients = DeactivatedClients.Keys.ToArray();
DeactivatedClients.Clear();
foreach (var client in allClients)
{
if (log.IsDebugEnabled)
log.Debug("Disposed Deactivated Client (All): {0}".Fmt(client.GetHostString()));
client.DisposeConnection();
}
}
}
}