@@ -32,7 +32,10 @@ type Mutex struct {
3232ML * common.MapLocker
3333}
3434
35- // Lock
35+ // Lock will create/update lock in postgres.
36+ // If no lock exists for an allocation then new lock is created.
37+ // If lock exists and is of same connection ID then lock's createdAt is updated
38+ // If lock exists and is of other connection ID then `pending` response is sent.
3639func (m * Mutex ) Lock (ctx context.Context , allocationID , connectionID string , requestTime * time.Time ) (* LockResult , error ) {
3740if allocationID == "" {
3841return nil , errors .Throw (constants .ErrInvalidParameter , "allocationID" )
@@ -77,48 +80,30 @@ func (m *Mutex) Lock(ctx context.Context, allocationID, connectionID string, req
7780Status : LockStatusOK ,
7881CreatedAt : lock .CreatedAt .Unix (),
7982}, nil
80-
8183}
8284
8385//native postgres error
8486return nil , errors .ThrowLog (err .Error (), common .ErrBadDataStore )
85-
8687}
8788
88- timeout := lock .CreatedAt .Add (config .Configuration .WriteMarkerLockTimeout )
89-
90- // locked, but it is timeout
91- if now .After (timeout ) {
92-
93- lock .ConnectionID = connectionID
94- lock .CreatedAt = * requestTime
95-
96- err = db .Table (TableNameWriteLock ).Where ("allocation_id=?" , allocationID ).Save (& lock ).Error
97- if err != nil {
98- return nil , errors .ThrowLog (err .Error (), common .ErrBadDataStore )
99- }
100-
89+ if lock .ConnectionID != connectionID {
90+ // pending
10191return & LockResult {
102- Status : LockStatusOK ,
92+ Status : LockStatusPending ,
10393CreatedAt : lock .CreatedAt .Unix (),
10494}, nil
105-
10695}
10796
108- //try lock by same session, return old lock directly
109- if lock .ConnectionID == connectionID && lock .CreatedAt .Equal (* requestTime ) {
110- return & LockResult {
111- Status : LockStatusOK ,
112- CreatedAt : lock .CreatedAt .Unix (),
113- }, nil
97+ lock .CreatedAt = * requestTime
98+ err = db .Table (TableNameWriteLock ).Where ("allocation_id=?" , allocationID ).Save (& lock ).Error
99+ if err != nil {
100+ return nil , errors .ThrowLog (err .Error (), common .ErrBadDataStore )
114101}
115102
116- // pending
117103return & LockResult {
118- Status : LockStatusPending ,
104+ Status : LockStatusOK ,
119105CreatedAt : lock .CreatedAt .Unix (),
120106}, nil
121-
122107}
123108
124109func (* Mutex ) Unlock (ctx context.Context , allocationID string , connectionID string ) error {
0 commit comments