Skip to content

Commit

Permalink
Merge pull request #59 from RealFax/dev
Browse files Browse the repository at this point in the history
sync
  • Loading branch information
PotatoCloud authored Nov 9, 2023
2 parents 9bec94f + b6d879f commit c2aa915
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
6 changes: 3 additions & 3 deletions locker/mutex.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package locker

import (
"errors"
"github.com/RealFax/RedQueen/internal/hack"
"time"

Expand All @@ -18,7 +19,7 @@ func MutexLock(lockID string, ttl int32, backend Backend) error {
return uint32(ttl)
}(),
); err != nil {
if err == store.ErrKeyAlreadyExists {
if errors.Is(err, store.ErrKeyAlreadyExists) {
return ErrStatusBusy
}
return err
Expand All @@ -27,8 +28,7 @@ func MutexLock(lockID string, ttl int32, backend Backend) error {
}

func MutexUnlock(lockID string, backend Backend) error {
val, err := backend.Get(hack.String2Bytes(lockID))
if len(val.Data) == 0 || err == store.ErrKeyNotFound {
if _, err := backend.Get(hack.String2Bytes(lockID)); errors.Is(err, store.ErrKeyNotFound) {
return ErrStatusBusy
}
return backend.Del(hack.String2Bytes(lockID))
Expand Down
6 changes: 4 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,13 @@ func NewServer(cfg *config.Config) (*Server, error) {
return clusters
}(),
}); err != nil {
return nil, err
return nil, errors.Wrap(err, "NewServer")
}

// init distributed lock backend
server.lockerBackend = NewLockerBackend(server.store, server.applyLog)
if server.lockerBackend, err = NewLockerBackend(server.store, server.applyLog); err != nil {
return nil, errors.Wrap(err, "NewServer")
}

// init requests merged
if cfg.Node.RequestsMerged {
Expand Down
12 changes: 8 additions & 4 deletions server_misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ func (w LockerBackendWrapper) Watch(key []byte) (store.WatcherNotify, error) {
}

func NewLockerBackend(
ns store.Namespace,
s store.Store,
raftApplyFunc func(context.Context, *serverpb.RaftLogPayload, time.Duration) error,
) locker.Backend {
) (locker.Backend, error) {
current, err := s.Namespace(locker.Namespace)
if err != nil {
return nil, err
}
return &LockerBackendWrapper{
store: ns,
store: current,
apply: raftApplyFunc,
}
}, nil
}

0 comments on commit c2aa915

Please sign in to comment.