1
1
mirror of https://github.com/go-gitea/gitea synced 2025-08-15 05:58:27 +00:00

Fix parallel creating commit status bug with tests (#21911) (#21989)

backport #21911 
backport #21998

Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
Lunny Xiao
2022-12-13 18:59:18 +08:00
committed by GitHub
parent 079ef56824
commit c36a1bc766
3 changed files with 106 additions and 70 deletions

View File

@@ -8,6 +8,9 @@ import (
"context"
"errors"
"fmt"
"strconv"
"code.gitea.io/gitea/modules/setting"
)
// ResourceIndex represents a resource index which could be used as issue/release and others
@@ -24,11 +27,6 @@ var (
ErrGetResourceIndexFailed = errors.New("get resource index failed")
)
const (
// MaxDupIndexAttempts max retry times to create index
MaxDupIndexAttempts = 3
)
// SyncMaxResourceIndex sync the max index with the resource
func SyncMaxResourceIndex(ctx context.Context, tableName string, groupID, maxIndex int64) (err error) {
e := GetEngine(ctx)
@@ -61,8 +59,25 @@ func SyncMaxResourceIndex(ctx context.Context, tableName string, groupID, maxInd
return nil
}
func postgresGetNextResourceIndex(ctx context.Context, tableName string, groupID int64) (int64, error) {
res, err := GetEngine(ctx).Query(fmt.Sprintf("INSERT INTO %s (group_id, max_index) "+
"VALUES (?,1) ON CONFLICT (group_id) DO UPDATE SET max_index = %s.max_index+1 RETURNING max_index",
tableName, tableName), groupID)
if err != nil {
return 0, err
}
if len(res) == 0 {
return 0, ErrGetResourceIndexFailed
}
return strconv.ParseInt(string(res[0]["max_index"]), 10, 64)
}
// GetNextResourceIndex generates a resource index, it must run in the same transaction where the resource is created
func GetNextResourceIndex(ctx context.Context, tableName string, groupID int64) (int64, error) {
if setting.Database.UsePostgreSQL {
return postgresGetNextResourceIndex(ctx, tableName, groupID)
}
e := GetEngine(ctx)
// try to update the max_index to next value, and acquire the write-lock for the record