Skip to content
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

NewClusterClient.Get context does not take effect #3199

Open
fengcunwu opened this issue Nov 26, 2024 · 0 comments
Open

NewClusterClient.Get context does not take effect #3199

fengcunwu opened this issue Nov 26, 2024 · 0 comments

Comments

@fengcunwu
Copy link

fengcunwu commented Nov 26, 2024

When I use go-redis v8 NewClusterClient, create a client and use Get to get the key data, and set the context timeout, the context takes effect, but the Get command takes longer than the context timeout

func TestGoRedis(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.TODO(), 200*time.Millisecond)
	defer cancel()
	client := goredisv8.NewClusterClient(&goredisv8.ClusterOptions{
		Addrs:      strings.Split("127.0.0.1:30001", ";"),
		MaxRetries: 0,
	})

	now := time.Now()
	fmt.Println(client.Get(ctx, "test").Result())
	fmt.Println(time.Now().Sub(now).Milliseconds())
}

demo As above, the result I get is:

=== RUN TestGoRedis
context deadline exceeded
603ms
--- PASS: TestGoRedis (0.60s)
PASS

gomod: github.com/go-redis/redis/v8 v8.11.5

127.0.0.1:30001 is available. In the Get function, I pause the program for 500ms when getting the link to simulate the delay time of the redis cluster. The code is as follows

func (p *ConnPool) popIdle() (*Conn, error) {
	if p.closed() {
		return nil, ErrClosed
	}
	n := len(p.idleConns)
	if n == 0 {
		return nil, nil
	}

	var cn *Conn
	if p.opt.PoolFIFO {
		cn = p.idleConns[0]
		copy(p.idleConns, p.idleConns[1:])
		p.idleConns = p.idleConns[:n-1]
	} else {
		idx := n - 1
		cn = p.idleConns[idx]
		p.idleConns = p.idleConns[:idx]
		time.Sleep(500*time.Millisecond)
	}
	p.idleConnsLen--
	p.checkMinIdleConns()
	return cn, nil
}
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

No branches or pull requests

1 participant