Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
333aec5
Passes PSQL, SQLite and MSSQL
guillep2k Jan 20, 2020
70a2a8d
Move to upsert strategy; all tests work
guillep2k Jan 21, 2020
677e49f
Merge branch 'master' into bylock-indexes
guillep2k Jan 21, 2020
d834fb1
Use a LockedResource to numerate issues and prs
guillep2k Jan 21, 2020
58ac901
Fix tests and reserved keyword
guillep2k Jan 21, 2020
aa3797c
Fix unit tests
guillep2k Jan 21, 2020
95db48a
Fix export comments
guillep2k Jan 22, 2020
d8ad174
A little refactoring and better function naming
guillep2k Jan 22, 2020
6bec3d5
Merge branch 'master' into bylock-indexes
guillep2k Jan 22, 2020
e747a2c
Support LockType == "" and LockKey == 0
guillep2k Jan 22, 2020
afeb6f0
Merge branch 'master' into bylock-indexes
guillep2k Jan 22, 2020
c503f0d
Prepare for merge
guillep2k Jan 28, 2020
9b7ec1d
Merge branch 'master' into bylock-indexes
guillep2k Jan 28, 2020
1792664
Go simple
guillep2k Jan 28, 2020
ce6c24f
Improve test legibility
guillep2k Jan 30, 2020
15ffbb4
Fix typo
guillep2k Jan 30, 2020
ea9c875
Remove dead code
guillep2k Jan 30, 2020
9cb79c9
Merge branch 'master' into bylock-indexes
guillep2k Jan 30, 2020
d185a4f
Prepare for merge
guillep2k Feb 1, 2020
f46eaf5
Merge branch 'master' into bylock-indexes
guillep2k Feb 1, 2020
621c9d6
Prepare to merge
guillep2k Feb 12, 2020
17fa5e1
Merge branch 'master' into bylock-indexes
guillep2k Feb 12, 2020
b30094b
Merge branch 'master' into bylock-indexes
guillep2k Feb 15, 2020
299d313
Merge branch 'master' into bylock-indexes
guillep2k Feb 16, 2020
cea7c4f
Merge branch 'master' into bylock-indexes
guillep2k Feb 20, 2020
2311de3
Merge branch 'master' into bylock-indexes
guillep2k Feb 29, 2020
7e280a4
Merge branch 'master' into bylock-indexes
guillep2k May 2, 2020
15e407b
Code review suggestions by @lunny
guillep2k May 2, 2020
dd85873
Ignore SQLite3 integration when _txlock=immediate
guillep2k May 2, 2020
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
Prev Previous commit
Next Next commit
Fix export comments
  • Loading branch information
guillep2k committed Jan 22, 2020
commit 95db48a8c2b6babb4e29c991726cee5ad818444f
12 changes: 12 additions & 0 deletions models/locked_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ import (
"code.gitea.io/gitea/modules/setting"
)

// LockedResource represents the locking key for a pessimistic
// lock that can hold a counter
type LockedResource struct {
LockType string `xorm:"pk VARCHAR(30)"`
LockKey int64 `xorm:"pk"`
Counter int64 `xorm:"NOT NULL DEFAULT 0"`
}

// GetLockedResource gets or creates a pessimistic lock on the given type and key
func GetLockedResource(e Engine, lockType string, lockKey int64) (*LockedResource, error) {
locked := &LockedResource{LockType: lockType, LockKey: lockKey}

Expand All @@ -33,16 +36,19 @@ func GetLockedResource(e Engine, lockType string, lockKey int64) (*LockedResourc
return locked, nil
}

// UpdateLockedResource updates the value of the counter of a locked resource
func UpdateLockedResource(e Engine, resource *LockedResource) error {
_, err := e.Table(resource).Cols("counter").Update(resource)
return err
}

// DeleteLockedResource deletes a locked resource
func DeleteLockedResource(e Engine, resource *LockedResource) error {
_, err := e.Delete(resource)
return err
}

// TempLockResource locks the given key but does not leave a permanent record
func TempLockResource(e Engine, lockType string, lockKey int64) error {
locked := &LockedResource{LockType: lockType, LockKey: lockKey}
// Temporary locked resources must not exist in the table.
Expand All @@ -54,22 +60,28 @@ func TempLockResource(e Engine, lockType string, lockKey int64) error {
return err
}

// GetLockedResourceCtx gets or creates a pessimistic lock on the given type and key
func GetLockedResourceCtx(ctx DBContext, lockType string, lockKey int64) (*LockedResource, error) {
return GetLockedResource(ctx.e, lockType, lockKey)
}

// UpdateLockedResourceCtx updates the value of the counter of a locked resource
func UpdateLockedResourceCtx(ctx DBContext, resource *LockedResource) error {
return UpdateLockedResource(ctx.e, resource)
}

// DeleteLockedResourceCtx deletes a locked resource
func DeleteLockedResourceCtx(ctx DBContext, resource *LockedResource) error {
return DeleteLockedResource(ctx.e, resource)
}

// TempLockResourceCtx locks the given key but does not leave a permanent record
func TempLockResourceCtx(ctx DBContext, lockType string, lockKey int64) error {
return TempLockResource(ctx.e, lockType, lockKey)
}

// upsertLockedResource will create or lock the given key in the database.
// the function will not return until it acquires the lock or receives an error.
func upsertLockedResource(e Engine, resource *LockedResource) (err error) {
// An atomic UPSERT operation (INSERT/UPDATE) is the only operation
// that ensures that the key is actually locked.
Expand Down