From 8634185ca92d7e21fad5e18520d5d667a4e8e5eb Mon Sep 17 00:00:00 2001 From: Sydonian <794346190@qq.com> Date: Wed, 14 Jun 2023 16:52:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8A=A0=E9=94=81=E7=9A=84?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/cmdline/commandline.go | 9 +++-- internal/cmdline/distlock.go | 68 +++++++++++++++++++++++++++++++++ main.go | 2 +- 3 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 internal/cmdline/distlock.go diff --git a/internal/cmdline/commandline.go b/internal/cmdline/commandline.go index cc6fce8..abe54b8 100644 --- a/internal/cmdline/commandline.go +++ b/internal/cmdline/commandline.go @@ -6,6 +6,7 @@ import ( "gitlink.org.cn/cloudream/client/internal/services" "gitlink.org.cn/cloudream/common/pkg/cmdtrie" + distlocksvc "gitlink.org.cn/cloudream/common/pkg/distlock/service" ) type CommandContext struct { @@ -15,12 +16,14 @@ type CommandContext struct { var commands cmdtrie.CommandTrie[CommandContext, error] = cmdtrie.NewCommandTrie[CommandContext, error]() type Commandline struct { - Svc *services.Service + Svc *services.Service + DistLock *distlocksvc.Service } -func NewCommandline(svc *services.Service) (*Commandline, error) { +func NewCommandline(svc *services.Service, distLock *distlocksvc.Service) (*Commandline, error) { return &Commandline{ - Svc: svc, + Svc: svc, + DistLock: distLock, }, nil } diff --git a/internal/cmdline/distlock.go b/internal/cmdline/distlock.go new file mode 100644 index 0000000..1fc4475 --- /dev/null +++ b/internal/cmdline/distlock.go @@ -0,0 +1,68 @@ +package cmdline + +import ( + "fmt" + "strings" + + "gitlink.org.cn/cloudream/common/pkg/distlock" + "gitlink.org.cn/cloudream/common/pkg/distlock/lockprovider" +) + +func DistLockLock(ctx CommandContext, lockData []string) error { + req := distlock.LockRequest{} + + for _, lock := range lockData { + l, err := parseOneLock(lock) + if err != nil { + return fmt.Errorf("parse lock data %s failed, err: %w", lock, err) + } + + req.Locks = append(req.Locks, l) + } + + reqID, err := ctx.Cmdline.DistLock.Acquire(req) + if err != nil { + return fmt.Errorf("acquire locks failed, err: %w", err) + } + + fmt.Printf("%s\n", reqID) + + return nil +} + +func parseOneLock(lockData string) (distlock.Lock, error) { + var lock distlock.Lock + + fullPathAndTarget := strings.Split(lockData, "@") + if len(fullPathAndTarget) != 2 { + return lock, fmt.Errorf("lock data must contains lock path, name and target") + } + + pathAndName := strings.Split(fullPathAndTarget[0], "/") + if len(pathAndName) < 2 { + return lock, fmt.Errorf("lock data must contains lock path, name and target") + } + + lock.Path = pathAndName[0 : len(pathAndName)-1] + lock.Name = pathAndName[len(pathAndName)-1] + + target := lockprovider.NewStringLockTarget() + comps := strings.Split(fullPathAndTarget[1], "/") + for _, comp := range comps { + target.Add(strings.Split(comp, ".")) + } + + lock.Target = *target + + return lock, nil +} + +func DistLockUnlock(ctx CommandContext, reqID string) error { + return ctx.Cmdline.DistLock.Release(reqID) +} + +func init() { + commands.MustAdd(DistLockLock, "distlock", "lock") + + commands.MustAdd(DistLockUnlock, "distlock", "unlock") +} diff --git a/main.go b/main.go index 5d9d3eb..73f2b94 100644 --- a/main.go +++ b/main.go @@ -64,7 +64,7 @@ func main() { os.Exit(1) } - cmds, err := cmdline.NewCommandline(svc) + cmds, err := cmdline.NewCommandline(svc, distlockSvc) if err != nil { log.Warnf("new command line failed, err: %s", err.Error()) os.Exit(1)