go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

the owner can be passed in the function parameter of the RedisLock constructor

Open xixiwang12138 opened this issue 2 years ago • 3 comments
trafficstars

Is your feature request related to a problem? Please describe.

  • Random number generation requires a certain cost, so the current implementation of distributed locks may not perform well in high concurrency situations
  • At same time, in some cases (exactly what I have encountered so far), the owner may just be the process id or other fixed value

Describe the solution you'd like

  • improved RedisLock constructor using "option" style
type Option func(*RedisLock)

func WithOwner(owner string) Option {
	return func(lock *RedisLock) {
		lock.owner = owner
	}
}

func WithExpireAt(expire time.Time) Option {
	return func(lock *RedisLock) {
		lock.expiredAt = expire
	}
}

func NewRedisLock(key string, options ...Option) *RedisLock {
	r := &RedisLock{
		key:       key,
	}
	for _, option := range options {
		option(r)
	}
	return r
}

xixiwang12138 avatar Apr 11 '23 13:04 xixiwang12138

Thanks for your suggestion!

Any benchmark to talk about the performance?

kevwan avatar Apr 18 '23 14:04 kevwan

Thanks for your suggestion!

Any benchmark to talk about the performance?

Because the generation of random numbers will consume a certain amount of CPU, I am looking forward to more flexible input parameters. This brings two benefits: one is that it can reduce CPU consumption, and the other is that I need to specify the owner myself in some business scenarios. Such a change is also compatible with the old version, if the owner is not passed in, then generate a random number

xixiwang12138 avatar Apr 20 '23 02:04 xixiwang12138

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Apr 20 '24 01:04 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Jul 19 '24 01:07 github-actions[bot]