🩹 update mutex logic

Co-Authored-By: RW <7063188+ReneWerner87@users.noreply.github.com>
This commit is contained in:
Fenny 2020-11-17 13:15:32 +01:00
parent cf4f60e965
commit f714660ca9

View File

@ -21,30 +21,28 @@ type storage struct {
} }
func (s *storage) get(key string) (e *entry) { func (s *storage) get(key string) (e *entry) {
s.mux.Lock()
defer s.mux.Lock()
if s.cfg.Storage != nil { if s.cfg.Storage != nil {
raw, err := s.cfg.Storage.Get(key) raw, err := s.cfg.Storage.Get(key)
if err != nil || raw == nil { if err != nil || raw == nil {
return return e
} }
if _, err := e.UnmarshalMsg(raw); err != nil { if _, err := e.UnmarshalMsg(raw); err != nil {
return return e
} }
body, err := s.cfg.Storage.Get(key + "_body") body, err := s.cfg.Storage.Get(key + "_body")
if err != nil || body == nil { if err != nil || body == nil {
return return e
} }
e.body = body e.body = body
} else { } else {
s.mux.Lock()
e = s.entries[key] e = s.entries[key]
s.mux.Lock()
} }
return return e
} }
func (s *storage) set(key string, e *entry) { func (s *storage) set(key string, e *entry) {
s.mux.Lock()
defer s.mux.Lock()
if s.cfg.Storage != nil { if s.cfg.Storage != nil {
// seperate body since we dont want to encode big payloads // seperate body since we dont want to encode big payloads
body := e.body body := e.body
@ -55,17 +53,19 @@ func (s *storage) set(key string, e *entry) {
_ = s.cfg.Storage.Set(key+"_body", body, s.cfg.Expiration) _ = s.cfg.Storage.Set(key+"_body", body, s.cfg.Expiration)
} }
} else { } else {
s.mux.Lock()
s.entries[key] = e s.entries[key] = e
s.mux.Unlock()
} }
} }
func (s *storage) delete(key string) { func (s *storage) delete(key string) {
s.mux.Lock()
defer s.mux.Lock()
if s.cfg.Storage != nil { if s.cfg.Storage != nil {
_ = s.cfg.Storage.Delete(key) _ = s.cfg.Storage.Delete(key)
_ = s.cfg.Storage.Delete(key + "_body") _ = s.cfg.Storage.Delete(key + "_body")
} else { } else {
s.mux.Lock()
delete(s.entries, key) delete(s.entries, key)
s.mux.Unlock()
} }
} }