34 lines
706 B
Go
34 lines
706 B
Go
package redis
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
func NewClient(url string) (*redis.Client, error) {
|
|
opt, err := redis.ParseURL(url)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("разбор URL: %w", err)
|
|
}
|
|
|
|
opt.DialTimeout = 700 * time.Millisecond
|
|
opt.ReadTimeout = 700 * time.Millisecond
|
|
opt.WriteTimeout = 700 * time.Millisecond
|
|
opt.MaxRetries = 1
|
|
opt.PoolSize = 10
|
|
opt.ConnMaxIdleTime = time.Minute
|
|
|
|
client := redis.NewClient(opt)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
defer cancel()
|
|
if err := client.Ping(ctx).Err(); err != nil {
|
|
_ = client.Close()
|
|
return nil, fmt.Errorf("ping: %w", err)
|
|
}
|
|
return client, nil
|
|
}
|