private void RemoveAwaiter(CorrelatedAwaiter awaitableNotification) { if (!awaitableNotification.Removed) { var start = DateTime.Now; using (NamedLock.CreateAndEnter($"{_cacheKeyPrefixNamedLocks}{awaitableNotification.Key}")) { _traceWriter?.Write($"{nameof(CorrelatedAwaitManager<TMessage, TKey>)}: {nameof(RemoveAwaiter)}: Local_Lock_Acquired in {DateTime.Now.Subtract(start).TotalMilliseconds * 1000:#,##0.0}us"); if (!awaitableNotification.Removed) { if (_awaitedNotifications.TryGetValue(awaitableNotification.Key, out var hashSet)) { hashSet.Remove(awaitableNotification); if (hashSet.Count == 0) { _awaitedNotifications.TryRemove(awaitableNotification.Key, out var trash); } } awaitableNotification.Removed = true; } } } }
/// <summary> /// Returns an awaitable task interface to await a requested result / message (based on a message id / key). /// </summary> public ICorrelatedAwaiter <TMessage> CreateAwaiter(TKey key) { var awaitableNotification = new CorrelatedAwaiter(key, this); var start = DateTime.Now; using (NamedLock.CreateAndEnter($"{_cacheKeyPrefixNamedLocks}{key}")) { _traceWriter?.Write($"{nameof(CorrelatedAwaitManager<TMessage, TKey>)}: {nameof(CreateAwaiter)}: Local_Lock_Acquired in {DateTime.Now.Subtract(start).TotalMilliseconds * 1000:#,##0.0}us"); if (!_awaitedNotifications.TryGetValue(key, out var hashSet)) { _awaitedNotifications[key] = hashSet = new HashSet <CorrelatedAwaiter>(); } hashSet.Add(awaitableNotification); } return(awaitableNotification); }