From 94e906ff62b327c1c17e577d387eff68697da538 Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Mon, 3 Mar 2025 20:25:37 +0800 Subject: [PATCH] refactor: replace with builtin locker --- go.mod | 3 +-- go.sum | 6 ++---- internal/version/VERSION | 2 +- pkg/uixt/driver_utils.go | 24 +++++++++++++++++------- server/app.go | 3 ++- server/ext/app.go | 2 +- 6 files changed, 24 insertions(+), 16 deletions(-) diff --git a/go.mod b/go.mod index 9661ac61..0ba4a624 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.23.0 require ( code.byted.org/iesqa/ghdc v0.0.0-20241009025217-ecb76cf5bd27 - github.com/BurntSushi/locker v0.0.0-20171006230638-a6e239ea1c69 github.com/Masterminds/semver v1.5.0 github.com/andybalholm/brotli v1.0.4 github.com/danielpaulus/go-ios v1.0.161 @@ -54,7 +53,7 @@ require ( github.com/golang/protobuf v1.5.3 // indirect github.com/google/btree v1.1.2 // indirect github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect - github.com/google/uuid v1.3.0 // indirect + github.com/google/uuid v1.6.0 // indirect github.com/grandcat/zeroconf v1.0.0 // indirect github.com/hashicorp/go-hclog v1.5.0 // indirect github.com/hashicorp/go-plugin v1.4.10 // indirect diff --git a/go.sum b/go.sum index 692c28a6..9c7d42f3 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,5 @@ code.byted.org/iesqa/ghdc v0.0.0-20241009025217-ecb76cf5bd27 h1:+wNJiEXXIUP6luKJRA4tfwDqfnWUON6LIopKD9tvUns= code.byted.org/iesqa/ghdc v0.0.0-20241009025217-ecb76cf5bd27/go.mod h1:C2kq6TTE+JAOnqDorSwae1MQzRuex03RshuSUC2U/FY= -github.com/BurntSushi/locker v0.0.0-20171006230638-a6e239ea1c69 h1:+tu3HOoMXB7RXEINRVIpxJCT+KdYiI7LAEAUrOw3dIU= -github.com/BurntSushi/locker v0.0.0-20171006230638-a6e239ea1c69/go.mod h1:L1AbZdiDllfyYH5l5OkAaZtk7VkWe89bPJFmnDBNHxg= github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww= github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y= github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY= @@ -86,8 +84,8 @@ github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= -github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= -github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/grandcat/zeroconf v1.0.0 h1:uHhahLBKqwWBV6WZUDAT71044vwOTL+McW0mBJvo6kE= diff --git a/internal/version/VERSION b/internal/version/VERSION index f4a635b3..fca308a8 100644 --- a/internal/version/VERSION +++ b/internal/version/VERSION @@ -1 +1 @@ -v5.0.0+2503032009 +v5.0.0+2503032025 diff --git a/pkg/uixt/driver_utils.go b/pkg/uixt/driver_utils.go index 9cbd0498..c2c8ea33 100644 --- a/pkg/uixt/driver_utils.go +++ b/pkg/uixt/driver_utils.go @@ -3,21 +3,21 @@ package uixt import ( "crypto/md5" "fmt" - "github.com/BurntSushi/locker" - "github.com/httprunner/httprunner/v5/internal/builtin" - "github.com/httprunner/httprunner/v5/internal/config" "io" "math" "math/rand/v2" "net/http" "os" "path/filepath" + "sync" "time" "github.com/pkg/errors" "github.com/rs/zerolog/log" "github.com/httprunner/httprunner/v5/code" + "github.com/httprunner/httprunner/v5/internal/builtin" + "github.com/httprunner/httprunner/v5/internal/config" "github.com/httprunner/httprunner/v5/pkg/uixt/option" ) @@ -268,17 +268,27 @@ func sleepStrict(startTime time.Time, strictMilliseconds int64) { time.Sleep(time.Duration(dur) * time.Millisecond) } +// global file lock +var ( + fileLocks sync.Map +) + func DownloadFileByUrl(fileUrl string) (filePath string, err error) { hash := md5.Sum([]byte(fileUrl)) fileName := fmt.Sprintf("%x", hash) filePath = filepath.Join(config.DownloadsPath, fileName) - locker.Lock(filePath) - defer locker.Unlock(filePath) + + // get or create file lock + lockI, _ := fileLocks.LoadOrStore(filePath, &sync.Mutex{}) + lock := lockI.(*sync.Mutex) + lock.Lock() + defer lock.Unlock() + if builtin.FileExists(filePath) { return filePath, nil } - fmt.Printf("Downloading file to %s from URL %s\n", filePath, fileUrl) + log.Info().Str("fileUrl", fileUrl).Str("filePath", filePath).Msg("downloading file") // Create an HTTP client with default settings. client := &http.Client{} @@ -314,6 +324,6 @@ func DownloadFileByUrl(fileUrl string) (filePath string, err error) { return "", err } - fmt.Printf("File downloaded successfully: %s\n", filePath) + log.Info().Str("filePath", filePath).Msg("download file success") return filePath, nil } diff --git a/server/app.go b/server/app.go index a5bd45bf..889eada9 100644 --- a/server/app.go +++ b/server/app.go @@ -2,8 +2,9 @@ package server import ( "github.com/gin-gonic/gin" - "github.com/httprunner/httprunner/v5/pkg/uixt" "github.com/rs/zerolog/log" + + "github.com/httprunner/httprunner/v5/pkg/uixt" ) func (r *Router) foregroundAppHandler(c *gin.Context) { diff --git a/server/ext/app.go b/server/ext/app.go index 7ddc2728..9b6f9a87 100644 --- a/server/ext/app.go +++ b/server/ext/app.go @@ -2,12 +2,12 @@ package server_ext import ( "fmt" - "github.com/httprunner/httprunner/v5/pkg/uixt/driver_ext" "os" "github.com/gin-gonic/gin" "github.com/httprunner/httprunner/v5/pkg/uixt" + "github.com/httprunner/httprunner/v5/pkg/uixt/driver_ext" "github.com/httprunner/httprunner/v5/server" )