refactor: replace with builtin locker

This commit is contained in:
lilong.129 2025-03-03 20:25:37 +08:00
parent 359199a485
commit 94e906ff62
6 changed files with 24 additions and 16 deletions

3
go.mod
View File

@ -4,7 +4,6 @@ go 1.23.0
require ( require (
code.byted.org/iesqa/ghdc v0.0.0-20241009025217-ecb76cf5bd27 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/Masterminds/semver v1.5.0
github.com/andybalholm/brotli v1.0.4 github.com/andybalholm/brotli v1.0.4
github.com/danielpaulus/go-ios v1.0.161 github.com/danielpaulus/go-ios v1.0.161
@ -54,7 +53,7 @@ require (
github.com/golang/protobuf v1.5.3 // indirect github.com/golang/protobuf v1.5.3 // indirect
github.com/google/btree v1.1.2 // indirect github.com/google/btree v1.1.2 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // 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/grandcat/zeroconf v1.0.0 // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect github.com/hashicorp/go-hclog v1.5.0 // indirect
github.com/hashicorp/go-plugin v1.4.10 // indirect github.com/hashicorp/go-plugin v1.4.10 // indirect

6
go.sum
View File

@ -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 h1:+wNJiEXXIUP6luKJRA4tfwDqfnWUON6LIopKD9tvUns=
code.byted.org/iesqa/ghdc v0.0.0-20241009025217-ecb76cf5bd27/go.mod h1:C2kq6TTE+JAOnqDorSwae1MQzRuex03RshuSUC2U/FY= 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 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y= github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY= 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/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 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= 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.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= 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 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grandcat/zeroconf v1.0.0 h1:uHhahLBKqwWBV6WZUDAT71044vwOTL+McW0mBJvo6kE= github.com/grandcat/zeroconf v1.0.0 h1:uHhahLBKqwWBV6WZUDAT71044vwOTL+McW0mBJvo6kE=

View File

@ -1 +1 @@
v5.0.0+2503032009 v5.0.0+2503032025

View File

@ -3,21 +3,21 @@ package uixt
import ( import (
"crypto/md5" "crypto/md5"
"fmt" "fmt"
"github.com/BurntSushi/locker"
"github.com/httprunner/httprunner/v5/internal/builtin"
"github.com/httprunner/httprunner/v5/internal/config"
"io" "io"
"math" "math"
"math/rand/v2" "math/rand/v2"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"sync"
"time" "time"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v5/code" "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" "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) time.Sleep(time.Duration(dur) * time.Millisecond)
} }
// global file lock
var (
fileLocks sync.Map
)
func DownloadFileByUrl(fileUrl string) (filePath string, err error) { func DownloadFileByUrl(fileUrl string) (filePath string, err error) {
hash := md5.Sum([]byte(fileUrl)) hash := md5.Sum([]byte(fileUrl))
fileName := fmt.Sprintf("%x", hash) fileName := fmt.Sprintf("%x", hash)
filePath = filepath.Join(config.DownloadsPath, fileName) 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) { if builtin.FileExists(filePath) {
return filePath, nil 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. // Create an HTTP client with default settings.
client := &http.Client{} client := &http.Client{}
@ -314,6 +324,6 @@ func DownloadFileByUrl(fileUrl string) (filePath string, err error) {
return "", err return "", err
} }
fmt.Printf("File downloaded successfully: %s\n", filePath) log.Info().Str("filePath", filePath).Msg("download file success")
return filePath, nil return filePath, nil
} }

View File

@ -2,8 +2,9 @@ package server
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/httprunner/httprunner/v5/pkg/uixt"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v5/pkg/uixt"
) )
func (r *Router) foregroundAppHandler(c *gin.Context) { func (r *Router) foregroundAppHandler(c *gin.Context) {

View File

@ -2,12 +2,12 @@ package server_ext
import ( import (
"fmt" "fmt"
"github.com/httprunner/httprunner/v5/pkg/uixt/driver_ext"
"os" "os"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/httprunner/httprunner/v5/pkg/uixt" "github.com/httprunner/httprunner/v5/pkg/uixt"
"github.com/httprunner/httprunner/v5/pkg/uixt/driver_ext"
"github.com/httprunner/httprunner/v5/server" "github.com/httprunner/httprunner/v5/server"
) )