优化引入和文档

This commit is contained in:
桥下红药
2026-01-18 17:00:10 +08:00
parent 60f79f7487
commit e89f814895
5 changed files with 150 additions and 10 deletions
+2 -2
View File
@@ -109,9 +109,9 @@ jobs:
- name: Build
run: |
if [ "${{ matrix.use_cross }}" = "true" ]; then
cross build --release --target ${{ matrix.target }} --examples --features mimalloc
cross build --release --target ${{ matrix.target }} --examples --features mimalloc,metrics
else
cargo build --release --target ${{ matrix.target }} --examples --features mimalloc
cargo build --release --target ${{ matrix.target }} --examples --features mimalloc,metrics
fi
shell: bash
+4
View File
@@ -42,6 +42,10 @@ jobs:
run: cargo build --examples --verbose
- name: Build examples with mimalloc
run: cargo build --examples --verbose --features mimalloc
- name: Build examples with metrics
run: cargo build --examples --verbose --features metrics
- name: Build examples with all features
run: cargo build --examples --verbose --features mimalloc,metrics
# 测试不同的 feature 组合
test-features:
+1
View File
@@ -36,6 +36,7 @@ metrics = { version = "0.24", optional = true }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt", "tracing-log"] }
mimalloc = "0.1"
metrics-exporter-prometheus = "0.18"
[features]
default = []
+128 -3
View File
@@ -1,5 +1,9 @@
# dht-crawler
[![Crates.io](https://img.shields.io/crates/v/dht-crawler.svg)](https://crates.io/crates/dht-crawler)
[![Documentation](https://docs.rs/dht-crawler/badge.svg)](https://docs.rs/dht-crawler)
[![License](https://img.shields.io/crates/l/dht-crawler.svg)](https://github.com/yourusername/dht-crawler/blob/master/LICENSE)
一个高性能的 Rust DHT(分布式哈希表)爬虫库,用于爬取 BitTorrent DHT 网络中的种子信息。
## 特性
@@ -17,9 +21,37 @@
```toml
[dependencies]
dht-crawler = "0.0.3"
dht-crawler = "0.0.4"
```
或者查看 [crates.io](https://crates.io/crates/dht-crawler) 获取最新版本号。
### 启用可选 Features
如果需要使用 Prometheus 指标支持,可以启用 `metrics` feature
```toml
[dependencies]
dht-crawler = { version = "0.0.4", features = ["metrics"] }
```
## Features
本库支持以下可选特性:
### `mimalloc` - 高性能内存分配器
使用 `mimalloc` 作为全局内存分配器,可以显著降低内存占用(通常降低 10-30%),特别适合长时间运行的高并发场景。
### `metrics` - Prometheus 指标支持
启用 `metrics` feature 后,库会通过 `metrics` crate 暴露统计指标,可以与 Prometheus 等监控系统集成。
**注意**
- `mimalloc``metrics-exporter-prometheus` 仅在编译 example 时可用(位于 `dev-dependencies`
- 使用 example 时需要显式启用对应的 features
- 库本身可以使用 `metrics` feature(通过 `[dependencies]` 中的可选依赖)
## 快速开始
### 基本使用
@@ -28,15 +60,108 @@ dht-crawler = "0.0.3"
### 编译示例
#### 基本编译(不启用任何 features)
```bash
# Debug 模式
cargo build --example dht_crawler_example
# Release 模式
cargo build --release --example dht_crawler_example
```
#### 启用 mimalloc(推荐)
```bash
# Debug 模式
cargo build --example dht_crawler_example --features mimalloc
# Release 模式(推荐用于生产环境)
cargo build --release --example dht_crawler_example --features mimalloc
```
#### 启用 metricsPrometheus 指标导出)
```bash
# 启用 metrics feature
cargo build --example dht_crawler_example --features metrics
# Release 模式
cargo build --release --example dht_crawler_example --features metrics
```
#### 同时启用 mimalloc 和 metrics
```bash
# Debug 模式
cargo build --example dht_crawler_example --features mimalloc,metrics
# Release 模式(推荐)
cargo build --release --example dht_crawler_example --features mimalloc,metrics
```
#### 交叉编译(Linux
```bash
# 编译 Linux 版本(推荐使用 mimalloc 以获得更好的内存性能)
cargo build --release --target x86_64-unknown-linux-gnu --examples --features mimalloc
cargo build --release --target x86_64-unknown-linux-gnu --examples --features mimalloc,metrics
# 编译后的可执行文件位于:
# target/x86_64-unknown-linux-gnu/release/examples/dht_crawler_example
```
使用 mimalloc 可以显著降低内存占用(通常降低 10-30%)。
### 运行示例
#### 基本运行(不使用任何 features)
```bash
cargo run --example dht_crawler_example
```
#### 启用 mimalloc 运行
```bash
cargo run --example dht_crawler_example --features mimalloc
```
#### 启用 metrics 运行
启用 metrics 后,Prometheus metrics 导出器会在 `http://localhost:9000/metrics` 启动。
```bash
cargo run --example dht_crawler_example --features metrics
```
然后在浏览器或使用 curl 访问:
```bash
curl http://localhost:9000/metrics
```
#### 同时启用 mimalloc 和 metrics(推荐)
```bash
cargo run --example dht_crawler_example --features mimalloc,metrics
```
### 在项目中使用 Features
#### 使用库本身(不带任何 features)
```toml
[dependencies]
dht-crawler = "0.0.4"
```
#### 使用库并启用 metrics feature
```toml
[dependencies]
dht-crawler = { version = "0.0.4", features = ["metrics"] }
```
这样可以在你的代码中使用库暴露的 metrics 指标。
**注意**`mimalloc` feature 仅用于 example,库本身不依赖 mimalloc。
## 许可证
+15 -5
View File
@@ -6,7 +6,10 @@ use dht_crawler::prelude::*;
use std::sync::Arc;
use tracing_subscriber::EnvFilter;
use std::sync::atomic::{AtomicUsize, Ordering};
#[cfg(feature = "metrics")]
use metrics_exporter_prometheus::PrometheusBuilder;
#[cfg(feature = "metrics")]
use std::net::SocketAddr;
#[tokio::main]
async fn main() -> Result<()> {
@@ -18,6 +21,17 @@ async fn main() -> Result<()> {
.with_ansi(true)
.init();
// 初始化 Prometheus metrics 导出器
#[cfg(feature = "metrics")]
{
let addr: SocketAddr = "0.0.0.0:9000".parse().expect("无效的地址");
PrometheusBuilder::new()
.with_http_listener(addr)
.install()
.expect("无法安装 Prometheus metrics 导出器");
log::info!("📊 Prometheus metrics 导出器已启动,访问 http://localhost:9000/metrics");
}
let options = DHTOptions {
port: 12313,
auto_metadata: true,
@@ -67,10 +81,6 @@ async fn main() -> Result<()> {
true
});
server.set_filter(|_hash| {
true
});
// 启动监控任务
let count_monitor = torrent_count.clone();
tokio::spawn(async move {