Skip to content

Commit

Permalink
replace sync.map in engine factory
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Mar 8, 2022
1 parent 5a3dfcc commit 709c19c
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions engine/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"context"
"fmt"
"strings"
"sync"
"time"
"unsafe"

"github.com/cornelk/hashmap"
"github.com/projecteru2/core/engine"
"github.com/projecteru2/core/engine/docker"
"github.com/projecteru2/core/engine/fake"
Expand Down Expand Up @@ -50,15 +51,15 @@ func (ep engineParams) getCacheKey() string {
// EngineCache .
type EngineCache struct {
cache *utils.EngineCache
keysToCheck sync.Map
keysToCheck hashmap.HashMap
config types.Config
}

// NewEngineCache .
func NewEngineCache(config types.Config) *EngineCache {
return &EngineCache{
cache: utils.NewEngineCache(12*time.Hour, 10*time.Minute),
keysToCheck: sync.Map{},
keysToCheck: hashmap.HashMap{},
config: config,
}
}
Expand All @@ -77,7 +78,7 @@ func (e *EngineCache) Get(key string) engine.API {
// Set .
func (e *EngineCache) Set(params engineParams, client engine.API) {
e.cache.Set(params.getCacheKey(), client)
e.keysToCheck.Store(params, struct{}{})
e.keysToCheck.Set(uintptr(unsafe.Pointer(&params)), params)
}

// Delete .
Expand All @@ -98,10 +99,9 @@ func (e *EngineCache) CheckAlive(ctx context.Context) {

paramsChan := make(chan engineParams)
go func() {
e.keysToCheck.Range(func(key, _ interface{}) bool {
paramsChan <- key.(engineParams)
return true
})
for kv := range e.keysToCheck.Iter() {
paramsChan <- kv.Value.(engineParams)
}
close(paramsChan)
}()

Expand All @@ -113,7 +113,7 @@ func (e *EngineCache) CheckAlive(ctx context.Context) {
client := e.cache.Get(cacheKey)
if client == nil {
e.cache.Delete(params.getCacheKey())
e.keysToCheck.Delete(params)
e.keysToCheck.Del(uintptr(unsafe.Pointer(&params)))
return
}
if _, ok := client.(*fake.Engine); ok {
Expand Down

0 comments on commit 709c19c

Please sign in to comment.