private void ClearExpireMessage(int clearSize = 1000) { var startTime = DateTime.Now; var rmList = _mappingInfo.ToArray().OrderBy(t => t.Value.LastTime).Take(clearSize).ToDictionary(kv => kv.Value.InfoHash, kv => new { kv.Key, kv.Value.LastTime }); foreach (var kv in rmList) { var infohash = kv.Key; var msgId = kv.Value; if (_mappingInfo.TryRemove(msgId.Key, out var map)) { _bucket.Add(msgId.Key); } if (_idMappingInfo.TryRemove(infohash, out var idMap)) { if (idMap.Count > 0 && (DateTime.Now - msgId.LastTime).TotalSeconds > _expireSeconds)//过期了的消息节点过滤 { foreach (var peer in idMap.GetPeers()) { _filter.Add(peer); } } } } foreach (var mapInfo in _idMappingInfo) { if (mapInfo.Value.Count <= 0) { _idMappingInfo.TryRemove(mapInfo.Key, out var rm); } } log.Info($"清理过期的命令ID,清理后可用命令ID数:{_bucket.Count},用时:{(DateTime.Now - startTime).TotalSeconds}"); }