Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Svg Helper function with Read Write mutex w/signoff #581

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 12 additions & 24 deletions models/registration/svg_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,13 @@ import (
"os"
"path/filepath"
"strings"
"sync"

"github.com/layer5io/meshkit/utils/store"
)

var hashCheckSVG = make(map[string]string)
var mx sync.Mutex
var hashCheckSVG = store.NewGenericThreadSafeStore[string]() // Using the generic store
var UISVGPaths = make([]string, 1)

func writeHashCheckSVG(key string, val string) {
mx.Lock()
hashCheckSVG[key] = val
mx.Unlock()
}

func WriteAndReplaceSVGWithFileSystemPath(svgColor, svgWhite, svgComplete string, baseDir, dirname, filename string) (svgColorPath, svgWhitePath, svgCompletePath string) {
filename = strings.ToLower(filename)
successCreatingDirectory := false
Expand All @@ -39,8 +33,7 @@ func WriteAndReplaceSVGWithFileSystemPath(svgColor, svgWhite, svgComplete string

hash := md5.Sum([]byte(svgColor))
hashString := hex.EncodeToString(hash[:])
pathsvg := hashCheckSVG[hashString]
if pathsvg != "" { // the image has already been loaded, point the component to that path
if pathsvg, exists := hashCheckSVG.Get(hashString); exists { // the image has already been loaded, point the component to that path
svgColorPath = pathsvg
goto White
}
Expand All @@ -54,9 +47,8 @@ func WriteAndReplaceSVGWithFileSystemPath(svgColor, svgWhite, svgComplete string
fmt.Println(err)
return
}
svgColorPath = getRelativePathForAPI(baseDir, filepath.Join(dirname, "color", filename+"-color.svg")) //Replace the actual SVG with path to SVG
writeHashCheckSVG(hashString, svgColorPath)

svgColorPath = getRelativePathForAPI(baseDir, filepath.Join(dirname, "color", filename+"-color.svg"))
hashCheckSVG.Set(hashString, svgColorPath)
}
White:
if svgWhite != "" {
Expand All @@ -70,8 +62,7 @@ White:

hash := md5.Sum([]byte(svgWhite))
hashString := hex.EncodeToString(hash[:])
pathsvg := hashCheckSVG[hashString]
if pathsvg != "" { // the image has already been loaded, point the component to that path
if pathsvg, exists := hashCheckSVG.Get(hashString); exists { // the image has already been loaded, point the component to that path
svgWhitePath = pathsvg
goto Complete
}
Expand All @@ -85,9 +76,8 @@ White:
fmt.Println(err)
return
}
svgWhitePath = getRelativePathForAPI(baseDir, filepath.Join(dirname, "white", filename+"-white.svg")) //Replace the actual SVG with path to SVG
writeHashCheckSVG(hashString, svgWhitePath)

svgWhitePath = getRelativePathForAPI(baseDir, filepath.Join(dirname, "white", filename+"-white.svg"))
hashCheckSVG.Set(hashString, svgWhitePath)
}
Complete:
if svgComplete != "" {
Expand All @@ -101,8 +91,7 @@ Complete:

hash := md5.Sum([]byte(svgComplete))
hashString := hex.EncodeToString(hash[:])
pathsvg := hashCheckSVG[hashString]
if pathsvg != "" { // the image has already been loaded, point the component to that path
if pathsvg, exists := hashCheckSVG.Get(hashString); exists { // the image has already been loaded, point the component to that path
svgCompletePath = pathsvg
return
}
Expand All @@ -116,9 +105,8 @@ Complete:
fmt.Println(err)
return
}
svgCompletePath = getRelativePathForAPI(baseDir, filepath.Join(dirname, "complete", filename+"-complete.svg")) //Replace the actual SVG with path to SVG
writeHashCheckSVG(hashString, svgCompletePath)

svgCompletePath = getRelativePathForAPI(baseDir, filepath.Join(dirname, "complete", filename+"-complete.svg"))
hashCheckSVG.Set(hashString, svgCompletePath)
}
return
}
Expand Down
33 changes: 33 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"runtime"
"strconv"
"strings"
"sync"

"github.com/layer5io/meshkit/models/meshmodel/entity"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -495,3 +496,35 @@ func ConvertMapInterfaceMapString(v interface{}) interface{} {

return v
}

func WorkerPool[T any](tasks []T, workerFunc func(T), maxWorkers int) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will you revisit the workerFunc return type? How is the error handling done?

Copy link
Contributor Author

@Jougan-0 Jougan-0 Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'll see how to improve the error.

numWorkers := maxWorkers
if len(tasks) < numWorkers {
numWorkers = len(tasks)
}

if numWorkers == 0 {
return
}

taskChan := make(chan T)
var wg sync.WaitGroup

for i := 0; i < numWorkers; i++ {
wg.Add(1)
go func(workerID int) {
defer wg.Done()
for task := range taskChan {
workerFunc(task)
}
}(i)
}

for _, task := range tasks {
taskChan <- task
}

close(taskChan)

wg.Wait()
}
Loading