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

[Enhancement]: Keylock object background recycling #38587

Open
1 task done
SimFG opened this issue Dec 19, 2024 · 1 comment
Open
1 task done

[Enhancement]: Keylock object background recycling #38587

SimFG opened this issue Dec 19, 2024 · 1 comment
Assignees
Labels
kind/enhancement Issues or changes related to enhancement

Comments

@SimFG
Copy link
Contributor

SimFG commented Dec 19, 2024

Is there an existing issue for this?

  • I have searched the existing issues

What would you like to be added?

If keylock calls lock/unlock in sequence, a large number of Lock objects will be created and GC will be frequent. Change the lock objects to be recycled regularly to reduce the frequent creation of locks and GC pressure.

func (k *KeyLock[K]) Lock(key K) {
	k.keyLocksMutex.Lock()
	// update the key map
	if keyLock, ok := k.refLocks[key]; ok {
		keyLock.ref()

		k.keyLocksMutex.Unlock()
		keyLock.mutex.Lock()
	} else {
		newKLock := newRefLock()
		newKLock.mutex.Lock()
		k.refLocks[key] = newKLock
		newKLock.ref()

		k.keyLocksMutex.Unlock()
		return
	}
}

func (k *KeyLock[K]) Unlock(lockedKey K) {
	k.keyLocksMutex.Lock()
	defer k.keyLocksMutex.Unlock()
	keyLock, ok := k.refLocks[lockedKey]
	if !ok {
		log.Warn("Unlocking non-existing key", zap.Any("key", lockedKey))
		return
	}
	keyLock.unref()
	if keyLock.refCounter == 0 {
		delete(k.refLocks, lockedKey)
	}
	keyLock.mutex.Unlock()
}

Why is this needed?

No response

Anything else?

No response

@SimFG SimFG added the kind/enhancement Issues or changes related to enhancement label Dec 19, 2024
@SimFG
Copy link
Contributor Author

SimFG commented Dec 19, 2024

/assign @SimFG

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement Issues or changes related to enhancement
Projects
None yet
Development

No branches or pull requests

1 participant