mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
member mutex move to manager.
This commit is contained in:
parent
4207775fb3
commit
9483882c89
@ -5,7 +5,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"demodesk/neko/internal/types"
|
"demodesk/neko/internal/types"
|
||||||
)
|
)
|
||||||
@ -13,13 +12,11 @@ import (
|
|||||||
func New(config Config) types.MemberProvider {
|
func New(config Config) types.MemberProvider {
|
||||||
return &MemberProviderCtx{
|
return &MemberProviderCtx{
|
||||||
config: config,
|
config: config,
|
||||||
mu: sync.Mutex{},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type MemberProviderCtx struct {
|
type MemberProviderCtx struct {
|
||||||
config Config
|
config Config
|
||||||
mu sync.Mutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (provider *MemberProviderCtx) Connect() error {
|
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) {
|
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 will be also username
|
||||||
id := 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) {
|
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 will be also username
|
||||||
id := 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 {
|
func (provider *MemberProviderCtx) UpdateProfile(id string, profile types.MemberProfile) error {
|
||||||
provider.mu.Lock()
|
|
||||||
defer provider.mu.Unlock()
|
|
||||||
|
|
||||||
entries, err := provider.deserialize()
|
entries, err := provider.deserialize()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -97,9 +85,6 @@ func (provider *MemberProviderCtx) UpdateProfile(id string, profile types.Member
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (provider *MemberProviderCtx) UpdatePassword(id string, password string) error {
|
func (provider *MemberProviderCtx) UpdatePassword(id string, password string) error {
|
||||||
provider.mu.Lock()
|
|
||||||
defer provider.mu.Unlock()
|
|
||||||
|
|
||||||
entries, err := provider.deserialize()
|
entries, err := provider.deserialize()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -118,9 +103,6 @@ func (provider *MemberProviderCtx) UpdatePassword(id string, password string) er
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (provider *MemberProviderCtx) Select(id string) (types.MemberProfile, error) {
|
func (provider *MemberProviderCtx) Select(id string) (types.MemberProfile, error) {
|
||||||
provider.mu.Lock()
|
|
||||||
defer provider.mu.Unlock()
|
|
||||||
|
|
||||||
entry, err := provider.getEntry(id)
|
entry, err := provider.getEntry(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return types.MemberProfile{}, err
|
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) {
|
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{}
|
profiles := map[string]types.MemberProfile{}
|
||||||
|
|
||||||
entries, err := provider.deserialize()
|
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 {
|
func (provider *MemberProviderCtx) Delete(id string) error {
|
||||||
provider.mu.Lock()
|
|
||||||
defer provider.mu.Unlock()
|
|
||||||
|
|
||||||
entries, err := provider.deserialize()
|
entries, err := provider.deserialize()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -46,37 +46,64 @@ type MemberManagerCtx struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (manager *MemberManagerCtx) Connect() error {
|
func (manager *MemberManagerCtx) Connect() error {
|
||||||
|
manager.mu.Lock()
|
||||||
|
defer manager.mu.Unlock()
|
||||||
|
|
||||||
return manager.provider.Connect()
|
return manager.provider.Connect()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (manager *MemberManagerCtx) Disconnect() error {
|
func (manager *MemberManagerCtx) Disconnect() error {
|
||||||
|
manager.mu.Lock()
|
||||||
|
defer manager.mu.Unlock()
|
||||||
|
|
||||||
return manager.provider.Disconnect()
|
return manager.provider.Disconnect()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (manager *MemberManagerCtx) Authenticate(username string, password string) (string, types.MemberProfile, error) {
|
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)
|
return manager.provider.Authenticate(username, password)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (manager *MemberManagerCtx) Insert(username string, password string, profile types.MemberProfile) (string, error) {
|
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)
|
return manager.provider.Insert(username, password, profile)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (manager *MemberManagerCtx) Select(id string) (types.MemberProfile, error) {
|
func (manager *MemberManagerCtx) Select(id string) (types.MemberProfile, error) {
|
||||||
|
manager.mu.Lock()
|
||||||
|
defer manager.mu.Unlock()
|
||||||
|
|
||||||
return manager.provider.Select(id)
|
return manager.provider.Select(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (manager *MemberManagerCtx) SelectAll(limit int, offset int) (map[string]types.MemberProfile, error) {
|
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)
|
return manager.provider.SelectAll(limit, offset)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (manager *MemberManagerCtx) UpdateProfile(id string, profile types.MemberProfile) error {
|
func (manager *MemberManagerCtx) UpdateProfile(id string, profile types.MemberProfile) error {
|
||||||
|
manager.mu.Lock()
|
||||||
|
defer manager.mu.Unlock()
|
||||||
|
|
||||||
return manager.provider.UpdateProfile(id, profile)
|
return manager.provider.UpdateProfile(id, profile)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (manager *MemberManagerCtx) UpdatePassword(id string, password string) error {
|
func (manager *MemberManagerCtx) UpdatePassword(id string, password string) error {
|
||||||
|
manager.mu.Lock()
|
||||||
|
defer manager.mu.Unlock()
|
||||||
|
|
||||||
return manager.provider.UpdatePassword(id, password)
|
return manager.provider.UpdatePassword(id, password)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (manager *MemberManagerCtx) Delete(id string) error {
|
func (manager *MemberManagerCtx) Delete(id string) error {
|
||||||
|
manager.mu.Lock()
|
||||||
|
defer manager.mu.Unlock()
|
||||||
|
|
||||||
return manager.provider.Delete(id)
|
return manager.provider.Delete(id)
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package object
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"demodesk/neko/internal/types"
|
"demodesk/neko/internal/types"
|
||||||
)
|
)
|
||||||
@ -11,14 +10,12 @@ func New(config Config) types.MemberProvider {
|
|||||||
return &MemberProviderCtx{
|
return &MemberProviderCtx{
|
||||||
config: config,
|
config: config,
|
||||||
entries: make(map[string]*MemberEntry),
|
entries: make(map[string]*MemberEntry),
|
||||||
mu: sync.Mutex{},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type MemberProviderCtx struct {
|
type MemberProviderCtx struct {
|
||||||
config Config
|
config Config
|
||||||
entries map[string]*MemberEntry
|
entries map[string]*MemberEntry
|
||||||
mu sync.Mutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (provider *MemberProviderCtx) Connect() error {
|
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) {
|
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 will be also username
|
||||||
id := 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) {
|
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 will be also username
|
||||||
id := 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 {
|
func (provider *MemberProviderCtx) UpdateProfile(id string, profile types.MemberProfile) error {
|
||||||
provider.mu.Lock()
|
|
||||||
defer provider.mu.Unlock()
|
|
||||||
|
|
||||||
entry, ok := provider.entries[id]
|
entry, ok := provider.entries[id]
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("Member ID does not exist.")
|
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 {
|
func (provider *MemberProviderCtx) UpdatePassword(id string, password string) error {
|
||||||
provider.mu.Lock()
|
|
||||||
defer provider.mu.Unlock()
|
|
||||||
|
|
||||||
entry, ok := provider.entries[id]
|
entry, ok := provider.entries[id]
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("Member ID does not exist.")
|
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) {
|
func (provider *MemberProviderCtx) Select(id string) (types.MemberProfile, error) {
|
||||||
provider.mu.Lock()
|
|
||||||
defer provider.mu.Unlock()
|
|
||||||
|
|
||||||
entry, ok := provider.entries[id]
|
entry, ok := provider.entries[id]
|
||||||
if !ok {
|
if !ok {
|
||||||
return types.MemberProfile{}, fmt.Errorf("Member ID does not exist.")
|
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) {
|
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)
|
profiles := make(map[string]types.MemberProfile)
|
||||||
|
|
||||||
i := 0
|
i := 0
|
||||||
@ -158,9 +137,6 @@ func (provider *MemberProviderCtx) SelectAll(limit int, offset int) (map[string]
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (provider *MemberProviderCtx) Delete(id string) error {
|
func (provider *MemberProviderCtx) Delete(id string) error {
|
||||||
provider.mu.Lock()
|
|
||||||
defer provider.mu.Unlock()
|
|
||||||
|
|
||||||
_, ok := provider.entries[id]
|
_, ok := provider.entries[id]
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("Member ID does not exist.")
|
return fmt.Errorf("Member ID does not exist.")
|
||||||
|
Loading…
Reference in New Issue
Block a user