Skip to content

Geographic Detection

Turbo CDN automatically detects your geographic region to select optimal CDN mirrors.

How It Works

Detection Flow

┌─────────────────────────────────────────────────────────────┐
│                    Geographic Detection                      │
├─────────────────────────────────────────────────────────────┤
│  1. Check Cache                                              │
│     └─ If valid cached region exists, use it                │
│                                                              │
│  2. IP Geolocation (Primary)                                │
│     ├─ ip-api.com                                           │
│     ├─ ipinfo.io                                            │
│     └─ Multiple fallback APIs                               │
│                                                              │
│  3. Network Performance Testing (Fallback)                  │
│     ├─ Test latency to regional servers                     │
│     └─ Select region with lowest latency                    │
│                                                              │
│  4. Cache Result                                             │
│     └─ Store for future requests                            │
└─────────────────────────────────────────────────────────────┘

Supported Regions

RegionCodeDescription
ChinaChinaMainland China with specialized mirrors
Asia PacificAsiaPacificJapan, Korea, Southeast Asia, etc.
EuropeEuropeEuropean countries
North AmericaNorthAmericaUS, Canada, Mexico
GlobalGlobalDefault for other regions

Region-Specific Optimizations

China Region

Turbo CDN provides comprehensive mirror coverage for users in China:

ServiceMirrors
GitHubghfast.top, gh.con.sh, cors.isteed.cc, github.moeyy.xyz, mirror.ghproxy.com, ghproxy.net
PyPITsinghua, Aliyun, Douban
Crates.ioTsinghua, USTC
Go Modulesgoproxy.cn, Aliyun
Docker HubUSTC, NetEase, Docker China
MavenAliyun, Tsinghua

Global Regions

For users outside China:

ServiceCDN Nodes
jsDelivrfastly, gcore, testingcf, jsdelivr.b-cdn
CloudflareGlobal edge network
FastlyHigh-performance CDN
unpkgnpm package distribution

Configuration

Automatic Detection (Default)

rust
use turbo_cdn::*;

#[tokio::main]
async fn main() -> turbo_cdn::Result<()> {
    // Region is automatically detected
    let downloader = TurboCdn::new().await?;
    
    // Download uses optimal mirrors for detected region
    let result = downloader.download_from_url("https://example.com/file.zip").await?;
    Ok(())
}

Manual Region Setting

rust
use turbo_cdn::*;

#[tokio::main]
async fn main() -> turbo_cdn::Result<()> {
    let downloader = TurboCdn::builder()
        .with_region(Region::China)  // Explicitly set region
        .build()
        .await?;
    
    let result = downloader.download_from_url("https://example.com/file.zip").await?;
    Ok(())
}

Available Regions

rust
pub enum Region {
    China,
    AsiaPacific,
    Europe,
    NorthAmerica,
    Global,
}

Caching

Geographic detection results are cached to avoid repeated API calls:

  • Cache Duration: Configurable (default: 1 hour)
  • Cache Storage: In-memory with optional persistence
  • Cache Invalidation: Automatic on network change detection

API Fallbacks

Turbo CDN uses multiple IP geolocation APIs with automatic fallback:

  1. Primary: ip-api.com (free, no key required)
  2. Secondary: ipinfo.io (free tier available)
  3. Tertiary: Network latency testing

If all IP-based detection fails, the system falls back to network performance testing, measuring latency to known servers in each region.

Performance Impact

ScenarioDetection Time
Cached result< 1ms
IP geolocation50-200ms
Network testing500-2000ms

The caching system ensures that geographic detection overhead is minimal for most operations.

Troubleshooting

Detection Issues

If region detection seems incorrect:

  1. Check network: Ensure stable internet connection
  2. VPN/Proxy: May affect IP-based detection
  3. Manual override: Set region explicitly if needed
rust
// Force a specific region
let downloader = TurboCdn::builder()
    .with_region(Region::China)
    .build()
    .await?;

Logging

Enable debug logging to see detection details:

bash
RUST_LOG=turbo_cdn=debug turbo-cdn dl "https://example.com/file.zip"

Next Steps

Released under the MIT License.