-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Create a lock abstract layer and remove old one #22176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 21 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
0b6c92c
create a lock abstract layer and remove old one
lunny c87bc64
add setting
lunny 845752d
Fix bug
lunny 30d2023
comment year
lunny afe380f
Rename interface method
lunny 11b7584
Fix bug
lunny 3af650e
fix lint
lunny eef2002
Merge branch 'main' into lunny/lock_interface
lunny a788b44
Remove lock when Unlock
lunny a2281dd
Use moby/locker
lunny 10a1fd8
Merge branch 'main' into lunny/lock_interface
lunny 8ea63e4
Fix lint
lunny 2618c52
Merge branch 'main' into lunny/lock_interface
lunny 71208d7
Merge branch 'main' into lunny/lock_interface
lunny 6ecafea
Merge branch 'main' into lunny/lock_interface
lunny 5cc2850
Fix check
lunny 8642873
lock key
lunny 3a9df2e
Simple lock usage
lunny d2c3980
Update documentations
lunny e4a7e3b
Merge branch 'main' into lunny/lock_interface
lunny 98dd8f4
Apply suggestions from code review
lunny 02ef039
add tests
lunny 980780f
Merge branch 'lunny/lock_interface' of github.com:lunny/gitea into lu…
lunny 179beb8
Update documentations
lunny f3fa627
Merge branch 'main' into lunny/lock_interface
lunny 985f433
merge main branch
lunny 45a019d
Merge branch 'main' into lunny/lock_interface
lunny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package setting | ||
|
||
import "code.gitea.io/gitea/modules/log" | ||
|
||
// Sync represents configuration of sync | ||
var Sync = struct { | ||
LockServiceType string | ||
LockServiceConnStr string | ||
}{ | ||
LockServiceType: "memory", | ||
} | ||
|
||
func parseSyncSetting() { | ||
sec := Cfg.Section("sync") | ||
Sync.LockServiceType = sec.Key("LOCK_SERVICE_TYPE").MustString("memory") | ||
if Sync.LockServiceType != "memory" && Sync.LockServiceType != "redis" { | ||
lunny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
log.Fatal("Unknown sync lock service type: %s", Sync.LockServiceType) | ||
} | ||
Sync.LockServiceConnStr = sec.Key("LOCK_SERVICE_CONN_STR").MustString("") | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package sync | ||
|
||
import ( | ||
"sync" | ||
|
||
"code.gitea.io/gitea/modules/nosql" | ||
"code.gitea.io/gitea/modules/setting" | ||
|
||
redsync "github.com/go-redsync/redsync/v4" | ||
goredis "github.com/go-redsync/redsync/v4/redis/goredis/v8" | ||
"github.com/moby/locker" | ||
) | ||
|
||
type Locker interface { | ||
Lock() error | ||
Unlock() (bool, error) | ||
} | ||
|
||
type LockService interface { | ||
GetLock(name string) Locker | ||
} | ||
|
||
type memoryLock struct { | ||
locker *locker.Locker | ||
name string | ||
} | ||
|
||
func (r *memoryLock) Lock() error { | ||
r.locker.Lock(r.name) | ||
return nil | ||
} | ||
|
||
func (r *memoryLock) Unlock() (bool, error) { | ||
return true, r.locker.Unlock(r.name) | ||
} | ||
|
||
var _ Locker = &memoryLock{} | ||
|
||
type memoryLockService struct { | ||
locker *locker.Locker | ||
} | ||
|
||
var _ LockService = &memoryLockService{} | ||
|
||
func newMemoryLockService() *memoryLockService { | ||
return &memoryLockService{ | ||
locker: locker.New(), | ||
} | ||
} | ||
|
||
func (l *memoryLockService) GetLock(name string) Locker { | ||
return &memoryLock{ | ||
locker: l.locker, | ||
name: name, | ||
} | ||
} | ||
|
||
type redisLockService struct { | ||
rs *redsync.Redsync | ||
} | ||
|
||
var _ LockService = &redisLockService{} | ||
|
||
func newRedisLockService(connection string) *redisLockService { | ||
client := nosql.GetManager().GetRedisClient(connection) | ||
|
||
pool := goredis.NewPool(client) // or, pool := redigo.NewPool(...) | ||
|
||
// Create an instance of redisync to be used to obtain a mutual exclusion | ||
// lock. | ||
rs := redsync.New(pool) | ||
|
||
return &redisLockService{ | ||
rs: rs, | ||
} | ||
} | ||
|
||
type redisLock struct { | ||
mutex *redsync.Mutex | ||
} | ||
|
||
func (r *redisLockService) GetLock(name string) Locker { | ||
return &redisLock{mutex: r.rs.NewMutex(name)} | ||
} | ||
|
||
func (r *redisLock) Lock() error { | ||
return r.mutex.Lock() | ||
} | ||
|
||
func (r *redisLock) Unlock() (bool, error) { | ||
return r.mutex.Unlock() | ||
} | ||
|
||
var ( | ||
syncOnce sync.Once | ||
lockService LockService | ||
) | ||
|
||
func getLockService() LockService { | ||
syncOnce.Do(func() { | ||
if setting.Sync.LockServiceType == "redis" { | ||
lockService = newRedisLockService(setting.Sync.LockServiceConnStr) | ||
} else { | ||
lockService = newMemoryLockService() | ||
} | ||
}) | ||
return lockService | ||
} | ||
|
||
func GetLock(name string) Locker { | ||
return getLockService().GetLock(name) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package sync | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func Test_Lock(t *testing.T) { | ||
locker := GetLock("test") | ||
locker.Lock() | ||
locker.Unlock() | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.