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

[Optimization suggestions] Script.Run func #3186

Open
ppanphper opened this issue Nov 9, 2024 · 2 comments
Open

[Optimization suggestions] Script.Run func #3186

ppanphper opened this issue Nov 9, 2024 · 2 comments

Comments

@ppanphper
Copy link

ppanphper commented Nov 9, 2024

func (s *Script) Run(ctx context.Context, c Scripter, keys []string, args ...interface{}) *Cmd {
	r := s.EvalSha(ctx, c, keys, args...)
	if HasErrorPrefix(r.Err(), "NOSCRIPT") {
		return s.Eval(ctx, c, keys, args...)
	}
	return r
}

like this:

func (s *Script) Run(ctx context.Context, c Scripter, keys []string, args ...interface{}) *Cmd {
        retryTimes := 1
Retry: 
	r := s.EvalSha(ctx, c, keys, args...)
	if HasErrorPrefix(r.Err(), "NOSCRIPT") {
	        retryTimes--
		if retryTimes >= 0 {
			if sc := s.Load(ctx, c); sc.Err() != nil {
				return r
			}
			goto Retry
		}
	}
	return r
}
image

However, since each command uses a different connection, this function cannot be implemented. Is there any way to optimize it?
This optimization can improve some performance in high-concurrency scenarios, or when the Lua script is larger.

The usage environment is in a cloud vendor, with multiple agents and multiple nodes.

@ppanphper
Copy link
Author

ppanphper commented Nov 11, 2024

Unless you use client.Conn() to get a sticky connection externally, and then use

func (s *Script) Run(ctx context.Context, c Scripter, keys []string, args ...interface{}) *Cmd {
        retryTimes := 1
Retry: 
	r := s.EvalSha(ctx, c, keys, args...)
	if HasErrorPrefix(r.Err(), "NOSCRIPT") {
	        retryTimes--
		if retryTimes >= 0 {
			if sc := s.Load(ctx, c); sc.Err() != nil {
				return r
			}
			goto Retry
		}
	}
	return r
}

@ppanphper
Copy link
Author

type UniversalClient interface {
Conn(ctx context.Context) *Conn // Can you add this method?
}

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