thumb: only generate one thumbnail concurrently

This commit is contained in:
mappu 2018-09-09 18:31:25 +12:00
parent 8fbad2a1e0
commit c958c57794
2 changed files with 16 additions and 2 deletions

View File

@ -8,6 +8,7 @@ import (
"os" "os"
"regexp" "regexp"
"strings" "strings"
"sync"
"time" "time"
"github.com/boltdb/bolt" "github.com/boltdb/bolt"
@ -44,6 +45,7 @@ type Server struct {
opts ServerOptions opts ServerOptions
db *bolt.DB db *bolt.DB
startTime time.Time startTime time.Time
thumbnailSem sync.Mutex
metadataBucket []byte metadataBucket []byte
} }

View File

@ -1,6 +1,7 @@
package contented package contented
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"log" "log"
@ -51,6 +52,8 @@ func getThumbnailerConfig(t byte) (*thumbnail.Config, error) {
} }
func (this *Server) handleThumb(w http.ResponseWriter, r *http.Request, thumbnailType byte, fileId string) { func (this *Server) handleThumb(w http.ResponseWriter, r *http.Request, thumbnailType byte, fileId string) {
ctx := r.Context()
opts, err := getThumbnailerConfig(thumbnailType) opts, err := getThumbnailerConfig(thumbnailType)
if err != nil { if err != nil {
log.Printf("%s Thumbnail failed: %s\n", this.remoteIP(r), err.Error()) log.Printf("%s Thumbnail failed: %s\n", this.remoteIP(r), err.Error())
@ -58,9 +61,18 @@ func (this *Server) handleThumb(w http.ResponseWriter, r *http.Request, thumbnai
return return
} }
// Only calculate one thumbnail at a time
this.thumbnailSem.Lock()
defer this.thumbnailSem.Unlock()
if ctx.Err() != nil {
// The request was already cancelled
return
}
t := thumbnail.NewThumbnailerEx(opts) t := thumbnail.NewThumbnailerEx(opts)
err = this.handleThumbInternal(w, t, fileId) err = this.handleThumbInternal(ctx, w, t, fileId)
if err != nil { if err != nil {
log.Printf("%s Thumbnail failed: %s\n", this.remoteIP(r), err.Error()) log.Printf("%s Thumbnail failed: %s\n", this.remoteIP(r), err.Error())
@ -69,7 +81,7 @@ func (this *Server) handleThumb(w http.ResponseWriter, r *http.Request, thumbnai
} }
} }
func (this *Server) handleThumbInternal(w http.ResponseWriter, t thumbnail.Thumbnailer, fileId string) error { func (this *Server) handleThumbInternal(ctx context.Context, w http.ResponseWriter, t thumbnail.Thumbnailer, fileId string) error {
// Load metadata // Load metadata
m, err := this.Metadata(fileId) m, err := this.Metadata(fileId)