aboutsummaryrefslogtreecommitdiffstats
path: root/routers/api
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2024-10-11 15:37:49 +0800
committerGitHub <noreply@github.com>2024-10-11 07:37:49 +0000
commitaebb741c08e1c0eb25805ba18b7dc2b119211ddf (patch)
tree27b2ffbba54480dadd93261af67b57769f027138 /routers/api
parentcb739f533358a8cf6e1b6875b3d4f0da3bfa7c95 (diff)
downloadgitea-aebb741c08e1c0eb25805ba18b7dc2b119211ddf.tar.gz
gitea-aebb741c08e1c0eb25805ba18b7dc2b119211ddf.zip
Use per package global lock for container uploads instead of memory lock (#31860)
This PR uses a per-package lock instead of a global lock for container uploads. So that different packages can be uploaded simultaneously.
Diffstat (limited to 'routers/api')
-rw-r--r--routers/api/packages/container/blob.go26
1 files changed, 18 insertions, 8 deletions
diff --git a/routers/api/packages/container/blob.go b/routers/api/packages/container/blob.go
index 9e3a47076c..4595b9a33d 100644
--- a/routers/api/packages/container/blob.go
+++ b/routers/api/packages/container/blob.go
@@ -10,11 +10,11 @@ import (
"fmt"
"os"
"strings"
- "sync"
"code.gitea.io/gitea/models/db"
packages_model "code.gitea.io/gitea/models/packages"
container_model "code.gitea.io/gitea/models/packages/container"
+ "code.gitea.io/gitea/modules/globallock"
"code.gitea.io/gitea/modules/log"
packages_module "code.gitea.io/gitea/modules/packages"
container_module "code.gitea.io/gitea/modules/packages/container"
@@ -22,8 +22,6 @@ import (
packages_service "code.gitea.io/gitea/services/packages"
)
-var uploadVersionMutex sync.Mutex
-
// saveAsPackageBlob creates a package blob from an upload
// The uploaded blob gets stored in a special upload version to link them to the package/image
func saveAsPackageBlob(ctx context.Context, hsr packages_module.HashedSizeReader, pci *packages_service.PackageCreationInfo) (*packages_model.PackageBlob, error) { //nolint:unparam
@@ -90,13 +88,20 @@ func mountBlob(ctx context.Context, pi *packages_service.PackageInfo, pb *packag
})
}
+func containerPkgName(piOwnerID int64, piName string) string {
+ return fmt.Sprintf("pkg_%d_container_%s", piOwnerID, strings.ToLower(piName))
+}
+
func getOrCreateUploadVersion(ctx context.Context, pi *packages_service.PackageInfo) (*packages_model.PackageVersion, error) {
var uploadVersion *packages_model.PackageVersion
- // FIXME: Replace usage of mutex with database transaction
- // https://github.com/go-gitea/gitea/pull/21862
- uploadVersionMutex.Lock()
- err := db.WithTx(ctx, func(ctx context.Context) error {
+ releaser, err := globallock.Lock(ctx, containerPkgName(pi.Owner.ID, pi.Name))
+ if err != nil {
+ return nil, err
+ }
+ defer releaser()
+
+ err = db.WithTx(ctx, func(ctx context.Context) error {
created := true
p := &packages_model.Package{
OwnerID: pi.Owner.ID,
@@ -140,7 +145,6 @@ func getOrCreateUploadVersion(ctx context.Context, pi *packages_service.PackageI
return nil
})
- uploadVersionMutex.Unlock()
return uploadVersion, err
}
@@ -173,6 +177,12 @@ func createFileForBlob(ctx context.Context, pv *packages_model.PackageVersion, p
}
func deleteBlob(ctx context.Context, ownerID int64, image, digest string) error {
+ releaser, err := globallock.Lock(ctx, containerPkgName(ownerID, image))
+ if err != nil {
+ return err
+ }
+ defer releaser()
+
return db.WithTx(ctx, func(ctx context.Context) error {
pfds, err := container_model.GetContainerBlobs(ctx, &container_model.BlobSearchOptions{
OwnerID: ownerID,