private void CleanUpFields() { _logger.LogDebug("Searching for items to clean up."); var currTickCount = Environment.TickCount; var toRemove = Shield.InTransaction(() => _freshIndex .Where(i => i.Item.RemovableSince.HasValue ? unchecked (currTickCount - i.Item.RemovableSince.Value) > Configuration.RemovableItemLingerMs : i.Item.ExpiresInMs <= 0) .Select(i => i.Item) .ToArray()); _logger.LogDebug("Found {ToRemoveCount} items to clean up.", toRemove.Length); Shield.InTransaction(() => { foreach (var item in toRemove) { if (_local.TryGetValue(item.Key, out var mi) && mi == item) { if (item.RemovableSince.HasValue) { _local.Remove(item.Key); } else { Expire(item); } } } }); }
private TimerCallback GetDeletableTimerMethod() { var lockObj = new object(); return(_ => { bool lockTaken = false; try { Monitor.TryEnter(lockObj, ref lockTaken); if (!lockTaken) { return; } var currTickCount = Environment.TickCount; var toRemove = Shield.InTransaction(() => _freshIndex .Where(i => i.Item.RemovableSince.HasValue ? unchecked (currTickCount - i.Item.RemovableSince.Value) > Configuration.RemovableItemLingerMs : i.Item.ExpiresInMs <= 0) .Select(i => i.Item) .ToArray()); Shield.InTransaction(() => { foreach (var item in toRemove) { if (_local.TryGetValue(item.Key, out var mi) && mi == item) { if (item.RemovableSince.HasValue) { _local.Remove(item.Key); } else { Expire(item); } } } }); } catch (Exception ex) { RaiseError(new GossipBackendException("Unexpected error on deletable timer task.", ex)); } finally { if (lockTaken) { Monitor.Exit(lockObj); } } }); }