mirror of
https://gitee.com/milvus-io/milvus.git
synced 2024-12-04 21:09:06 +08:00
233bf90c55
Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>
33 lines
730 B
Go
33 lines
730 B
Go
package syncmgr
|
|
|
|
import (
|
|
"github.com/milvus-io/milvus/pkg/util/conc"
|
|
"github.com/milvus-io/milvus/pkg/util/lock"
|
|
)
|
|
|
|
type Task interface {
|
|
Run() error
|
|
}
|
|
|
|
type keyLockDispatcher[K comparable] struct {
|
|
keyLock *lock.KeyLock[K]
|
|
workerPool *conc.Pool[struct{}]
|
|
}
|
|
|
|
func newKeyLockDispatcher[K comparable](maxParallel int) *keyLockDispatcher[K] {
|
|
return &keyLockDispatcher[K]{
|
|
workerPool: conc.NewPool[struct{}](maxParallel, conc.WithPreAlloc(true)),
|
|
keyLock: lock.NewKeyLock[K](),
|
|
}
|
|
}
|
|
|
|
func (d *keyLockDispatcher[K]) Submit(key K, t Task) *conc.Future[struct{}] {
|
|
d.keyLock.Lock(key)
|
|
defer d.keyLock.Unlock(key)
|
|
|
|
return d.workerPool.Submit(func() (struct{}, error) {
|
|
err := t.Run()
|
|
return struct{}{}, err
|
|
})
|
|
}
|