diff options
author | Přemysl Eric Janouch <p@janouch.name> | 2023-12-27 06:42:57 +0100 |
---|---|---|
committer | Přemysl Eric Janouch <p@janouch.name> | 2023-12-27 06:42:57 +0100 |
commit | 042fd36ada2cff0bd96e6235c92268119b2af261 (patch) | |
tree | a1d9b755999919bb3acde3f29dd37fcbc466e98a | |
parent | f1c3531b0523574368df3865cfb19adb7e322f6c (diff) | |
download | gallery-042fd36ada2cff0bd96e6235c92268119b2af261.tar.gz gallery-042fd36ada2cff0bd96e6235c92268119b2af261.tar.xz gallery-042fd36ada2cff0bd96e6235c92268119b2af261.zip |
Avoid database lock timed backoff
-rw-r--r-- | main.go | 79 |
1 files changed, 47 insertions, 32 deletions
@@ -2176,22 +2176,6 @@ func makeThumbnail(pathImage, pathThumb string) (int, int, error) { return w, h, err } -func makeThumbnailFor(sha1 string) (message string, err error) { - pathImage := imagePath(sha1) - pathThumb := thumbPath(sha1) - w, h, err := makeThumbnail(pathImage, pathThumb) - if err != nil { - if ee, ok := err.(*exec.ExitError); ok { - return string(ee.Stderr), nil - } - return "", err - } - - _, err = db.Exec(`UPDATE image SET thumbw = ?, thumbh = ? - WHERE sha1 = ?`, w, h, sha1) - return "", err -} - // cmdThumbnail generates missing thumbnails, in parallel. func cmdThumbnail(fs *flag.FlagSet, args []string) error { if err := fs.Parse(args); err != nil { @@ -2214,7 +2198,31 @@ func cmdThumbnail(fs *flag.FlagSet, args []string) error { return err } } - return parallelize(hexSHA1, makeThumbnailFor) + + stmt, err := db.Prepare( + `UPDATE image SET thumbw = ?, thumbh = ? WHERE sha1 = ?`) + if err != nil { + return err + } + defer stmt.Close() + + var mu sync.Mutex + return parallelize(hexSHA1, func(sha1 string) (message string, err error) { + pathImage := imagePath(sha1) + pathThumb := thumbPath(sha1) + w, h, err := makeThumbnail(pathImage, pathThumb) + if err != nil { + if ee, ok := err.(*exec.ExitError); ok { + return string(ee.Stderr), nil + } + return "", err + } + + mu.Lock() + defer mu.Unlock() + _, err = stmt.Exec(w, h, sha1) + return "", err + }) } // --- Perceptual hash --------------------------------------------------------- @@ -2336,20 +2344,6 @@ func makeDhash(sha1 string) (uint64, error) { return dhashWebP(f) } -func makeDhashFor(sha1 string) (message string, err error) { - hash, err := makeDhash(sha1) - if errors.Is(err, errIsAnimation) { - // Ignoring this common condition. - return "", nil - } else if err != nil { - return err.Error(), nil - } - - _, err = db.Exec( - `UPDATE image SET dhash = ? WHERE sha1 = ?`, int64(hash), sha1) - return "", err -} - // cmdDhash computes perceptual hashes from thumbnails. func cmdDhash(fs *flag.FlagSet, args []string) error { if err := fs.Parse(args); err != nil { @@ -2371,7 +2365,28 @@ func cmdDhash(fs *flag.FlagSet, args []string) error { return err } } - return parallelize(hexSHA1, makeDhashFor) + + stmt, err := db.Prepare(`UPDATE image SET dhash = ? WHERE sha1 = ?`) + if err != nil { + return err + } + defer stmt.Close() + + var mu sync.Mutex + return parallelize(hexSHA1, func(sha1 string) (message string, err error) { + hash, err := makeDhash(sha1) + if errors.Is(err, errIsAnimation) { + // Ignoring this common condition. + return "", nil + } else if err != nil { + return err.Error(), nil + } + + mu.Lock() + defer mu.Unlock() + _, err = stmt.Exec(int64(hash), sha1) + return "", err + }) } // --- Main -------------------------------------------------------------------- |