| | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.ServiceBus.Primitives |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Collections.Concurrent; |
| | 9 | | using System.Threading; |
| | 10 | | using System.Threading.Tasks; |
| | 11 | |
|
| | 12 | | sealed class ConcurrentExpiringSet<TKey> |
| | 13 | | { |
| | 14 | | readonly ConcurrentDictionary<TKey, DateTime> dictionary; |
| | 15 | | readonly ICollection<KeyValuePair<TKey, DateTime>> dictionaryAsCollection; |
| 10 | 16 | | readonly CancellationTokenSource tokenSource = new CancellationTokenSource(); |
| 10 | 17 | | volatile TaskCompletionSource<bool> cleanupTaskCompletionSource = new TaskCompletionSource<bool>(TaskCreationOpt |
| | 18 | | int closeSignaled; |
| | 19 | | bool closed; |
| 2 | 20 | | static readonly TimeSpan delayBetweenCleanups = TimeSpan.FromSeconds(30); |
| | 21 | |
|
| 10 | 22 | | public ConcurrentExpiringSet() |
| | 23 | | { |
| 10 | 24 | | this.dictionary = new ConcurrentDictionary<TKey, DateTime>(); |
| 10 | 25 | | this.dictionaryAsCollection = dictionary; |
| 10 | 26 | | _ = CollectExpiredEntriesAsync(tokenSource.Token); |
| 10 | 27 | | } |
| | 28 | |
|
| | 29 | | public void AddOrUpdate(TKey key, DateTime expiration) |
| | 30 | | { |
| 10 | 31 | | this.ThrowIfClosed(); |
| | 32 | |
|
| 8 | 33 | | this.dictionary[key] = expiration; |
| 8 | 34 | | this.cleanupTaskCompletionSource.TrySetResult(true); |
| 8 | 35 | | } |
| | 36 | |
|
| | 37 | | public bool Contains(TKey key) |
| | 38 | | { |
| 6 | 39 | | this.ThrowIfClosed(); |
| | 40 | |
|
| 4 | 41 | | return this.dictionary.TryGetValue(key, out var expiration) && expiration > DateTime.UtcNow; |
| | 42 | | } |
| | 43 | |
|
| | 44 | | public void Close() |
| | 45 | | { |
| 12 | 46 | | if (Interlocked.Exchange(ref this.closeSignaled, 1) != 0) |
| | 47 | | { |
| 6 | 48 | | return; |
| | 49 | | } |
| | 50 | |
|
| 6 | 51 | | this.closed = true; |
| | 52 | |
|
| 6 | 53 | | this.tokenSource.Cancel(); |
| 6 | 54 | | this.cleanupTaskCompletionSource.TrySetCanceled(); |
| 6 | 55 | | this.dictionary.Clear(); |
| 6 | 56 | | this.tokenSource.Dispose(); |
| 6 | 57 | | } |
| | 58 | |
|
| | 59 | | async Task CollectExpiredEntriesAsync(CancellationToken token) |
| | 60 | | { |
| 14 | 61 | | while (!token.IsCancellationRequested) |
| | 62 | | { |
| | 63 | | try |
| | 64 | | { |
| 14 | 65 | | await this.cleanupTaskCompletionSource.Task.ConfigureAwait(false); |
| 12 | 66 | | await Task.Delay(delayBetweenCleanups, token).ConfigureAwait(false); |
| 4 | 67 | | } |
| 6 | 68 | | catch (OperationCanceledException) |
| | 69 | | { |
| 6 | 70 | | return; |
| | 71 | | } |
| | 72 | |
|
| 4 | 73 | | var isEmpty = true; |
| 4 | 74 | | var utcNow = DateTime.UtcNow; |
| 16 | 75 | | foreach (var kvp in this.dictionary) |
| | 76 | | { |
| 4 | 77 | | isEmpty = false; |
| 4 | 78 | | var expiration = kvp.Value; |
| 4 | 79 | | if (utcNow > expiration) |
| | 80 | | { |
| 4 | 81 | | this.dictionaryAsCollection.Remove(kvp); |
| | 82 | | } |
| | 83 | | } |
| | 84 | |
|
| 4 | 85 | | if (isEmpty) |
| | 86 | | { |
| 0 | 87 | | this.cleanupTaskCompletionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuatio |
| | 88 | | } |
| | 89 | | } |
| 6 | 90 | | } |
| | 91 | |
|
| | 92 | | void ThrowIfClosed() |
| | 93 | | { |
| 16 | 94 | | if (closed) |
| | 95 | | { |
| 4 | 96 | | throw new ObjectDisposedException($"ConcurrentExpiringSet has already been closed. Please create a new s |
| | 97 | | } |
| 12 | 98 | | } |
| | 99 | | } |
| | 100 | | } |