This repository was archived by the owner on Nov 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCacheServiceSubscriber.cs
More file actions
135 lines (115 loc) · 4.17 KB
/
Copy pathCacheServiceSubscriber.cs
File metadata and controls
135 lines (115 loc) · 4.17 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Chatham.Kit.ServiceDiscovery.Abstractions;
using Chatham.Kit.ServiceDiscovery.Cache.Internal;
namespace Chatham.Kit.ServiceDiscovery.Cache
{
public class CacheServiceSubscriber : IPollingServiceSubscriber
{
private bool _disposed;
private readonly ICacheClient _cache;
private readonly CancellationTokenSource _cts = new CancellationTokenSource();
private readonly IServiceSubscriber _serviceSubscriber;
private readonly string _id = Guid.NewGuid().ToString();
private Task _subscriptionTask;
private readonly SemaphoreSlim _mutex = new SemaphoreSlim(1, 1);
public event EventHandler EndpointsChanged;
public CacheServiceSubscriber(IServiceSubscriber serviceSubscriber, ICacheClient cache)
{
_cache = cache;
_serviceSubscriber = serviceSubscriber;
}
public async Task<List<Endpoint>> Endpoints(CancellationToken ct = default(CancellationToken))
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(CacheServiceSubscriber));
}
await StartSubscription(ct).ConfigureAwait(false);
return _cache.Get<List<Endpoint>>(_id);
}
public async Task StartSubscription(CancellationToken ct = default(CancellationToken))
{
if (_subscriptionTask == null)
{
await _mutex.WaitAsync(ct).ConfigureAwait(false);
try
{
if (_subscriptionTask == null)
{
var serviceUris = await _serviceSubscriber.Endpoints(ct).ConfigureAwait(false);
_cache.Set(_id, serviceUris);
_subscriptionTask = StartSubscriptionLoop(serviceUris);
}
}
finally
{
_mutex.Release();
}
}
}
private Task StartSubscriptionLoop(List<Endpoint> previousEndpoints)
{
return Task.Run(async () =>
{
while (!_cts.IsCancellationRequested)
{
try
{
var currentEndpoints = await _serviceSubscriber.Endpoints(_cts.Token).ConfigureAwait(false);
if (!EndpointListsMatch(previousEndpoints, currentEndpoints))
{
_cache.Set(_id, currentEndpoints);
EndpointsChanged?.Invoke(this, EventArgs.Empty);
previousEndpoints = currentEndpoints;
}
}
catch
{
// ignore
}
}
}, _cts.Token);
}
private static bool EndpointListsMatch(ICollection<Endpoint> endpoints1, ICollection<Endpoint> endpoints2)
{
if (endpoints1.Count != endpoints2.Count)
{
return false;
}
var filteredSequence = endpoints1.Where(endpoints2.Contains);
return filteredSequence.Count() == endpoints1.Count;
}
~CacheServiceSubscriber()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
if (disposing)
{
if (!_cts.IsCancellationRequested)
{
_cts.Cancel();
}
_cts.Dispose();
_mutex.Dispose();
_serviceSubscriber.Dispose();
}
_cache.Remove(_id);
_disposed = true;
}
}
}