From 87c7405bc607a40805e389982952ab733d6278ce Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 26 Aug 2024 11:10:29 -0700 Subject: [PATCH] use globallock lock --- routers/api/packages/container/blob.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/routers/api/packages/container/blob.go b/routers/api/packages/container/blob.go index 49828d6612..e178322d62 100644 --- a/routers/api/packages/container/blob.go +++ b/routers/api/packages/container/blob.go @@ -14,16 +14,14 @@ import ( "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" - "code.gitea.io/gitea/modules/sync" "code.gitea.io/gitea/modules/util" packages_service "code.gitea.io/gitea/services/packages" ) -var uploadVersionMutex = sync.NewExclusivePool() - // 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,14 +88,20 @@ func mountBlob(ctx context.Context, pi *packages_service.PackageInfo, pb *packag }) } +func containerPkgName(piName string) string { + return "pkg_container_" + 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.CheckIn(strings.ToLower(pi.Name)) - defer uploadVersionMutex.CheckOut(strings.ToLower(pi.Name)) - err := db.WithTx(ctx, func(ctx context.Context) error { + ctx, releaser, err := globallock.Lock(ctx, containerPkgName(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,