mirror of
https://github.com/datarhei/core.git
synced 2025-12-24 13:07:56 +08:00
Remove pressure from raft while acquiring a lock
This commit is contained in:
@@ -50,7 +50,7 @@ func (s *storage) unprefixKey(key string) string {
|
||||
func (s *storage) Lock(ctx context.Context, name string) error {
|
||||
s.logger.Debug().WithField("name", name).Log("StorageLock")
|
||||
for {
|
||||
lock, err := s.kvs.CreateLock(s.prefixKey(name), time.Now().Add(time.Minute))
|
||||
lock, err := s.kvs.CreateLock(s.prefixKey(name), time.Now().Add(5*time.Minute))
|
||||
if err == nil {
|
||||
go func() {
|
||||
<-lock.Expired()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -27,6 +28,10 @@ func (c *cluster) CreateLock(origin string, name string, validUntil time.Time) (
|
||||
return l, nil
|
||||
}
|
||||
|
||||
if c.store.HasLock(name) {
|
||||
return nil, fmt.Errorf("the lock '%s' already exists", name)
|
||||
}
|
||||
|
||||
cmd := &store.Command{
|
||||
Operation: store.OpCreateLock,
|
||||
Data: &store.CommandCreateLock{
|
||||
|
||||
@@ -31,6 +31,7 @@ type Store interface {
|
||||
ListPolicies() Policies
|
||||
ListUserPolicies(name string) Policies
|
||||
|
||||
HasLock(name string) bool
|
||||
ListLocks() map[string]time.Time
|
||||
|
||||
ListKVS(prefix string) map[string]Value
|
||||
@@ -877,6 +878,15 @@ func (s *store) GetProcessNodeMap() map[string]string {
|
||||
return m
|
||||
}
|
||||
|
||||
func (s *store) HasLock(name string) bool {
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
|
||||
_, ok := s.data.Locks[name]
|
||||
|
||||
return ok
|
||||
}
|
||||
|
||||
func (s *store) ListLocks() map[string]time.Time {
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
|
||||
Reference in New Issue
Block a user