mirror of
https://github.com/woodchen-ink/webp_server_go.git
synced 2025-07-18 13:42:02 +08:00
Lock on convert (#307)
* Lock on convert * Fix typo * Should use debug for convert lock * Bump to 0.10.5
This commit is contained in:
parent
484c876e86
commit
d35e0e0d0b
@ -46,8 +46,9 @@ var (
|
|||||||
ProxyMode bool
|
ProxyMode bool
|
||||||
Prefetch bool
|
Prefetch bool
|
||||||
Config = NewWebPConfig()
|
Config = NewWebPConfig()
|
||||||
Version = "0.10.4"
|
Version = "0.10.5"
|
||||||
WriteLock = cache.New(5*time.Minute, 10*time.Minute)
|
WriteLock = cache.New(5*time.Minute, 10*time.Minute)
|
||||||
|
ConvertLock = cache.New(5*time.Minute, 10*time.Minute)
|
||||||
RemoteRaw = "./remote-raw"
|
RemoteRaw = "./remote-raw"
|
||||||
Metadata = "./metadata"
|
Metadata = "./metadata"
|
||||||
LocalHostAlias = "local"
|
LocalHostAlias = "local"
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
"webp_server_go/config"
|
"webp_server_go/config"
|
||||||
"webp_server_go/helper"
|
"webp_server_go/helper"
|
||||||
|
|
||||||
@ -33,7 +34,24 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ConvertFilter(rawPath, avifPath, webpPath string, extraParams config.ExtraParams, c chan int) {
|
func ConvertFilter(rawPath, avifPath, webpPath string, extraParams config.ExtraParams, c chan int) {
|
||||||
// all absolute paths
|
// Wait for the conversion to complete and return the converted image
|
||||||
|
retryDelay := 100 * time.Millisecond // Initial retry delay
|
||||||
|
|
||||||
|
for {
|
||||||
|
if _, found := config.ConvertLock.Get(rawPath); found {
|
||||||
|
log.Debugf("file %s is locked under conversion, retrying in %s", rawPath, retryDelay)
|
||||||
|
time.Sleep(retryDelay)
|
||||||
|
} else {
|
||||||
|
// The lock is released, indicating that the conversion is complete
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there is a lock here, it means that another thread is converting the same image
|
||||||
|
// Lock rawPath to prevent concurrent conversion
|
||||||
|
config.ConvertLock.Set(rawPath, true, -1)
|
||||||
|
defer config.ConvertLock.Delete(rawPath)
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
wg.Add(2)
|
wg.Add(2)
|
||||||
if !helper.ImageExists(avifPath) && config.Config.EnableAVIF {
|
if !helper.ImageExists(avifPath) && config.Config.EnableAVIF {
|
||||||
@ -77,9 +95,9 @@ func convertImage(rawPath, optimizedPath, imageType string, extraParams config.E
|
|||||||
var convertedRaw, converted = ConvertRawToJPG(rawPath, optimizedPath)
|
var convertedRaw, converted = ConvertRawToJPG(rawPath, optimizedPath)
|
||||||
// If converted, use converted file as raw
|
// If converted, use converted file as raw
|
||||||
if converted {
|
if converted {
|
||||||
// Use converted file(JPG) as raw input for further convertion
|
// Use converted file(JPG) as raw input for further conversion
|
||||||
rawPath = convertedRaw
|
rawPath = convertedRaw
|
||||||
// Remove converted file after convertion
|
// Remove converted file after conversion
|
||||||
defer func() {
|
defer func() {
|
||||||
log.Infoln("Removing intermediate conversion file:", convertedRaw)
|
log.Infoln("Removing intermediate conversion file:", convertedRaw)
|
||||||
err := os.Remove(convertedRaw)
|
err := os.Remove(convertedRaw)
|
||||||
@ -105,6 +123,7 @@ func convertImage(rawPath, optimizedPath, imageType string, extraParams config.E
|
|||||||
case "avif":
|
case "avif":
|
||||||
err = avifEncoder(img, rawPath, optimizedPath, extraParams)
|
err = avifEncoder(img, rawPath, optimizedPath, extraParams)
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,7 +198,6 @@ func webpEncoder(img *vips.ImageRef, rawPath string, optimizedPath string, extra
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
buf, _, err = img.ExportWebp(&ep)
|
buf, _, err = img.ExportWebp(&ep)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user