ProxyManager class implements thread-safe round-robin load balancing across multiple proxies.
Overview
The proxy system supports:- Round-robin rotation: Automatically cycles through proxies for each request
- Thread-safe operation: Concurrent requests use different proxies
- Authentication encoding: URL-encodes proxy credentials automatically
- Direct connection fallback: Optionally include host IP in rotation
- Selective proxying: Use proxies only for API requests, not downloads
Proxy File Format
Create a text file (e.g.,proxies.txt) with one proxy URL per line:
proxies.txt
Supported Formats
http://host:port- HTTP proxyhttps://host:port- HTTPS proxysocks5://host:port- SOCKS5 proxyhttp://user:pass@host:port- Proxy with authentication
Authentication Encoding
Proxy credentials are automatically URL-encoded. Special characters in usernames/passwords are handled correctly:Configuration Options
Basic Setup
Set the proxy file path in your.env:
.env
Data-Only Proxying
Use proxies only for TikTok API requests, not media downloads:.env
Include Host Connection
Add direct (no proxy) connection to rotation:.env
Rotation Strategy
The proxy manager uses round-robin load balancing:- First request: Uses proxy1
- Second request: Uses proxy2
- Third request: Uses proxy3
- Fourth request: Back to proxy1
Retry Integration
Proxies rotate on retry failures:Implementation Details
TheProxyManager class (tiktok_api/proxy_manager.py:14) provides:
Thread Safety
Uses locks to ensure concurrent requests get different proxies:Singleton Pattern
Initialized once at startup:Methods
get_next_proxy()- Get next proxy in rotationget_proxy_count()- Total proxies in rotationhas_proxies()- Check if proxies configuredpeek_current()- View current proxy without rotating
Testing Proxies
Verify proxies work before deployment:Best Practices
Proxy count
Proxy count
Use 3-10 proxies for optimal performance. More proxies = better rate limit distribution.
Geographic diversity
Geographic diversity
Use proxies from different regions to bypass regional restrictions.
Residential proxies
Residential proxies
Residential proxies work better than datacenter IPs for TikTok.
Monitor failures
Monitor failures
Check logs for proxy failures. Remove non-working proxies from the file.
Rotation speed
Rotation speed
With high request volume, even 3 proxies provide good distribution.
Troubleshooting
Proxy Not Found Error
PROXY_FILE path is correct and file exists.
No Proxies Loaded
Authentication Failures
Connection Timeouts
Solution: Test proxy manually withcurl. Remove slow/dead proxies from file.
Performance Impact
| Configuration | API Speed | Download Speed | Rate Limits |
|---|---|---|---|
| No proxies | Fast | Fast | High risk |
| Proxies (all) | Medium | Slow | Low risk |
| Proxies (data only) | Medium | Fast | Low risk |
PROXY_DATA_ONLY=true for best balance of speed and reliability.
