All caches is now protected with mutexes.

This commit is contained in:
Stanislav Nikitin 2016-12-13 04:58:31 +05:00
parent 384754276a
commit 2997abf1aa
3 changed files with 11 additions and 0 deletions

View File

@ -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() {

View File

@ -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"]]

View File

@ -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.")
} }