All caches is now protected with mutexes.
This commit is contained in:
parent
384754276a
commit
2997abf1aa
5
cache/cache_object.go
vendored
5
cache/cache_object.go
vendored
@ -12,6 +12,7 @@ package cache
|
||||
import (
|
||||
// stdlib
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
// local
|
||||
"github.com/pztrn/urtrator/cachemodels"
|
||||
@ -20,8 +21,12 @@ import (
|
||||
type Cache struct {
|
||||
// Profiles cache.
|
||||
Profiles map[string]*cachemodels.Profile
|
||||
// Profiles cache mutex.
|
||||
ProfilesMutex sync.Mutex
|
||||
// Servers cache.
|
||||
Servers map[string]*cachemodels.Server
|
||||
// Servers cache mutex.
|
||||
ServersMutex sync.Mutex
|
||||
}
|
||||
|
||||
func (c *Cache) Initialize() {
|
||||
|
4
cache/cache_profiles.go
vendored
4
cache/cache_profiles.go
vendored
@ -24,8 +24,10 @@ func (c *Cache) CreateProfile(name string) {
|
||||
_, ok := c.Profiles[name]
|
||||
|
||||
if !ok {
|
||||
c.ProfilesMutex.Lock()
|
||||
c.Profiles[name] = &cachemodels.Profile{}
|
||||
c.Profiles[name].Profile = &datamodels.Profile{}
|
||||
c.ProfilesMutex.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
@ -34,7 +36,9 @@ func (c *Cache) deleteProfile(data map[string]string) {
|
||||
|
||||
_, ok := c.Profiles[data["profile_name"]]
|
||||
if ok {
|
||||
c.ProfilesMutex.Lock()
|
||||
delete(c.Profiles, data["profile_name"])
|
||||
c.ProfilesMutex.Unlock()
|
||||
}
|
||||
|
||||
_, ok1 := c.Profiles[data["profile_name"]]
|
||||
|
2
cache/cache_servers.go
vendored
2
cache/cache_servers.go
vendored
@ -21,8 +21,10 @@ import (
|
||||
func (c *Cache) CreateServer(addr string) {
|
||||
_, ok := c.Servers[addr]
|
||||
if !ok {
|
||||
c.ServersMutex.Lock()
|
||||
c.Servers[addr] = &cachemodels.Server{}
|
||||
c.Servers[addr].Server = &datamodels.Server{}
|
||||
c.ServersMutex.Unlock()
|
||||
} else {
|
||||
fmt.Println("Server " + addr + " already exist.")
|
||||
}
|
||||
|
Reference in New Issue
Block a user