Skip to content

Commit

Permalink
Lock on convert
Browse files Browse the repository at this point in the history
  • Loading branch information
n0vad3v committed Dec 29, 2023
1 parent 484c876 commit e5cfe55
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var (
Config = NewWebPConfig()
Version = "0.10.4"
WriteLock = cache.New(5*time.Minute, 10*time.Minute)
ConvertLock = cache.New(5*time.Minute, 10*time.Minute)
RemoteRaw = "./remote-raw"
Metadata = "./metadata"
LocalHostAlias = "local"
Expand Down
22 changes: 20 additions & 2 deletions encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"runtime"
"strings"
"sync"
"time"
"webp_server_go/config"
"webp_server_go/helper"

Expand Down Expand Up @@ -33,7 +34,24 @@ func init() {
}

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.Infof("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 convertion
config.ConvertLock.Set(rawPath, true, -1)
defer config.ConvertLock.Delete(rawPath)

var wg sync.WaitGroup
wg.Add(2)
if !helper.ImageExists(avifPath) && config.Config.EnableAVIF {
Expand Down Expand Up @@ -105,6 +123,7 @@ func convertImage(rawPath, optimizedPath, imageType string, extraParams config.E
case "avif":
err = avifEncoder(img, rawPath, optimizedPath, extraParams)
}

return err
}

Expand Down Expand Up @@ -179,7 +198,6 @@ func webpEncoder(img *vips.ImageRef, rawPath string, optimizedPath string, extra
}
}
buf, _, err = img.ExportWebp(&ep)

}

if err != nil {
Expand Down

0 comments on commit e5cfe55

Please sign in to comment.