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 (
|
import (
|
||||||
// stdlib
|
// stdlib
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
|
||||||
// local
|
// local
|
||||||
"github.com/pztrn/urtrator/cachemodels"
|
"github.com/pztrn/urtrator/cachemodels"
|
||||||
@ -20,8 +21,12 @@ import (
|
|||||||
type Cache struct {
|
type Cache struct {
|
||||||
// Profiles cache.
|
// Profiles cache.
|
||||||
Profiles map[string]*cachemodels.Profile
|
Profiles map[string]*cachemodels.Profile
|
||||||
|
// Profiles cache mutex.
|
||||||
|
ProfilesMutex sync.Mutex
|
||||||
// Servers cache.
|
// Servers cache.
|
||||||
Servers map[string]*cachemodels.Server
|
Servers map[string]*cachemodels.Server
|
||||||
|
// Servers cache mutex.
|
||||||
|
ServersMutex sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cache) Initialize() {
|
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]
|
_, ok := c.Profiles[name]
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
|
c.ProfilesMutex.Lock()
|
||||||
c.Profiles[name] = &cachemodels.Profile{}
|
c.Profiles[name] = &cachemodels.Profile{}
|
||||||
c.Profiles[name].Profile = &datamodels.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"]]
|
_, ok := c.Profiles[data["profile_name"]]
|
||||||
if ok {
|
if ok {
|
||||||
|
c.ProfilesMutex.Lock()
|
||||||
delete(c.Profiles, data["profile_name"])
|
delete(c.Profiles, data["profile_name"])
|
||||||
|
c.ProfilesMutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
_, ok1 := c.Profiles[data["profile_name"]]
|
_, 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) {
|
func (c *Cache) CreateServer(addr string) {
|
||||||
_, ok := c.Servers[addr]
|
_, ok := c.Servers[addr]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
c.ServersMutex.Lock()
|
||||||
c.Servers[addr] = &cachemodels.Server{}
|
c.Servers[addr] = &cachemodels.Server{}
|
||||||
c.Servers[addr].Server = &datamodels.Server{}
|
c.Servers[addr].Server = &datamodels.Server{}
|
||||||
|
c.ServersMutex.Unlock()
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("Server " + addr + " already exist.")
|
fmt.Println("Server " + addr + " already exist.")
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user