ProxyManager Class
TheProxyManager class implements singleton pattern with thread-safe round-robin rotation:
tiktok_api/proxy_manager.py:13-176
Initialization
Proxy manager is initialized once at bot startup inmain.py:
Proxy File Format
One proxy URL per line with support for comments:http://host:porthttp://user:pass@host:porthttps://user:pass@host:portsocks5://user:pass@host:port
Round-Robin Rotation
Proxies are rotated using a thread-safe index:Rotation Example
Given proxy file:INCLUDE_HOST=true:
Integration with TikTokClient
Client Initialization
ProxySession Pattern
Each request creates aProxySession that maintains sticky proxy state:
ProxySession gets the next proxy from rotation only once (lazily on first use), then sticks with it across all 3 parts unless rotate_proxy() is called.
Proxy Rotation Flow
Data-Only Proxy Mode
Optionally use proxies only for API requests, not media downloads:- Proxies have bandwidth limits
- CDN doesn’t block datacenter IPs
- Media download is faster without proxy
Session Pooling by Proxy
curl_cffi sessions are pooled by proxy URL to prevent proxy contamination:Monitoring and Debugging
Proxy Count
Current Proxy (Peek)
Check If Proxies Configured
Safe Logging (Strip Auth)
Proxy URLs are logged without credentials:Singleton Pattern
Initialize Once
Get Instance Anywhere
Reset (Testing Only)
Error Handling
Missing Proxy File
If proxy file doesn’t exist:Empty Proxy File
If no valid proxies are loaded:Load Errors
File read errors are caught and logged:Configuration Reference
Environment Variables
Config Structure
Best Practices
- Use include_host=false for production - Only use direct connection if proxies are for API only
- Monitor proxy performance - Remove proxies that consistently fail
- Rotate credentials - Change proxy passwords periodically
- Use residential proxies - Datacenter IPs often blocked by TikTok
- Balance load - Ensure enough proxies to distribute load
- Test proxy file - Validate format before deployment
Related Components
- 3-Part Retry Strategy - How proxies are used with retries
- Architecture Overview - System-wide resource management
- Source:
tiktok_api/proxy_manager.py(full implementation)

