Skip to content

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
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
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 Dec 20, 2022
c87bc64
add setting
lunny Dec 20, 2022
845752d
Fix bug
lunny Dec 20, 2022
30d2023
comment year
lunny Dec 20, 2022
afe380f
Rename interface method
lunny Dec 20, 2022
11b7584
Fix bug
lunny Dec 20, 2022
3af650e
fix lint
lunny Dec 21, 2022
eef2002
Merge branch 'main' into lunny/lock_interface
lunny Dec 21, 2022
a788b44
Remove lock when Unlock
lunny Dec 21, 2022
a2281dd
Use moby/locker
lunny Dec 26, 2022
10a1fd8
Merge branch 'main' into lunny/lock_interface
lunny Dec 26, 2022
8ea63e4
Fix lint
lunny Dec 27, 2022
2618c52
Merge branch 'main' into lunny/lock_interface
lunny Dec 27, 2022
71208d7
Merge branch 'main' into lunny/lock_interface
lunny Dec 31, 2022
6ecafea
Merge branch 'main' into lunny/lock_interface
lunny Jan 1, 2023
5cc2850
Fix check
lunny Jan 1, 2023
8642873
lock key
lunny Jan 1, 2023
3a9df2e
Simple lock usage
lunny Jan 1, 2023
d2c3980
Update documentations
lunny Jan 10, 2023
e4a7e3b
Merge branch 'main' into lunny/lock_interface
lunny Jan 10, 2023
98dd8f4
Apply suggestions from code review
lunny Jan 10, 2023
02ef039
add tests
lunny Jan 11, 2023
980780f
Merge branch 'lunny/lock_interface' of github.com:lunny/gitea into lu…
lunny Jan 11, 2023
179beb8
Update documentations
lunny Jan 11, 2023
f3fa627
Merge branch 'main' into lunny/lock_interface
lunny Jan 11, 2023
985f433
merge main branch
lunny Jan 18, 2023
45a019d
Merge branch 'main' into lunny/lock_interface
lunny Jan 21, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions assets/go-licenses.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions custom/conf/app.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2487,3 +2487,9 @@ ROUTER = console
;PROXY_URL =
;; Comma separated list of host names requiring proxy. Glob patterns (*) are accepted; use ** to match all hosts.
;PROXY_HOSTS =

;[sync]
;; Lock service type, could be memory or redis
;LOCK_SERVICE_TYPE = memory
;; Ignored by memory type, for redis, it likes "addrs=127.0.0.1:6379 db=0"
;LOCK_SERVICE_CONN_STR =
5 changes: 5 additions & 0 deletions docs/content/doc/advanced/config-cheat-sheet.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -1290,6 +1290,11 @@ PROXY_URL = socks://127.0.0.1:1080
PROXY_HOSTS = *.github.com
```

## Sync (`sync`)

- `LOCK_SERVICE_TYPE`: **memory**: Lock service type, could be `memory` or `redis`
- `LOCK_SERVICE_CONN_STR`: ****: Ignored when `LOCK_SERVICE_TYPE` is `memory` type, for `redis`, it likes `addrs=127.0.0.1:6379 db=0`

## Other (`other`)

- `SHOW_FOOTER_BRANDING`: **false**: Show Gitea branding in the footer.
Expand Down
5 changes: 5 additions & 0 deletions docs/content/doc/advanced/config-cheat-sheet.zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,11 @@ PROXY_URL = socks://127.0.0.1:1080
PROXY_HOSTS = *.github.com
```

## Sync (`sync`)

- `LOCK_SERVICE_TYPE`: **memory**: 锁服务类型,可以是 `memory` 或 `redis`
- `LOCK_SERVICE_CONN_STR`: ****: 如果 `LOCK_SERVICE_TYPE` 是 `memory`,则忽略,如果是 `redis`,则支持形式如 `addrs=127.0.0.1:6379 db=0`

## Other (`other`)

- `SHOW_FOOTER_BRANDING`: 为真则在页面底部显示Gitea的字样。
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ require (
github.com/go-git/go-git/v5 v5.4.3-0.20220529141257-bc1f419cebcf
github.com/go-ldap/ldap/v3 v3.4.4
github.com/go-redis/redis/v8 v8.11.5
github.com/go-redsync/redsync/v4 v4.7.1
github.com/go-sql-driver/mysql v1.6.0
github.com/go-swagger/go-swagger v0.30.3
github.com/go-testfixtures/testfixtures/v3 v3.8.1
Expand Down Expand Up @@ -70,6 +71,7 @@ require (
github.com/mholt/archiver/v3 v3.5.1
github.com/microcosm-cc/bluemonday v1.0.20
github.com/minio/minio-go/v7 v7.0.39
github.com/moby/locker v1.0.1
github.com/msteinert/pam v1.1.0
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/niklasfasching/go-org v1.6.5
Expand Down Expand Up @@ -203,7 +205,9 @@ require (
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-multierror v1.1.0 // indirect
github.com/hashicorp/go-retryablehttp v0.7.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
Expand Down
42 changes: 41 additions & 1 deletion go.sum

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions modules/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,8 @@ func loadFromConf(allowEmpty bool, extraConfig string) {
for _, emoji := range UI.CustomEmojis {
UI.CustomEmojisMap[emoji] = ":" + emoji + ":"
}

parseSyncSetting()
}

func parseAuthorizedPrincipalsAllow(values []string) ([]string, bool) {
Expand Down
23 changes: 23 additions & 0 deletions modules/setting/sync.go
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" {
log.Fatal("Unknown sync lock service type: %s", Sync.LockServiceType)
}
Sync.LockServiceConnStr = sec.Key("LOCK_SERVICE_CONN_STR").MustString("")
}
69 changes: 0 additions & 69 deletions modules/sync/exclusive_pool.go

This file was deleted.

115 changes: 115 additions & 0 deletions modules/sync/lock.go
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)
}
14 changes: 14 additions & 0 deletions modules/sync/lock_test.go
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()
}
57 changes: 0 additions & 57 deletions modules/sync/status_pool.go

This file was deleted.

31 changes: 0 additions & 31 deletions modules/sync/status_pool_test.go

This file was deleted.

Loading