-  
 -   Notifications  
You must be signed in to change notification settings  - Fork 6.2k
 
Use global lock instead of NewExclusivePool to allow distributed lock between multiple Gitea instances #31813
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
   Merged  
     Merged  
 Changes from 5 commits
 Commits 
  Show all changes 
  18 commits   Select commit Hold shift + click to select a range 
 1c483f8  Use an abstract lock layer to allow distributed lock between multiple… 
  lunny b98fb46  Merge branch 'main' into lunny/lock_abstract2 
  lunny 5141395  Fix go tidy 
  lunny b0f6710  Revert changes about status table 
  lunny 1748ba8  Revert unnecessary change 
  lunny 0c3c1a3  refactor the names and add remove locker 
  lunny 3d3bfaa  Merge branch 'main' into lunny/lock_abstract2 
  lunny 1276449  Merge branch 'main' into lunny/lock_abstract2 
  lunny 35daece  use globallock lock 
  lunny fe9efa9  finish the global lock init 
  lunny 4bf86e9  Follow the old logic 
  lunny 1e1d6e9  Use lockCtx when necessary 
  lunny 3e268b9  Merge branch 'main' into lunny/lock_abstract2 
  lunny 8e09e01  Use correct ctx usage 
  lunny 30dc681  Merge branch 'main' into lunny/lock_abstract2 
  lunny 860f3ec  Follow global lock refactor 
  lunny aa51a8c  fix: always defer release 
  wolfogre 5df67a8  Merge branch 'main' into lunny/lock_abstract2 
  lafriks 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   
     | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| // Copyright 2023 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|   |  ||
