improve (efficiency): improve memory efficiency for resource intensive tasks

This commit is contained in:
Mickael Kerjean
2019-05-06 15:16:57 +10:00
parent 284d554a67
commit 2feed9127e
4 changed files with 67 additions and 18 deletions

View File

@ -8,10 +8,16 @@ import (
"context"
"golang.org/x/sync/semaphore"
. "github.com/mickael-kerjean/filestash/server/common"
"time"
"unsafe"
)
var LIBRAW_LOCK = semaphore.NewWeighted(int64(5))
const (
TRANSCODE_TIMEOUT = 10 * time.Second
TRANSCODE_MAX_CONCURRENT = 5
)
var LIBRAW_LOCK = semaphore.NewWeighted(int64(TRANSCODE_MAX_CONCURRENT))
func IsRaw(mType string) bool {
switch mType {
@ -45,14 +51,28 @@ func IsRaw(mType string) bool {
}
func ExtractPreview(t *Transform) error {
LIBRAW_LOCK.Acquire(context.Background(), 1)
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(TRANSCODE_TIMEOUT))
defer cancel()
if err := LIBRAW_LOCK.Acquire(ctx, 1); err != nil {
return ErrCongestion
}
defer LIBRAW_LOCK.Release(1)
filename := C.CString(t.Input)
defer C.free(unsafe.Pointer(filename))
transcodeChannel := make(chan error, 1)
go func() {
filename := C.CString(t.Input)
defer C.free(unsafe.Pointer(filename))
if err := C.image_transcode_compute(filename, C.int(t.Size)); err != 0 {
transcodeChannel <- ErrNotValid
}
transcodeChannel <- nil
}()
if err := C.image_transcode_compute(filename, C.int(t.Size)); err != 0 {
return ErrNotValid
select {
case err := <- transcodeChannel:
return err
case <- ctx.Done():
return ErrTimeout
}
return nil
}