From 2997abf1aa9f7644450b42259ce6c92b232a4707 Mon Sep 17 00:00:00 2001 From: "Stanislav N. aka pztrn" Date: Tue, 13 Dec 2016 04:58:31 +0500 Subject: [PATCH] All caches is now protected with mutexes. --- cache/cache_object.go | 5 +++++ cache/cache_profiles.go | 4 ++++ cache/cache_servers.go | 2 ++ 3 files changed, 11 insertions(+) diff --git a/cache/cache_object.go b/cache/cache_object.go index 451851d..e27c5ac 100644 --- a/cache/cache_object.go +++ b/cache/cache_object.go @@ -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() { diff --git a/cache/cache_profiles.go b/cache/cache_profiles.go index e227779..a66370b 100644 --- a/cache/cache_profiles.go +++ b/cache/cache_profiles.go @@ -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"]] diff --git a/cache/cache_servers.go b/cache/cache_servers.go index 32b6dff..f85b20f 100644 --- a/cache/cache_servers.go +++ b/cache/cache_servers.go @@ -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.") }