member mutex move to manager.

This commit is contained in:
Miroslav Šedivý 2021-03-14 19:36:30 +01:00
parent 4207775fb3
commit 9483882c89
3 changed files with 27 additions and 48 deletions

View File

@ -5,7 +5,6 @@ import (
"fmt"
"io/ioutil"
"os"
"sync"
"demodesk/neko/internal/types"
)
@ -13,13 +12,11 @@ import (
func New(config Config) types.MemberProvider {
return &MemberProviderCtx{
config: config,
mu: sync.Mutex{},
}
}
type MemberProviderCtx struct {
config Config
mu sync.Mutex
}
func (provider *MemberProviderCtx) Connect() error {
@ -31,9 +28,6 @@ func (provider *MemberProviderCtx) Disconnect() error {
}
func (provider *MemberProviderCtx) Authenticate(username string, password string) (string, types.MemberProfile, error) {
provider.mu.Lock()
defer provider.mu.Unlock()
// id will be also username
id := username
@ -51,9 +45,6 @@ func (provider *MemberProviderCtx) Authenticate(username string, password string
}
func (provider *MemberProviderCtx) Insert(username string, password string, profile types.MemberProfile) (string, error) {
provider.mu.Lock()
defer provider.mu.Unlock()
// id will be also username
id := username
@ -77,9 +68,6 @@ func (provider *MemberProviderCtx) Insert(username string, password string, prof
}
func (provider *MemberProviderCtx) UpdateProfile(id string, profile types.MemberProfile) error {
provider.mu.Lock()
defer provider.mu.Unlock()
entries, err := provider.deserialize()
if err != nil {
return err
@ -97,9 +85,6 @@ func (provider *MemberProviderCtx) UpdateProfile(id string, profile types.Member
}
func (provider *MemberProviderCtx) UpdatePassword(id string, password string) error {
provider.mu.Lock()
defer provider.mu.Unlock()
entries, err := provider.deserialize()
if err != nil {
return err
@ -118,9 +103,6 @@ func (provider *MemberProviderCtx) UpdatePassword(id string, password string) er
}
func (provider *MemberProviderCtx) Select(id string) (types.MemberProfile, error) {
provider.mu.Lock()
defer provider.mu.Unlock()
entry, err := provider.getEntry(id)
if err != nil {
return types.MemberProfile{}, err
@ -130,9 +112,6 @@ func (provider *MemberProviderCtx) Select(id string) (types.MemberProfile, error
}
func (provider *MemberProviderCtx) SelectAll(limit int, offset int) (map[string]types.MemberProfile, error) {
provider.mu.Lock()
defer provider.mu.Unlock()
profiles := map[string]types.MemberProfile{}
entries, err := provider.deserialize()
@ -153,9 +132,6 @@ func (provider *MemberProviderCtx) SelectAll(limit int, offset int) (map[string]
}
func (provider *MemberProviderCtx) Delete(id string) error {
provider.mu.Lock()
defer provider.mu.Unlock()
entries, err := provider.deserialize()
if err != nil {
return err

View File

@ -46,37 +46,64 @@ type MemberManagerCtx struct {
}
func (manager *MemberManagerCtx) Connect() error {
manager.mu.Lock()
defer manager.mu.Unlock()
return manager.provider.Connect()
}
func (manager *MemberManagerCtx) Disconnect() error {
manager.mu.Lock()
defer manager.mu.Unlock()
return manager.provider.Disconnect()
}
func (manager *MemberManagerCtx) Authenticate(username string, password string) (string, types.MemberProfile, error) {
manager.mu.Lock()
defer manager.mu.Unlock()
return manager.provider.Authenticate(username, password)
}
func (manager *MemberManagerCtx) Insert(username string, password string, profile types.MemberProfile) (string, error) {
manager.mu.Lock()
defer manager.mu.Unlock()
return manager.provider.Insert(username, password, profile)
}
func (manager *MemberManagerCtx) Select(id string) (types.MemberProfile, error) {
manager.mu.Lock()
defer manager.mu.Unlock()
return manager.provider.Select(id)
}
func (manager *MemberManagerCtx) SelectAll(limit int, offset int) (map[string]types.MemberProfile, error) {
manager.mu.Lock()
defer manager.mu.Unlock()
return manager.provider.SelectAll(limit, offset)
}
func (manager *MemberManagerCtx) UpdateProfile(id string, profile types.MemberProfile) error {
manager.mu.Lock()
defer manager.mu.Unlock()
return manager.provider.UpdateProfile(id, profile)
}
func (manager *MemberManagerCtx) UpdatePassword(id string, password string) error {
manager.mu.Lock()
defer manager.mu.Unlock()
return manager.provider.UpdatePassword(id, password)
}
func (manager *MemberManagerCtx) Delete(id string) error {
manager.mu.Lock()
defer manager.mu.Unlock()
return manager.provider.Delete(id)
}

View File

@ -2,7 +2,6 @@ package object
import (
"fmt"
"sync"
"demodesk/neko/internal/types"
)
@ -11,14 +10,12 @@ func New(config Config) types.MemberProvider {
return &MemberProviderCtx{
config: config,
entries: make(map[string]*MemberEntry),
mu: sync.Mutex{},
}
}
type MemberProviderCtx struct {
config Config
entries map[string]*MemberEntry
mu sync.Mutex
}
func (provider *MemberProviderCtx) Connect() error {
@ -58,9 +55,6 @@ func (provider *MemberProviderCtx) Disconnect() error {
}
func (provider *MemberProviderCtx) Authenticate(username string, password string) (string, types.MemberProfile, error) {
provider.mu.Lock()
defer provider.mu.Unlock()
// id will be also username
id := username
@ -78,9 +72,6 @@ func (provider *MemberProviderCtx) Authenticate(username string, password string
}
func (provider *MemberProviderCtx) Insert(username string, password string, profile types.MemberProfile) (string, error) {
provider.mu.Lock()
defer provider.mu.Unlock()
// id will be also username
id := username
@ -99,9 +90,6 @@ func (provider *MemberProviderCtx) Insert(username string, password string, prof
}
func (provider *MemberProviderCtx) UpdateProfile(id string, profile types.MemberProfile) error {
provider.mu.Lock()
defer provider.mu.Unlock()
entry, ok := provider.entries[id]
if !ok {
return fmt.Errorf("Member ID does not exist.")
@ -113,9 +101,6 @@ func (provider *MemberProviderCtx) UpdateProfile(id string, profile types.Member
}
func (provider *MemberProviderCtx) UpdatePassword(id string, password string) error {
provider.mu.Lock()
defer provider.mu.Unlock()
entry, ok := provider.entries[id]
if !ok {
return fmt.Errorf("Member ID does not exist.")
@ -128,9 +113,6 @@ func (provider *MemberProviderCtx) UpdatePassword(id string, password string) er
}
func (provider *MemberProviderCtx) Select(id string) (types.MemberProfile, error) {
provider.mu.Lock()
defer provider.mu.Unlock()
entry, ok := provider.entries[id]
if !ok {
return types.MemberProfile{}, fmt.Errorf("Member ID does not exist.")
@ -140,9 +122,6 @@ func (provider *MemberProviderCtx) Select(id string) (types.MemberProfile, error
}
func (provider *MemberProviderCtx) SelectAll(limit int, offset int) (map[string]types.MemberProfile, error) {
provider.mu.Lock()
defer provider.mu.Unlock()
profiles := make(map[string]types.MemberProfile)
i := 0
@ -158,9 +137,6 @@ func (provider *MemberProviderCtx) SelectAll(limit int, offset int) (map[string]
}
func (provider *MemberProviderCtx) Delete(id string) error {
provider.mu.Lock()
defer provider.mu.Unlock()
_, ok := provider.entries[id]
if !ok {
return fmt.Errorf("Member ID does not exist.")