Unverified Commit 0cea5eba authored by Athurg Gooth's avatar Athurg Gooth Committed by GitHub

fix: concurrent counter operates (#1706)

Co-authored-by: 's avatarAthurg Feng <athurg@gooth.org>
parent 1e4a867a
...@@ -13,6 +13,7 @@ import ( ...@@ -13,6 +13,7 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"sync/atomic"
"time" "time"
"github.com/disintegration/imaging" "github.com/disintegration/imaging"
...@@ -501,7 +502,7 @@ func replacePathTemplate(path string, filename string) string { ...@@ -501,7 +502,7 @@ func replacePathTemplate(path string, filename string) string {
return path return path
} }
var availableGeneratorAmount = 32 var availableGeneratorAmount int32 = 32
func getOrGenerateThumbnailImage(srcBlob []byte, dstPath string) ([]byte, error) { func getOrGenerateThumbnailImage(srcBlob []byte, dstPath string) ([]byte, error) {
if _, err := os.Stat(dstPath); err != nil { if _, err := os.Stat(dstPath); err != nil {
...@@ -509,12 +510,12 @@ func getOrGenerateThumbnailImage(srcBlob []byte, dstPath string) ([]byte, error) ...@@ -509,12 +510,12 @@ func getOrGenerateThumbnailImage(srcBlob []byte, dstPath string) ([]byte, error)
return nil, errors.Wrap(err, "failed to check thumbnail image stat") return nil, errors.Wrap(err, "failed to check thumbnail image stat")
} }
if availableGeneratorAmount <= 0 { if atomic.LoadInt32(&availableGeneratorAmount) <= 0 {
return nil, errors.New("not enough available generator amount") return nil, errors.New("not enough available generator amount")
} }
availableGeneratorAmount-- atomic.AddInt32(&availableGeneratorAmount, -1)
defer func() { defer func() {
availableGeneratorAmount++ atomic.AddInt32(&availableGeneratorAmount, 1)
}() }()
reader := bytes.NewReader(srcBlob) reader := bytes.NewReader(srcBlob)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment