public GrpcPeer(GrpcClient client, DnsEndPoint remoteEndpoint, PeerConnectionInfo peerConnectionInfo) { _channel = client.Channel; _client = client.Client; RemoteEndpoint = remoteEndpoint; Info = peerConnectionInfo; _knownTransactionCache = new BoundedExpirationCache(TransactionCacheMaxItems, QueuedItemTimeout); _knownBlockCache = new BoundedExpirationCache(BlockCacheMaxItems, QueuedItemTimeout); _recentRequestsRoundtripTimes = new ConcurrentDictionary <string, ConcurrentQueue <RequestMetric> >(); RecentRequestsRoundtripTimes = new ReadOnlyDictionary <string, ConcurrentQueue <RequestMetric> >(_recentRequestsRoundtripTimes); _recentRequestsRoundtripTimes.TryAdd(nameof(MetricNames.Announce), new ConcurrentQueue <RequestMetric>()); _recentRequestsRoundtripTimes.TryAdd(nameof(MetricNames.GetBlock), new ConcurrentQueue <RequestMetric>()); _recentRequestsRoundtripTimes.TryAdd(nameof(MetricNames.GetBlocks), new ConcurrentQueue <RequestMetric>()); _sendAnnouncementJobs = new ActionBlock <StreamJob>(SendStreamJobAsync, new ExecutionDataflowBlockOptions { BoundedCapacity = NetworkConstants.DefaultMaxBufferedAnnouncementCount }); _sendBlockJobs = new ActionBlock <StreamJob>(SendStreamJobAsync, new ExecutionDataflowBlockOptions { BoundedCapacity = NetworkConstants.DefaultMaxBufferedBlockCount }); _sendTransactionJobs = new ActionBlock <StreamJob>(SendStreamJobAsync, new ExecutionDataflowBlockOptions { BoundedCapacity = NetworkConstants.DefaultMaxBufferedTransactionCount }); }
public async Task Test_Expiration() { var hash = HashHelper.ComputeFrom("hello_world"); var cache = new BoundedExpirationCache(10, 4); // 4ms timeout cache.TryAdd(hash); await Task.Delay(TimeSpan.FromSeconds(1)); cache.HasHash(hash, false).ShouldBeTrue(); cache.HasHash(hash).ShouldBeFalse(); }
public async Task Test_MultiItem_Expiration() { int cacheCapacity = 10; int timeout = 1_000; var cache = new BoundedExpirationCache(cacheCapacity, timeout); List <string> hashStrings = new List <string>(); for (int i = 0; i < cacheCapacity; i++) { var current = $"hello_world_{i}"; hashStrings.Add(current); cache.TryAdd(HashHelper.ComputeFrom(current)).ShouldBeTrue(); } await Task.Delay(TimeSpan.FromMilliseconds(timeout + 500)); cache.TryAdd(HashHelper.ComputeFrom($"hello_world_{cacheCapacity}")).ShouldBeTrue(); foreach (string hashString in hashStrings) { cache.HasHash(HashHelper.ComputeFrom(hashString)).ShouldBeFalse(); } }