2015-05-14 16:13:45 +03:00
|
|
|
package redis_test
|
|
|
|
|
|
|
|
import (
|
2016-12-03 18:30:13 +03:00
|
|
|
"fmt"
|
2015-05-14 16:13:45 +03:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2024-01-01 23:19:22 +03:00
|
|
|
"strconv"
|
2016-03-14 14:17:33 +03:00
|
|
|
"sync"
|
2015-05-14 16:13:45 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-01-27 18:00:49 +03:00
|
|
|
. "github.com/bsm/ginkgo/v2"
|
|
|
|
. "github.com/bsm/gomega"
|
2021-09-08 16:00:52 +03:00
|
|
|
|
2023-01-23 09:48:54 +03:00
|
|
|
"github.com/redis/go-redis/v9"
|
2015-05-14 16:13:45 +03:00
|
|
|
)
|
|
|
|
|
2015-05-18 14:43:08 +03:00
|
|
|
const (
|
|
|
|
redisSecondaryPort = "6381"
|
|
|
|
)
|
2015-05-14 16:13:45 +03:00
|
|
|
|
2015-05-25 16:22:27 +03:00
|
|
|
const (
|
|
|
|
ringShard1Port = "6390"
|
|
|
|
ringShard2Port = "6391"
|
2018-06-29 10:45:05 +03:00
|
|
|
ringShard3Port = "6392"
|
2015-05-25 16:22:27 +03:00
|
|
|
)
|
|
|
|
|
2015-05-14 16:13:45 +03:00
|
|
|
const (
|
|
|
|
sentinelName = "mymaster"
|
2020-03-11 17:26:42 +03:00
|
|
|
sentinelMasterPort = "9123"
|
|
|
|
sentinelSlave1Port = "9124"
|
|
|
|
sentinelSlave2Port = "9125"
|
2020-09-03 22:11:56 +03:00
|
|
|
sentinelPort1 = "9126"
|
|
|
|
sentinelPort2 = "9127"
|
|
|
|
sentinelPort3 = "9128"
|
2015-05-14 16:13:45 +03:00
|
|
|
)
|
|
|
|
|
2023-09-20 14:03:44 +03:00
|
|
|
var (
|
|
|
|
redisPort = "6380"
|
|
|
|
redisAddr = ":" + redisPort
|
|
|
|
)
|
2023-03-06 16:31:39 +03:00
|
|
|
|
2023-12-17 13:35:10 +03:00
|
|
|
var (
|
|
|
|
rediStackPort = "6379"
|
|
|
|
rediStackAddr = ":" + rediStackPort
|
|
|
|
)
|
|
|
|
|
2015-05-25 16:22:27 +03:00
|
|
|
var (
|
2020-09-09 17:39:13 +03:00
|
|
|
sentinelAddrs = []string{":" + sentinelPort1, ":" + sentinelPort2, ":" + sentinelPort3}
|
|
|
|
|
|
|
|
processes map[string]*redisProcess
|
|
|
|
|
2020-09-03 22:11:56 +03:00
|
|
|
redisMain *redisProcess
|
|
|
|
ringShard1, ringShard2, ringShard3 *redisProcess
|
|
|
|
sentinelMaster, sentinelSlave1, sentinelSlave2 *redisProcess
|
|
|
|
sentinel1, sentinel2, sentinel3 *redisProcess
|
2015-05-25 16:22:27 +03:00
|
|
|
)
|
2015-05-14 16:13:45 +03:00
|
|
|
|
|
|
|
var cluster = &clusterScenario{
|
|
|
|
ports: []string{"8220", "8221", "8222", "8223", "8224", "8225"},
|
2019-07-25 13:53:00 +03:00
|
|
|
nodeIDs: make([]string, 6),
|
2015-05-14 16:13:45 +03:00
|
|
|
processes: make(map[string]*redisProcess, 6),
|
|
|
|
clients: make(map[string]*redis.Client, 6),
|
|
|
|
}
|
|
|
|
|
2024-01-01 23:19:22 +03:00
|
|
|
var RECluster = false
|
2024-12-16 20:04:39 +03:00
|
|
|
var USE_CONTAINERIZED_REDIS = false
|
2024-01-01 23:19:22 +03:00
|
|
|
|
2020-09-09 17:39:13 +03:00
|
|
|
func registerProcess(port string, p *redisProcess) {
|
|
|
|
if processes == nil {
|
|
|
|
processes = make(map[string]*redisProcess)
|
|
|
|
}
|
|
|
|
processes[port] = p
|
|
|
|
}
|
|
|
|
|
2015-05-14 16:13:45 +03:00
|
|
|
var _ = BeforeSuite(func() {
|
2023-03-06 16:31:39 +03:00
|
|
|
addr := os.Getenv("REDIS_PORT")
|
|
|
|
if addr != "" {
|
|
|
|
redisPort = addr
|
|
|
|
redisAddr = ":" + redisPort
|
|
|
|
}
|
2015-05-14 16:13:45 +03:00
|
|
|
var err error
|
2024-01-01 23:19:22 +03:00
|
|
|
RECluster, _ = strconv.ParseBool(os.Getenv("RE_CLUSTER"))
|
2024-12-16 20:04:39 +03:00
|
|
|
USE_CONTAINERIZED_REDIS, _ = strconv.ParseBool(os.Getenv("USE_CONTAINERIZED_REDIS"))
|
|
|
|
if !RECluster || !USE_CONTAINERIZED_REDIS {
|
2015-05-14 16:13:45 +03:00
|
|
|
|
2024-01-01 23:19:22 +03:00
|
|
|
redisMain, err = startRedis(redisPort)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2015-05-18 14:43:08 +03:00
|
|
|
|
2024-01-01 23:19:22 +03:00
|
|
|
ringShard1, err = startRedis(ringShard1Port)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2015-05-25 16:22:27 +03:00
|
|
|
|
2024-01-01 23:19:22 +03:00
|
|
|
ringShard2, err = startRedis(ringShard2Port)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2015-05-25 16:22:27 +03:00
|
|
|
|
2024-01-01 23:19:22 +03:00
|
|
|
ringShard3, err = startRedis(ringShard3Port)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2018-06-29 10:45:05 +03:00
|
|
|
|
2024-01-01 23:19:22 +03:00
|
|
|
sentinelMaster, err = startRedis(sentinelMasterPort)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2015-05-14 16:13:45 +03:00
|
|
|
|
2024-01-01 23:19:22 +03:00
|
|
|
sentinel1, err = startSentinel(sentinelPort1, sentinelName, sentinelMasterPort)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2020-09-03 22:11:56 +03:00
|
|
|
|
2024-01-01 23:19:22 +03:00
|
|
|
sentinel2, err = startSentinel(sentinelPort2, sentinelName, sentinelMasterPort)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2020-09-03 22:11:56 +03:00
|
|
|
|
2024-01-01 23:19:22 +03:00
|
|
|
sentinel3, err = startSentinel(sentinelPort3, sentinelName, sentinelMasterPort)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2015-05-14 16:13:45 +03:00
|
|
|
|
2024-01-01 23:19:22 +03:00
|
|
|
sentinelSlave1, err = startRedis(
|
|
|
|
sentinelSlave1Port, "--slaveof", "127.0.0.1", sentinelMasterPort)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2015-05-14 16:13:45 +03:00
|
|
|
|
2024-01-01 23:19:22 +03:00
|
|
|
sentinelSlave2, err = startRedis(
|
|
|
|
sentinelSlave2Port, "--slaveof", "127.0.0.1", sentinelMasterPort)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2015-05-14 16:13:45 +03:00
|
|
|
|
2024-01-01 23:19:22 +03:00
|
|
|
Expect(startCluster(ctx, cluster)).NotTo(HaveOccurred())
|
|
|
|
} else {
|
|
|
|
redisPort = rediStackPort
|
|
|
|
redisAddr = rediStackAddr
|
|
|
|
}
|
2015-05-14 16:13:45 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
var _ = AfterSuite(func() {
|
2024-01-01 23:19:22 +03:00
|
|
|
if !RECluster {
|
|
|
|
Expect(cluster.Close()).NotTo(HaveOccurred())
|
2015-06-03 16:45:46 +03:00
|
|
|
|
2024-01-01 23:19:22 +03:00
|
|
|
for _, p := range processes {
|
|
|
|
Expect(p.Close()).NotTo(HaveOccurred())
|
|
|
|
}
|
2020-09-09 17:39:13 +03:00
|
|
|
}
|
|
|
|
processes = nil
|
2015-05-14 16:13:45 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
func TestGinkgoSuite(t *testing.T) {
|
|
|
|
RegisterFailHandler(Fail)
|
2017-02-18 17:42:34 +03:00
|
|
|
RunSpecs(t, "go-redis")
|
2015-05-14 16:13:45 +03:00
|
|
|
}
|
|
|
|
|
2021-08-06 12:16:22 +03:00
|
|
|
//------------------------------------------------------------------------------
|
2015-05-14 16:13:45 +03:00
|
|
|
|
2016-03-16 17:57:24 +03:00
|
|
|
func redisOptions() *redis.Options {
|
2024-01-01 23:19:22 +03:00
|
|
|
if RECluster {
|
|
|
|
return &redis.Options{
|
|
|
|
Addr: redisAddr,
|
|
|
|
DB: 0,
|
|
|
|
|
|
|
|
DialTimeout: 10 * time.Second,
|
|
|
|
ReadTimeout: 30 * time.Second,
|
|
|
|
WriteTimeout: 30 * time.Second,
|
|
|
|
ContextTimeoutEnabled: true,
|
|
|
|
|
|
|
|
MaxRetries: -1,
|
|
|
|
PoolSize: 10,
|
|
|
|
|
|
|
|
PoolTimeout: 30 * time.Second,
|
|
|
|
ConnMaxIdleTime: time.Minute,
|
|
|
|
}
|
|
|
|
}
|
2016-03-16 17:57:24 +03:00
|
|
|
return &redis.Options{
|
2020-09-11 11:24:38 +03:00
|
|
|
Addr: redisAddr,
|
|
|
|
DB: 15,
|
|
|
|
|
2022-10-11 10:22:42 +03:00
|
|
|
DialTimeout: 10 * time.Second,
|
|
|
|
ReadTimeout: 30 * time.Second,
|
|
|
|
WriteTimeout: 30 * time.Second,
|
|
|
|
ContextTimeoutEnabled: true,
|
2020-09-11 11:24:38 +03:00
|
|
|
|
|
|
|
MaxRetries: -1,
|
|
|
|
|
2022-07-28 15:11:35 +03:00
|
|
|
PoolSize: 10,
|
|
|
|
PoolTimeout: 30 * time.Second,
|
|
|
|
ConnMaxIdleTime: time.Minute,
|
2016-10-02 15:44:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func redisClusterOptions() *redis.ClusterOptions {
|
|
|
|
return &redis.ClusterOptions{
|
2020-09-11 11:24:38 +03:00
|
|
|
DialTimeout: 10 * time.Second,
|
|
|
|
ReadTimeout: 30 * time.Second,
|
|
|
|
WriteTimeout: 30 * time.Second,
|
|
|
|
|
|
|
|
MaxRedirects: 8,
|
|
|
|
|
2022-07-28 15:11:35 +03:00
|
|
|
PoolSize: 10,
|
|
|
|
PoolTimeout: 30 * time.Second,
|
|
|
|
ConnMaxIdleTime: time.Minute,
|
2016-10-02 15:44:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func redisRingOptions() *redis.RingOptions {
|
|
|
|
return &redis.RingOptions{
|
|
|
|
Addrs: map[string]string{
|
|
|
|
"ringShardOne": ":" + ringShard1Port,
|
|
|
|
"ringShardTwo": ":" + ringShard2Port,
|
|
|
|
},
|
2020-09-11 11:24:38 +03:00
|
|
|
|
|
|
|
DialTimeout: 10 * time.Second,
|
|
|
|
ReadTimeout: 30 * time.Second,
|
|
|
|
WriteTimeout: 30 * time.Second,
|
|
|
|
|
|
|
|
MaxRetries: -1,
|
|
|
|
|
2022-07-28 15:11:35 +03:00
|
|
|
PoolSize: 10,
|
|
|
|
PoolTimeout: 30 * time.Second,
|
|
|
|
ConnMaxIdleTime: time.Minute,
|
2016-03-16 17:57:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-08 11:16:53 +03:00
|
|
|
func performAsync(n int, cbs ...func(int)) *sync.WaitGroup {
|
2016-03-14 14:17:33 +03:00
|
|
|
var wg sync.WaitGroup
|
2016-03-17 19:00:47 +03:00
|
|
|
for _, cb := range cbs {
|
2020-12-06 12:05:42 +03:00
|
|
|
wg.Add(n)
|
2016-03-17 19:00:47 +03:00
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
go func(cb func(int), i int) {
|
|
|
|
defer GinkgoRecover()
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
cb(i)
|
|
|
|
}(cb, i)
|
|
|
|
}
|
2016-03-14 14:17:33 +03:00
|
|
|
}
|
2018-03-08 11:16:53 +03:00
|
|
|
return &wg
|
|
|
|
}
|
|
|
|
|
|
|
|
func perform(n int, cbs ...func(int)) {
|
|
|
|
wg := performAsync(n, cbs...)
|
2016-03-14 14:17:33 +03:00
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2016-03-09 16:14:01 +03:00
|
|
|
func eventually(fn func() error, timeout time.Duration) error {
|
2018-08-16 13:25:19 +03:00
|
|
|
errCh := make(chan error, 1)
|
2016-03-16 17:57:24 +03:00
|
|
|
done := make(chan struct{})
|
2018-07-22 10:50:26 +03:00
|
|
|
exit := make(chan struct{})
|
2016-03-16 17:57:24 +03:00
|
|
|
|
2015-05-14 16:13:45 +03:00
|
|
|
go func() {
|
2018-07-22 10:50:26 +03:00
|
|
|
for {
|
2016-03-16 17:57:24 +03:00
|
|
|
err := fn()
|
2015-11-14 16:54:16 +03:00
|
|
|
if err == nil {
|
|
|
|
close(done)
|
2015-05-14 16:13:45 +03:00
|
|
|
return
|
|
|
|
}
|
2018-07-22 10:50:26 +03:00
|
|
|
|
2016-12-03 18:30:13 +03:00
|
|
|
select {
|
|
|
|
case errCh <- err:
|
|
|
|
default:
|
|
|
|
}
|
2018-07-22 10:50:26 +03:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-exit:
|
|
|
|
return
|
|
|
|
case <-time.After(timeout / 100):
|
|
|
|
}
|
2015-05-14 16:13:45 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
2015-11-14 16:54:16 +03:00
|
|
|
case <-done:
|
2015-05-14 16:13:45 +03:00
|
|
|
return nil
|
|
|
|
case <-time.After(timeout):
|
2018-07-22 10:50:26 +03:00
|
|
|
close(exit)
|
2016-12-03 18:30:13 +03:00
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
return err
|
|
|
|
default:
|
2018-08-16 13:25:19 +03:00
|
|
|
return fmt.Errorf("timeout after %s without an error", timeout)
|
2016-12-03 18:30:13 +03:00
|
|
|
}
|
2015-05-14 16:13:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func execCmd(name string, args ...string) (*os.Process, error) {
|
|
|
|
cmd := exec.Command(name, args...)
|
|
|
|
if testing.Verbose() {
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
}
|
|
|
|
return cmd.Process, cmd.Start()
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:44:49 +03:00
|
|
|
func connectTo(port string) (*redis.Client, error) {
|
|
|
|
client := redis.NewClient(&redis.Options{
|
2020-09-11 11:24:38 +03:00
|
|
|
Addr: ":" + port,
|
|
|
|
MaxRetries: -1,
|
2015-05-14 16:13:45 +03:00
|
|
|
})
|
|
|
|
|
2015-12-22 12:44:49 +03:00
|
|
|
err := eventually(func() error {
|
2020-03-11 17:26:42 +03:00
|
|
|
return client.Ping(ctx).Err()
|
2016-03-14 14:17:33 +03:00
|
|
|
}, 30*time.Second)
|
2015-12-22 12:44:49 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-05-14 16:13:45 +03:00
|
|
|
}
|
|
|
|
|
2015-12-22 12:44:49 +03:00
|
|
|
return client, nil
|
2015-05-14 16:13:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type redisProcess struct {
|
|
|
|
*os.Process
|
|
|
|
*redis.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *redisProcess) Close() error {
|
2015-12-22 12:44:49 +03:00
|
|
|
if err := p.Kill(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err := eventually(func() error {
|
2020-03-11 17:26:42 +03:00
|
|
|
if err := p.Client.Ping(ctx).Err(); err != nil {
|
2015-12-22 12:44:49 +03:00
|
|
|
return nil
|
|
|
|
}
|
2022-10-06 10:06:02 +03:00
|
|
|
return fmt.Errorf("client %s is not shutdown", p.Options().Addr)
|
2015-12-22 12:44:49 +03:00
|
|
|
}, 10*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-14 16:13:45 +03:00
|
|
|
p.Client.Close()
|
2015-12-22 12:44:49 +03:00
|
|
|
return nil
|
2015-05-14 16:13:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2022-06-04 17:25:12 +03:00
|
|
|
redisServerBin, _ = filepath.Abs(filepath.Join("testdata", "redis", "src", "redis-server"))
|
|
|
|
redisServerConf, _ = filepath.Abs(filepath.Join("testdata", "redis", "redis.conf"))
|
|
|
|
redisSentinelConf, _ = filepath.Abs(filepath.Join("testdata", "redis", "sentinel.conf"))
|
2015-05-14 16:13:45 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func redisDir(port string) (string, error) {
|
2016-03-12 13:25:59 +03:00
|
|
|
dir, err := filepath.Abs(filepath.Join("testdata", "instances", port))
|
2015-05-14 16:13:45 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2015-12-22 12:44:49 +03:00
|
|
|
}
|
|
|
|
if err := os.RemoveAll(dir); err != nil {
|
2015-05-14 16:13:45 +03:00
|
|
|
return "", err
|
2015-12-22 12:44:49 +03:00
|
|
|
}
|
2021-09-08 16:00:52 +03:00
|
|
|
if err := os.MkdirAll(dir, 0o775); err != nil {
|
2015-05-14 16:13:45 +03:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return dir, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func startRedis(port string, args ...string) (*redisProcess, error) {
|
|
|
|
dir, err := redisDir(port)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-06-04 17:25:12 +03:00
|
|
|
|
|
|
|
if err := exec.Command("cp", "-f", redisServerConf, dir).Run(); err != nil {
|
2015-05-14 16:13:45 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-18 16:03:47 +03:00
|
|
|
baseArgs := []string{filepath.Join(dir, "redis.conf"), "--port", port, "--dir", dir, "--enable-module-command", "yes"}
|
2015-05-14 16:13:45 +03:00
|
|
|
process, err := execCmd(redisServerBin, append(baseArgs, args...)...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := connectTo(port)
|
|
|
|
if err != nil {
|
|
|
|
process.Kill()
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-09 17:39:13 +03:00
|
|
|
|
|
|
|
p := &redisProcess{process, client}
|
|
|
|
registerProcess(port, p)
|
2022-06-04 14:52:46 +03:00
|
|
|
return p, nil
|
2015-05-14 16:13:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func startSentinel(port, masterName, masterPort string) (*redisProcess, error) {
|
|
|
|
dir, err := redisDir(port)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-09 17:39:13 +03:00
|
|
|
|
2022-06-04 17:25:12 +03:00
|
|
|
sentinelConf := filepath.Join(dir, "sentinel.conf")
|
|
|
|
if err := os.WriteFile(sentinelConf, nil, 0o644); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
process, err := execCmd(redisServerBin, sentinelConf, "--sentinel", "--port", port, "--dir", dir)
|
2015-05-14 16:13:45 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-09 17:39:13 +03:00
|
|
|
|
2015-05-14 16:13:45 +03:00
|
|
|
client, err := connectTo(port)
|
|
|
|
if err != nil {
|
|
|
|
process.Kill()
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-09 17:39:13 +03:00
|
|
|
|
2021-03-12 14:06:11 +03:00
|
|
|
// set down-after-milliseconds=2000
|
|
|
|
// link: https://github.com/redis/redis/issues/8607
|
2015-05-14 16:13:45 +03:00
|
|
|
for _, cmd := range []*redis.StatusCmd{
|
2020-09-03 22:11:56 +03:00
|
|
|
redis.NewStatusCmd(ctx, "SENTINEL", "MONITOR", masterName, "127.0.0.1", masterPort, "2"),
|
2021-03-12 14:06:11 +03:00
|
|
|
redis.NewStatusCmd(ctx, "SENTINEL", "SET", masterName, "down-after-milliseconds", "2000"),
|
2020-03-11 17:26:42 +03:00
|
|
|
redis.NewStatusCmd(ctx, "SENTINEL", "SET", masterName, "failover-timeout", "1000"),
|
|
|
|
redis.NewStatusCmd(ctx, "SENTINEL", "SET", masterName, "parallel-syncs", "1"),
|
2015-05-14 16:13:45 +03:00
|
|
|
} {
|
2020-03-11 17:26:42 +03:00
|
|
|
client.Process(ctx, cmd)
|
2015-05-14 16:13:45 +03:00
|
|
|
if err := cmd.Err(); err != nil {
|
|
|
|
process.Kill()
|
2022-06-04 17:25:12 +03:00
|
|
|
return nil, fmt.Errorf("%s failed: %w", cmd, err)
|
2015-05-14 16:13:45 +03:00
|
|
|
}
|
|
|
|
}
|
2020-09-09 17:39:13 +03:00
|
|
|
|
|
|
|
p := &redisProcess{process, client}
|
|
|
|
registerProcess(port, p)
|
|
|
|
return p, nil
|
2015-05-14 16:13:45 +03:00
|
|
|
}
|
|
|
|
|
2021-08-06 12:16:22 +03:00
|
|
|
//------------------------------------------------------------------------------
|
2015-05-14 16:13:45 +03:00
|
|
|
|
2015-10-13 12:02:29 +03:00
|
|
|
type badConnError string
|
|
|
|
|
|
|
|
func (e badConnError) Error() string { return string(e) }
|
2020-07-24 14:57:12 +03:00
|
|
|
func (e badConnError) Timeout() bool { return true }
|
2015-10-13 12:02:29 +03:00
|
|
|
func (e badConnError) Temporary() bool { return false }
|
2015-05-14 16:13:45 +03:00
|
|
|
|
2015-09-06 13:50:16 +03:00
|
|
|
type badConn struct {
|
|
|
|
net.TCPConn
|
2015-05-14 16:13:45 +03:00
|
|
|
|
2015-09-06 13:50:16 +03:00
|
|
|
readDelay, writeDelay time.Duration
|
|
|
|
readErr, writeErr error
|
2015-05-14 16:13:45 +03:00
|
|
|
}
|
|
|
|
|
2015-09-06 13:50:16 +03:00
|
|
|
var _ net.Conn = &badConn{}
|
|
|
|
|
2019-08-16 17:45:45 +03:00
|
|
|
func (cn *badConn) SetReadDeadline(t time.Time) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cn *badConn) SetWriteDeadline(t time.Time) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-06 13:50:16 +03:00
|
|
|
func (cn *badConn) Read([]byte) (int, error) {
|
|
|
|
if cn.readDelay != 0 {
|
|
|
|
time.Sleep(cn.readDelay)
|
|
|
|
}
|
|
|
|
if cn.readErr != nil {
|
|
|
|
return 0, cn.readErr
|
|
|
|
}
|
2015-10-13 12:02:29 +03:00
|
|
|
return 0, badConnError("bad connection")
|
2015-05-14 16:13:45 +03:00
|
|
|
}
|
|
|
|
|
2015-09-06 13:50:16 +03:00
|
|
|
func (cn *badConn) Write([]byte) (int, error) {
|
|
|
|
if cn.writeDelay != 0 {
|
|
|
|
time.Sleep(cn.writeDelay)
|
|
|
|
}
|
|
|
|
if cn.writeErr != nil {
|
|
|
|
return 0, cn.writeErr
|
|
|
|
}
|
2015-10-13 12:02:29 +03:00
|
|
|
return 0, badConnError("bad connection")
|
2015-05-14 16:13:45 +03:00
|
|
|
}
|
2020-02-14 15:30:07 +03:00
|
|
|
|
2021-08-06 12:16:22 +03:00
|
|
|
//------------------------------------------------------------------------------
|
2020-02-14 15:30:07 +03:00
|
|
|
|
|
|
|
type hook struct {
|
2022-10-11 15:37:34 +03:00
|
|
|
dialHook func(hook redis.DialHook) redis.DialHook
|
|
|
|
processHook func(hook redis.ProcessHook) redis.ProcessHook
|
|
|
|
processPipelineHook func(hook redis.ProcessPipelineHook) redis.ProcessPipelineHook
|
2020-02-14 15:30:07 +03:00
|
|
|
}
|
|
|
|
|
2022-10-11 15:37:34 +03:00
|
|
|
func (h *hook) DialHook(hook redis.DialHook) redis.DialHook {
|
|
|
|
if h.dialHook != nil {
|
|
|
|
return h.dialHook(hook)
|
2020-02-14 15:30:07 +03:00
|
|
|
}
|
2022-10-11 15:37:34 +03:00
|
|
|
return hook
|
2020-02-14 15:30:07 +03:00
|
|
|
}
|
|
|
|
|
2022-10-11 15:37:34 +03:00
|
|
|
func (h *hook) ProcessHook(hook redis.ProcessHook) redis.ProcessHook {
|
|
|
|
if h.processHook != nil {
|
|
|
|
return h.processHook(hook)
|
2020-02-14 15:30:07 +03:00
|
|
|
}
|
2022-10-11 15:37:34 +03:00
|
|
|
return hook
|
2020-02-14 15:30:07 +03:00
|
|
|
}
|
|
|
|
|
2022-10-11 15:37:34 +03:00
|
|
|
func (h *hook) ProcessPipelineHook(hook redis.ProcessPipelineHook) redis.ProcessPipelineHook {
|
|
|
|
if h.processPipelineHook != nil {
|
|
|
|
return h.processPipelineHook(hook)
|
2020-02-14 15:30:07 +03:00
|
|
|
}
|
2022-10-11 15:37:34 +03:00
|
|
|
return hook
|
2020-02-14 15:30:07 +03:00
|
|
|
}
|