| package globallock | ||
|   |  ||
| import ( | ||
| "context" | ||
| "sync" | ||
| "time" | ||
|   |  ||
| "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/v9" | ||
| ) | ||
|   |  ||
| type Locker interface { | ||
| Lock() error | ||
| TryLock() (bool, error) | ||
| Unlock() (bool, error) | ||
| } | ||
|   |  ||
| type LockService interface { | ||
| GetLock(name string) Locker | ||
    lunny marked this conversation as resolved.    Outdated   Show resolved Hide resolved  |  ||
| } | ||
|   |  ||
| type memoryLock struct { | ||
| mutex sync.Mutex | ||
| } | ||
|   |  ||
| func (r *memoryLock) Lock() error { | ||
| r.mutex.Lock() | ||
| return nil | ||
| } | ||
|   |  ||
| func (r *memoryLock) TryLock() (bool, error) { | ||
| return r.mutex.TryLock(), nil | ||
| } | ||
|   |  ||
| func (r *memoryLock) Unlock() (bool, error) { | ||
| r.mutex.Unlock() | ||
| return true, nil | ||
| } | ||
|   |  ||
| var _ Locker = &memoryLock{} | ||
|   |  ||
| type memoryLockService struct { | ||
| syncMap sync.Map | ||
| } | ||
|   |  ||
| var _ LockService = &memoryLockService{} | ||
|   |  ||
| func newMemoryLockService() *memoryLockService { | ||
| return &memoryLockService{ | ||
| syncMap: sync.Map{}, | ||
| } | ||
| } | ||
|   |  ||
| func (l *memoryLockService) GetLock(name string) Locker { | ||
| v, _ := l.syncMap.LoadOrStore(name, &memoryLock{}) | ||
| return v.(*memoryLock) | ||
| } | ||
|   |  ||
| type redisLockService struct { | ||
| rs *redsync.Redsync | ||
| } | ||
|   |  ||
| var _ LockService = &redisLockService{} | ||
|   |  ||
| func newRedisLockService(connection string) *redisLockService { | ||
| client := nosql.GetManager().GetRedisClient(connection) | ||
|   |  ||
| pool := goredis.NewPool(client) | ||
|   |  ||
| // 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() | ||
    lunny marked this conversation as resolved.    Outdated   Show resolved Hide resolved  |  ||
| } | ||
|   |  ||
| func (r *redisLock) TryLock() (bool, error) { | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) | ||
| defer cancel() | ||
| if err := r.mutex.LockContext(ctx); err != nil { | ||
| return false, err | ||
| } | ||
| return true, nil | ||
| } | ||
|   |  ||
| func (r *redisLock) Unlock() (bool, error) { | ||
| return r.mutex.Unlock() | ||
| } | ||
|   |  ||
| var ( | ||
| syncOnce sync.Once | ||
| lockService LockService | ||
| ) | ||
|   |  ||
| func getLockService() LockService { | ||
| syncOnce.Do(func() { | ||
| if setting.GlobalLock.ServiceType == "redis" { | ||
| lockService = newRedisLockService(setting.GlobalLock.ServiceConnStr) | ||
| } 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,26 @@ | ||
| // Copyright 2024 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|   |  ||
| package globallock | ||
|   |  ||
| import ( | ||
| "testing" | ||
|   |  ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|   |  ||
| func Test_Lock(t *testing.T) { | ||
| locker := GetLock("test") | ||
| assert.NoError(t, locker.Lock()) | ||
| locker.Unlock() | ||
|   |  ||
| locked1, err1 := locker.TryLock() | ||
| assert.NoError(t, err1) | ||
| assert.True(t, locked1) | ||
|   |  ||
| locked2, err2 := locker.TryLock() | ||
| assert.NoError(t, err2) | ||
| assert.False(t, locked2) | ||
|   |  ||
| locker.Unlock() | ||
| } | 
   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,37 @@ | ||
| // Copyright 2024 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|   |  ||
| package setting | ||
|   |  ||
| import ( | ||
| "code.gitea.io/gitea/modules/log" | ||
| "code.gitea.io/gitea/modules/nosql" | ||
| ) | ||
|   |  ||
| // GlobalLock represents configuration of global lock | ||
| var GlobalLock = struct { | ||
| ServiceType string | ||
| ServiceConnStr string | ||
| }{ | ||
| ServiceType: "memory", | ||
| } | ||
|   |  ||
| func loadGlobalLockFrom(rootCfg ConfigProvider) { | ||
| sec := rootCfg.Section("global_lock") | ||
| GlobalLock.ServiceType = sec.Key("SERVICE_TYPE").MustString("memory") | ||
| switch GlobalLock.ServiceType { | ||
| case "memory": | ||
| case "redis": | ||
| connStr := sec.Key("SERVICE_CONN_STR").String() | ||
| if connStr == "" { | ||
| log.Fatal("SERVICE_CONN_STR is empty for redis") | ||
| } | ||
| u := nosql.ToRedisURI(connStr) | ||
| if u == nil { | ||
| log.Fatal("SERVICE_CONN_STR %s is not a valid redis connection string", connStr) | ||
| } | ||
| GlobalLock.ServiceConnStr = connStr | ||
| default: | ||
| log.Fatal("Unknown sync lock service type: %s", GlobalLock.ServiceType) | ||
| } | ||
| } | 
   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,35 @@ | ||
| // Copyright 2024 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|   |  ||
| package setting | ||
|   |  ||
| import ( | ||
| "testing" | ||
|   |  ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|   |  ||
| func TestLoadGlobalLockConfig(t *testing.T) { | ||
| t.Run("DefaultGlobalLockConfig", func(t *testing.T) { | ||
| iniStr := `` | ||
| cfg, err := NewConfigProviderFromData(iniStr) | ||
| assert.NoError(t, err) | ||
|   |  ||
| loadGlobalLockFrom(cfg) | ||
| assert.EqualValues(t, "memory", GlobalLock.ServiceType) | ||
| }) | ||
|   |  ||
| t.Run("RedisGlobalLockConfig", func(t *testing.T) { | ||
| iniStr := ` | ||
| [global_lock] | ||
| SERVICE_TYPE = redis | ||
| SERVICE_CONN_STR = addrs=127.0.0.1:6379 db=0 | ||
| ` | ||
| cfg, err := NewConfigProviderFromData(iniStr) | ||
| assert.NoError(t, err) | ||
|   |  ||
| loadGlobalLockFrom(cfg) | ||
| assert.EqualValues(t, "redis", GlobalLock.ServiceType) | ||
| assert.EqualValues(t, "addrs=127.0.0.1:6379 db=0", GlobalLock.ServiceConnStr) | ||
| }) | ||
| } | 
   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 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.