reconnect-mym #1

Merged
mym merged 7 commits from reconnect-mym into main 2025-12-12 15:50:43 +08:00
Showing only changes of commit c70c2e9f1c - Show all commits

216
README.md
View File

@@ -1,2 +1,218 @@
# reconnect # reconnect
一个通用的Go语言重连库支持Redis、PostgreSQL和etcd的自动重连功能。
## 特性
- 🔄 自动重连机制
- ⚙️ 可配置的重连策略
- 🔍 连接健康检查
- 📊 连接状态监控
- 🎯 支持多种服务Redis、PostgreSQL、etcd
- 🛡️ 线程安全
## 安装
```bash
go get github.com/blueocean-go/reconnect
```
## 快速开始
### Redis重连
```go
package main
import (
"context"
"time"
"github.com/go-redis/redis/v8"
"github.com/blueocean-go/reconnect"
)
func main() {
options := &redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
}
config := reconnect.DefaultConfig()
config.RetryInterval = 3 * time.Second
config.OnReconnect = func() {
fmt.Println("Redis重连成功!")
}
manager := reconnect.RedisManager(options, config)
ctx := context.Background()
if err := manager.Start(ctx); err != nil {
log.Fatal(err)
}
defer manager.Stop()
// 使用Redis客户端
redisClient := reconnect.NewRedisClient(options)
redisClient.Connect(ctx)
client := redisClient.GetClient()
// ... 使用client进行操作
}
```
### PostgreSQL重连
```go
package main
import (
"context"
"time"
"github.com/blueocean-go/reconnect"
)
func main() {
dsn := "host=localhost user=postgres password=postgres dbname=testdb sslmode=disable"
config := reconnect.DefaultConfig()
config.RetryInterval = 3 * time.Second
config.OnReconnect = func() {
fmt.Println("PostgreSQL重连成功!")
}
manager := reconnect.PostgresManager(
dsn,
25, // maxConnections
5, // maxIdleConnections
30*time.Minute, // maxConnectionAge
config,
)
ctx := context.Background()
if err := manager.Start(ctx); err != nil {
log.Fatal(err)
}
defer manager.Stop()
// 使用数据库连接
pgClient := reconnect.NewPostgresClient(dsn, 25, 5, 30*time.Minute)
pgClient.Connect(ctx)
db := pgClient.GetDB()
// ... 使用db进行操作
}
```
### etcd重连
```go
package main
import (
"context"
"time"
clientv3 "go.etcd.io/etcd/client/v3"
"github.com/blueocean-go/reconnect"
)
func main() {
etcdConfig := clientv3.Config{
Endpoints: []string{"localhost:2379"},
DialTimeout: 5 * time.Second,
}
config := reconnect.DefaultConfig()
config.RetryInterval = 3 * time.Second
config.OnReconnect = func() {
fmt.Println("etcd重连成功!")
}
manager := reconnect.EtcdManager(etcdConfig, config)
ctx := context.Background()
if err := manager.Start(ctx); err != nil {
log.Fatal(err)
}
defer manager.Stop()
// 使用etcd客户端
etcdClient := reconnect.NewEtcdClient(etcdConfig)
etcdClient.Connect(ctx)
client := etcdClient.GetClient()
// ... 使用client进行操作
}
```
## 配置选项
```go
type Config struct {
// MaxRetries 最大重试次数0表示无限重试
MaxRetries int
// RetryInterval 重试间隔
RetryInterval time.Duration
// Timeout 连接超时时间
Timeout time.Duration
// OnReconnect 重连成功后的回调函数
OnReconnect func()
// OnDisconnect 断开连接时的回调函数
OnDisconnect func(error)
}
```
## API文档
### Manager
重连管理器,负责监控连接状态并自动重连。
- `Start(ctx context.Context) error` - 启动连接并开始监控
- `Stop() error` - 停止重连管理器
- `IsConnected() bool` - 返回当前连接状态
- `WaitForError() error` - 等待错误(用于阻塞等待)
### RedisClient
Redis客户端包装器。
- `Connect(ctx context.Context) error` - 建立连接
- `Close() error` - 关闭连接
- `Ping(ctx context.Context) error` - 健康检查
- `IsConnected() bool` - 检查连接状态
- `GetClient() *redis.Client` - 获取底层Redis客户端
### PostgresClient
PostgreSQL客户端包装器。
- `Connect(ctx context.Context) error` - 建立连接
- `Close() error` - 关闭连接
- `Ping(ctx context.Context) error` - 健康检查
- `IsConnected() bool` - 检查连接状态
- `GetDB() *sql.DB` - 获取底层数据库连接
### EtcdClient
etcd客户端包装器。
- `Connect(ctx context.Context) error` - 建立连接
- `Close() error` - 关闭连接
- `Ping(ctx context.Context) error` - 健康检查
- `IsConnected() bool` - 检查连接状态
- `GetClient() *clientv3.Client` - 获取底层etcd客户端
## 示例
更多示例代码请查看 `examples/` 目录。
## 许可证
MIT License
Copyright (c) 2025 blueocean-go