|
5 | 5 |
|
6 | 6 | namespace Elasticsearch.Net |
7 | 7 | { |
8 | | - public class StickyConnectionPool : IConnectionPool |
9 | | - { |
10 | | - protected IDateTimeProvider DateTimeProvider { get; } |
| 8 | + public class StickyConnectionPool : IConnectionPool |
| 9 | + { |
| 10 | + protected IDateTimeProvider DateTimeProvider { get; } |
11 | 11 |
|
12 | | - protected List<Node> InternalNodes { get; set; } |
| 12 | + protected List<Node> InternalNodes { get; set; } |
13 | 13 |
|
14 | | - public bool UsingSsl { get; } |
15 | | - public bool SniffedOnStartup { get; set; } |
| 14 | + public bool UsingSsl { get; } |
| 15 | + public bool SniffedOnStartup { get; set; } |
16 | 16 |
|
17 | | - public IReadOnlyCollection<Node> Nodes => this.InternalNodes; |
| 17 | + public IReadOnlyCollection<Node> Nodes => this.InternalNodes; |
18 | 18 |
|
19 | | - public int MaxRetries => this.InternalNodes.Count - 1; |
| 19 | + public int MaxRetries => this.InternalNodes.Count - 1; |
20 | 20 |
|
21 | | - public bool SupportsReseeding => false; |
| 21 | + public bool SupportsReseeding => false; |
22 | 22 |
|
23 | | - public bool SupportsPinging => true; |
| 23 | + public bool SupportsPinging => true; |
24 | 24 |
|
25 | | - public DateTime LastUpdate { get; protected set; } |
| 25 | + public DateTime LastUpdate { get; protected set; } |
26 | 26 |
|
27 | | - public StickyConnectionPool(IEnumerable<Uri> uris, IDateTimeProvider dateTimeProvider = null) |
28 | | - : this(uris.Select(uri => new Node(uri)), dateTimeProvider) |
29 | | - { } |
| 27 | + public StickyConnectionPool(IEnumerable<Uri> uris, IDateTimeProvider dateTimeProvider = null) |
| 28 | + : this(uris.Select(uri => new Node(uri)), dateTimeProvider) |
| 29 | + { } |
30 | 30 |
|
31 | | - public StickyConnectionPool(IEnumerable<Node> nodes, IDateTimeProvider dateTimeProvider = null) |
32 | | - { |
33 | | - nodes.ThrowIfEmpty(nameof(nodes)); |
| 31 | + public StickyConnectionPool(IEnumerable<Node> nodes, IDateTimeProvider dateTimeProvider = null) |
| 32 | + { |
| 33 | + nodes.ThrowIfEmpty(nameof(nodes)); |
34 | 34 |
|
35 | | - this.DateTimeProvider = dateTimeProvider ?? Elasticsearch.Net.DateTimeProvider.Default; |
| 35 | + this.DateTimeProvider = dateTimeProvider ?? Elasticsearch.Net.DateTimeProvider.Default; |
36 | 36 |
|
37 | | - var nn = nodes.ToList(); |
38 | | - var uris = nn.Select(n => n.Uri).ToList(); |
39 | | - if (uris.Select(u => u.Scheme).Distinct().Count() > 1) |
40 | | - throw new ArgumentException("Trying to instantiate a connection pool with mixed URI Schemes"); |
| 37 | + var nn = nodes.ToList(); |
| 38 | + var uris = nn.Select(n => n.Uri).ToList(); |
| 39 | + if (uris.Select(u => u.Scheme).Distinct().Count() > 1) |
| 40 | + throw new ArgumentException("Trying to instantiate a connection pool with mixed URI Schemes"); |
41 | 41 |
|
42 | | - this.UsingSsl = uris.Any(uri => uri.Scheme == "https"); |
| 42 | + this.UsingSsl = uris.Any(uri => uri.Scheme == "https"); |
43 | 43 |
|
44 | | - this.InternalNodes = nn |
45 | | - .DistinctBy(n => n.Uri) |
46 | | - .ToList(); |
| 44 | + this.InternalNodes = nn |
| 45 | + .DistinctBy(n => n.Uri) |
| 46 | + .ToList(); |
47 | 47 |
|
48 | | - this.LastUpdate = this.DateTimeProvider.Now(); |
49 | | - } |
| 48 | + this.LastUpdate = this.DateTimeProvider.Now(); |
| 49 | + } |
50 | 50 |
|
51 | | - protected int GlobalCursor = -1; |
| 51 | + protected int GlobalCursor = -1; |
52 | 52 |
|
53 | | - public IEnumerable<Node> CreateView(Action<AuditEvent, Node> audit = null) |
54 | | - { |
55 | | - var now = this.DateTimeProvider.Now(); |
56 | | - var nodes = this.InternalNodes.Where(n => n.IsAlive || n.DeadUntil <= now) |
57 | | - .ToList(); |
58 | | - var count = nodes.Count; |
59 | | - Node node; |
| 53 | + public IEnumerable<Node> CreateView(Action<AuditEvent, Node> audit = null) |
| 54 | + { |
| 55 | + var now = this.DateTimeProvider.Now(); |
| 56 | + var nodes = this.InternalNodes.Where(n => n.IsAlive || n.DeadUntil <= now) |
| 57 | + .ToList(); |
| 58 | + var count = nodes.Count; |
| 59 | + Node node; |
60 | 60 |
|
61 | | - if (count == 0) |
62 | | - { |
63 | | - var globalCursor = Interlocked.Increment(ref GlobalCursor); |
64 | | - //could not find a suitable node retrying on first node off globalCursor |
65 | | - audit?.Invoke(AuditEvent.AllNodesDead, null); |
66 | | - node = this.InternalNodes[globalCursor % this.InternalNodes.Count]; |
67 | | - node.IsResurrected = true; |
68 | | - audit?.Invoke(AuditEvent.Resurrection, node); |
69 | | - yield return node; |
70 | | - yield break; |
71 | | - } |
| 61 | + if (count == 0) |
| 62 | + { |
| 63 | + var globalCursor = Interlocked.Increment(ref GlobalCursor); |
| 64 | + //could not find a suitable node retrying on first node off globalCursor |
| 65 | + audit?.Invoke(AuditEvent.AllNodesDead, null); |
| 66 | + node = this.InternalNodes[globalCursor % this.InternalNodes.Count]; |
| 67 | + node.IsResurrected = true; |
| 68 | + audit?.Invoke(AuditEvent.Resurrection, node); |
| 69 | + yield return node; |
| 70 | + yield break; |
| 71 | + } |
72 | 72 |
|
73 | | - // If the cursor is greater than the default then it's been |
74 | | - // set already but we now have a live node so we should reset it |
75 | | - if (GlobalCursor > -1) |
76 | | - { |
77 | | - Interlocked.Exchange(ref GlobalCursor, -1); |
78 | | - } |
| 73 | + // If the cursor is greater than the default then it's been |
| 74 | + // set already but we now have a live node so we should reset it |
| 75 | + if (GlobalCursor > -1) |
| 76 | + { |
| 77 | + Interlocked.Exchange(ref GlobalCursor, -1); |
| 78 | + } |
79 | 79 |
|
80 | | - var localCursor = 0; |
| 80 | + var localCursor = 0; |
81 | 81 |
|
82 | | - for (var attempts = 0; attempts < count; attempts++) |
83 | | - { |
84 | | - node = nodes[localCursor]; |
85 | | - localCursor = (localCursor + 1) % count; |
86 | | - //if this node is not alive or no longer dead mark it as resurrected |
87 | | - if (!node.IsAlive) |
88 | | - { |
89 | | - audit?.Invoke(AuditEvent.Resurrection, node); |
90 | | - node.IsResurrected = true; |
91 | | - } |
92 | | - yield return node; |
93 | | - } |
94 | | - } |
| 82 | + for (var attempts = 0; attempts < count; attempts++) |
| 83 | + { |
| 84 | + node = nodes[localCursor]; |
| 85 | + localCursor = (localCursor + 1) % count; |
| 86 | + //if this node is not alive or no longer dead mark it as resurrected |
| 87 | + if (!node.IsAlive) |
| 88 | + { |
| 89 | + audit?.Invoke(AuditEvent.Resurrection, node); |
| 90 | + node.IsResurrected = true; |
| 91 | + } |
| 92 | + yield return node; |
| 93 | + } |
| 94 | + } |
95 | 95 |
|
96 | | - public void Reseed(IEnumerable<Node> nodes) { } |
| 96 | + public void Reseed(IEnumerable<Node> nodes) { } |
97 | 97 |
|
98 | | - void IDisposable.Dispose() => this.DisposeManagedResources(); |
| 98 | + void IDisposable.Dispose() => this.DisposeManagedResources(); |
99 | 99 |
|
100 | | - protected virtual void DisposeManagedResources() { } |
101 | | - } |
| 100 | + protected virtual void DisposeManagedResources() { } |
| 101 | + } |
102 | 102 | } |
0 commit comments