Use sync.Map in vips.SupportsLoad/SupportsSave

This commit is contained in:
DarthSim
2021-11-11 13:44:15 +06:00
parent 8f7404ec27
commit e9f1633021
2 changed files with 25 additions and 20 deletions

View File

@@ -1,19 +1,23 @@
package vips package vips
import "github.com/imgproxy/imgproxy/v3/imagetype" import (
"sync"
"github.com/imgproxy/imgproxy/v3/imagetype"
)
func DisableLoadSupport(it imagetype.Type) { func DisableLoadSupport(it imagetype.Type) {
typeSupportLoad[it] = false typeSupportLoad.Store(it, false)
} }
func ResetLoadSupport() { func ResetLoadSupport() {
typeSupportLoad = make(map[imagetype.Type]bool) typeSupportLoad = sync.Map{}
} }
func DisableSaveSupport(it imagetype.Type) { func DisableSaveSupport(it imagetype.Type) {
typeSupportSave[it] = false typeSupportSave.Store(it, false)
} }
func ResetSaveSupport() { func ResetSaveSupport() {
typeSupportSave = make(map[imagetype.Type]bool) typeSupportSave = sync.Map{}
} }

View File

@@ -13,6 +13,7 @@ import (
"math" "math"
"os" "os"
"runtime" "runtime"
"sync"
"unsafe" "unsafe"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@@ -29,8 +30,8 @@ type Image struct {
} }
var ( var (
typeSupportLoad = make(map[imagetype.Type]bool) typeSupportLoad sync.Map
typeSupportSave = make(map[imagetype.Type]bool) typeSupportSave sync.Map
) )
var vipsConf struct { var vipsConf struct {
@@ -123,8 +124,8 @@ func hasOperation(name string) bool {
} }
func SupportsLoad(it imagetype.Type) bool { func SupportsLoad(it imagetype.Type) bool {
if sup, ok := typeSupportLoad[it]; ok { if sup, ok := typeSupportLoad.Load(it); ok {
return sup return sup.(bool)
} }
sup := false sup := false
@@ -148,36 +149,36 @@ func SupportsLoad(it imagetype.Type) bool {
sup = hasOperation("tiffload_buffer") sup = hasOperation("tiffload_buffer")
} }
typeSupportLoad[it] = sup typeSupportLoad.Store(it, sup)
return sup return sup
} }
func SupportsSave(it imagetype.Type) bool { func SupportsSave(it imagetype.Type) bool {
if sup, ok := typeSupportSave[it]; ok { if sup, ok := typeSupportSave.Load(it); ok {
return sup return sup.(bool)
} }
sup := false sup := false
switch it { switch it {
case imagetype.JPEG: case imagetype.JPEG:
return hasOperation("jpegsave_buffer") sup = hasOperation("jpegsave_buffer")
case imagetype.PNG, imagetype.ICO: case imagetype.PNG, imagetype.ICO:
return hasOperation("pngsave_buffer") sup = hasOperation("pngsave_buffer")
case imagetype.WEBP: case imagetype.WEBP:
return hasOperation("webpsave_buffer") sup = hasOperation("webpsave_buffer")
case imagetype.GIF: case imagetype.GIF:
return hasOperation("magicksave_buffer") sup = hasOperation("magicksave_buffer")
case imagetype.AVIF: case imagetype.AVIF:
return hasOperation("heifsave_buffer") sup = hasOperation("heifsave_buffer")
case imagetype.BMP: case imagetype.BMP:
return true sup = true
case imagetype.TIFF: case imagetype.TIFF:
return hasOperation("tiffsave_buffer") sup = hasOperation("tiffsave_buffer")
} }
typeSupportSave[it] = sup typeSupportSave.Store(it, sup)
return sup return sup
} }