正在显示
2 个修改的文件
包含
36 行增加
和
0 行删除
@@ -2,6 +2,7 @@ package service | @@ -2,6 +2,7 @@ package service | ||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "fmt" | 4 | "fmt" |
5 | + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/xredis" | ||
5 | "strconv" | 6 | "strconv" |
6 | "strings" | 7 | "strings" |
7 | "time" | 8 | "time" |
@@ -501,6 +502,15 @@ func (rs *EvaluationProjectService) List(in *command.QueryProjectCommand) (inter | @@ -501,6 +502,15 @@ func (rs *EvaluationProjectService) List(in *command.QueryProjectCommand) (inter | ||
501 | } | 502 | } |
502 | 503 | ||
503 | func (rs *EvaluationProjectService) Activate(in *command.ActivateProjectCommand) (interface{}, error) { | 504 | func (rs *EvaluationProjectService) Activate(in *command.ActivateProjectCommand) (interface{}, error) { |
505 | + lock := xredis.NewLockProjectId(int(in.Id)) | ||
506 | + err := lock.Lock() | ||
507 | + if err != nil { | ||
508 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
509 | + } | ||
510 | + defer func() { | ||
511 | + lock.UnLock() | ||
512 | + }() | ||
513 | + | ||
504 | transactionContext, err := factory.ValidateStartTransaction(in) | 514 | transactionContext, err := factory.ValidateStartTransaction(in) |
505 | if err != nil { | 515 | if err != nil { |
506 | return nil, err | 516 | return nil, err |
1 | +package xredis | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + | ||
6 | + "github.com/go-redsync/redsync/v4" | ||
7 | +) | ||
8 | + | ||
9 | +type LockProjectId struct { | ||
10 | + m *redsync.Mutex | ||
11 | +} | ||
12 | + | ||
13 | +func NewLockProjectId(id int) *LockProjectId { | ||
14 | + key := fmt.Sprintf("performance:project_id:%d", id) | ||
15 | + return &LockProjectId{ | ||
16 | + m: rsync.NewMutex(key), | ||
17 | + } | ||
18 | +} | ||
19 | + | ||
20 | +func (lk *LockProjectId) Lock() error { | ||
21 | + return lk.m.Lock() | ||
22 | +} | ||
23 | + | ||
24 | +func (lk *LockProjectId) UnLock() (bool, error) { | ||
25 | + return lk.m.Unlock() | ||
26 | +} |
-
请 注册 或 登录 后发表评论