Skip to content

feat(python): add WebSocket auto-reconnect mechanism #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jacksonliuyn
Copy link
Collaborator

Summary

  • 为Python SDK添加WebSocket自动重连机制,提高网络连接稳定性
  • 实现指数退避算法和随机抖动,避免重连风暴
  • 添加断连测试用例和自动重连示例

主要改进

1. 功能增强

  • RTLowLevelClient添加重连参数:最大重试次数、初始延迟、最大延迟、随机抖动、重连回调
  • 为所有WebSocket操作添加错误处理和自动重连
  • 优化User-Agent生成,处理缺少package版本的情况

2. 新增文件

  • tests/test_low_level_client_reconnect.py: 重连机制的单元测试
  • samples/low_level_sample_with_reconnect.py: 演示重连功能的示例,包含模拟连接中断

3. 使用方法

client = RTLowLevelClient(
    url="wss://example.com/ws",
    headers={"Authorization": "Bearer token"},
    max_retries=5,               # 最大重试次数,0表示不重试,-1表示无限重试
    initial_retry_delay=1.0,     # 初始重试延迟(秒)
    max_retry_delay=30.0,        # 最大重试延迟(秒)
    retry_jitter=0.1,            # 随机抖动因子(0-1之间),避免重连风暴
    on_reconnect=on_reconnect    # 重连成功后的回调函数
)

测试

  • 已通过所有单元测试
  • 使用中断模拟工具进行了重连场景验证

🤖 Generated with Claude Code

- Implement robust reconnection with exponential backoff and jitter
- Add reconnect tests to verify the implementation
- Create a sample showcasing reconnection functionality
- Improve error handling in WebSocket operations
- Fix User-Agent generation to handle missing package version

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant