From aebb741c08e1c0eb25805ba18b7dc2b119211ddf Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 11 Oct 2024 15:37:49 +0800 Subject: [PATCH] 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. --- routers/api/packages/container/blob.go | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/routers/api/packages/container/blob.go b/routers/api/packages/container/blob.go index 9e3a47076c2..4595b9a33de 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,