aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Eric Janouch <p@janouch.name>2023-12-27 06:42:57 +0100
committerPřemysl Eric Janouch <p@janouch.name>2023-12-27 06:42:57 +0100
commit042fd36ada2cff0bd96e6235c92268119b2af261 (patch)
treea1d9b755999919bb3acde3f29dd37fcbc466e98a
parentf1c3531b0523574368df3865cfb19adb7e322f6c (diff)
downloadgallery-042fd36ada2cff0bd96e6235c92268119b2af261.tar.gz
gallery-042fd36ada2cff0bd96e6235c92268119b2af261.tar.xz
gallery-042fd36ada2cff0bd96e6235c92268119b2af261.zip
Avoid database lock timed backoff
-rw-r--r--main.go79
1 files changed, 47 insertions, 32 deletions
diff --git a/main.go b/main.go
index 9601164..abb5994 100644
--- a/main.go
+++ b/main.go
@@ -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 --------------------------------------------------------------------