thumbs: allow configuring limit on simultanous thumbs (default 16)

This commit is contained in:
mappu 2018-09-09 18:41:37 +12:00
parent 524f37d9fe
commit b08f1c33d5
3 changed files with 20 additions and 5 deletions

View File

@ -8,7 +8,6 @@ import (
"os" "os"
"regexp" "regexp"
"strings" "strings"
"sync"
"time" "time"
"github.com/boltdb/bolt" "github.com/boltdb/bolt"
@ -17,6 +16,8 @@ import (
var SERVER_HEADER string = `contented/0.0.0-dev` var SERVER_HEADER string = `contented/0.0.0-dev`
const DEFAULT_MAX_CONCURRENT_THUMBS = 16
type ServerPublicProperties struct { type ServerPublicProperties struct {
AppTitle string AppTitle string
MaxUploadBytes int64 MaxUploadBytes int64
@ -30,6 +31,7 @@ type ServerOptions struct {
BandwidthLimit int64 BandwidthLimit int64
TrustXForwardedFor bool TrustXForwardedFor bool
EnableHomepage bool EnableHomepage bool
MaxConcurrentThumbs int
ServerPublicProperties ServerPublicProperties
} }
@ -45,7 +47,7 @@ type Server struct {
opts ServerOptions opts ServerOptions
db *bolt.DB db *bolt.DB
startTime time.Time startTime time.Time
thumbnailSem sync.Mutex thumbnailSem chan struct{}
metadataBucket []byte metadataBucket []byte
} }
@ -56,6 +58,17 @@ func NewServer(opts *ServerOptions) (*Server, error) {
startTime: time.Now(), startTime: time.Now(),
} }
if s.opts.MaxConcurrentThumbs <= 0 {
s.opts.MaxConcurrentThumbs = DEFAULT_MAX_CONCURRENT_THUMBS // default
log.Printf("Allowing %d concurrent thumbnails", s.opts.MaxConcurrentThumbs)
}
// "fill" the thumbnailer semaphore
s.thumbnailSem = make(chan struct{}, s.opts.MaxConcurrentThumbs)
for i := 0; i < s.opts.MaxConcurrentThumbs; i += 1 {
s.thumbnailSem <- struct{}{}
}
b, err := bolt.Open(opts.DBPath, 0644, bolt.DefaultOptions) b, err := bolt.Open(opts.DBPath, 0644, bolt.DefaultOptions)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -21,6 +21,7 @@ func main() {
trustXForwardedFor := flag.Bool("trustXForwardedFor", false, "Trust X-Forwarded-For reverse proxy headers") trustXForwardedFor := flag.Bool("trustXForwardedFor", false, "Trust X-Forwarded-For reverse proxy headers")
enableHomepage := flag.Bool("enableHomepage", true, "Enable homepage (disable for embedded use only)") enableHomepage := flag.Bool("enableHomepage", true, "Enable homepage (disable for embedded use only)")
diskFilesWorldReadable := flag.Bool("diskFilesWorldReadable", false, "Save files as 0644 instead of 0600") diskFilesWorldReadable := flag.Bool("diskFilesWorldReadable", false, "Save files as 0644 instead of 0600")
maxConcurrentThumbs := flag.Int("concurrentthumbs", contented.DEFAULT_MAX_CONCURRENT_THUMBS, "Simultaneous thumbnail generation")
flag.Parse() flag.Parse()
@ -31,6 +32,7 @@ func main() {
TrustXForwardedFor: *trustXForwardedFor, TrustXForwardedFor: *trustXForwardedFor,
EnableHomepage: *enableHomepage, EnableHomepage: *enableHomepage,
DiskFilesWorldReadable: *diskFilesWorldReadable, DiskFilesWorldReadable: *diskFilesWorldReadable,
MaxConcurrentThumbs: *maxConcurrentThumbs,
ServerPublicProperties: contented.ServerPublicProperties{ ServerPublicProperties: contented.ServerPublicProperties{
AppTitle: *appTitle, AppTitle: *appTitle,
MaxUploadBytes: int64(*maxUploadMb) * 1024 * 1024, MaxUploadBytes: int64(*maxUploadMb) * 1024 * 1024,

View File

@ -61,9 +61,9 @@ func (this *Server) handleThumb(w http.ResponseWriter, r *http.Request, thumbnai
return return
} }
// Only calculate one thumbnail at a time // Only a limited number of thumbnails can be generated concurrently
this.thumbnailSem.Lock() <-this.thumbnailSem
defer this.thumbnailSem.Unlock() defer func() { this.thumbnailSem <- struct{}{} }()
if ctx.Err() != nil { if ctx.Err() != nil {
// The request was already cancelled // The request was already cancelled