Commit de1341b1 authored by Sietse Ringers's avatar Sietse Ringers
Browse files

refactor: make memory DB mutexes unnamed for consistency with other mutexes

parent b1d7a1f0
...@@ -12,7 +12,7 @@ import ( ...@@ -12,7 +12,7 @@ import (
// mocking some behaviour, as noted on the specific functions. // mocking some behaviour, as noted on the specific functions.
type memoryDB struct { type memoryDB struct {
lock sync.Mutex sync.Mutex
users map[string]keysharecore.User users map[string]keysharecore.User
} }
...@@ -22,8 +22,8 @@ func NewMemoryDB() DB { ...@@ -22,8 +22,8 @@ func NewMemoryDB() DB {
func (db *memoryDB) user(username string) (*User, error) { func (db *memoryDB) user(username string) (*User, error) {
// Ensure access to database is single-threaded // Ensure access to database is single-threaded
db.lock.Lock() db.Lock()
defer db.lock.Unlock() defer db.Unlock()
// Check and fetch user data // Check and fetch user data
data, ok := db.users[username] data, ok := db.users[username]
...@@ -35,8 +35,8 @@ func (db *memoryDB) user(username string) (*User, error) { ...@@ -35,8 +35,8 @@ func (db *memoryDB) user(username string) (*User, error) {
func (db *memoryDB) AddUser(user *User) error { func (db *memoryDB) AddUser(user *User) error {
// Ensure access to database is single-threaded // Ensure access to database is single-threaded
db.lock.Lock() db.Lock()
defer db.lock.Unlock() defer db.Unlock()
// Check and insert user // Check and insert user
_, exists := db.users[user.Username] _, exists := db.users[user.Username]
...@@ -49,8 +49,8 @@ func (db *memoryDB) AddUser(user *User) error { ...@@ -49,8 +49,8 @@ func (db *memoryDB) AddUser(user *User) error {
func (db *memoryDB) updateUser(user *User) error { func (db *memoryDB) updateUser(user *User) error {
// Ensure access to database is single-threaded // Ensure access to database is single-threaded
db.lock.Lock() db.Lock()
defer db.lock.Unlock() defer db.Unlock()
// Check and update user. // Check and update user.
_, exists := db.users[user.Username] _, exists := db.users[user.Username]
......
...@@ -15,7 +15,7 @@ type memoryUserData struct { ...@@ -15,7 +15,7 @@ type memoryUserData struct {
} }
type memoryDB struct { type memoryDB struct {
lock sync.Mutex sync.Mutex
userData map[string]memoryUserData userData map[string]memoryUserData
loginEmailTokens map[string]string loginEmailTokens map[string]string
...@@ -31,8 +31,8 @@ func newMemoryDB() db { ...@@ -31,8 +31,8 @@ func newMemoryDB() db {
} }
func (db *memoryDB) userIDByUsername(username string) (int64, error) { func (db *memoryDB) userIDByUsername(username string) (int64, error) {
db.lock.Lock() db.Lock()
defer db.lock.Unlock() defer db.Unlock()
data, ok := db.userData[username] data, ok := db.userData[username]
if !ok { if !ok {
return 0, keyshare.ErrUserNotFound return 0, keyshare.ErrUserNotFound
...@@ -41,8 +41,8 @@ func (db *memoryDB) userIDByUsername(username string) (int64, error) { ...@@ -41,8 +41,8 @@ func (db *memoryDB) userIDByUsername(username string) (int64, error) {
} }
func (db *memoryDB) scheduleUserRemoval(id int64, _ time.Duration) error { func (db *memoryDB) scheduleUserRemoval(id int64, _ time.Duration) error {
db.lock.Lock() db.Lock()
defer db.lock.Unlock() defer db.Unlock()
for username, user := range db.userData { for username, user := range db.userData {
if user.id == id { if user.id == id {
delete(db.userData, username) delete(db.userData, username)
...@@ -53,8 +53,8 @@ func (db *memoryDB) scheduleUserRemoval(id int64, _ time.Duration) error { ...@@ -53,8 +53,8 @@ func (db *memoryDB) scheduleUserRemoval(id int64, _ time.Duration) error {
} }
func (db *memoryDB) userIDByEmailToken(token string) (int64, error) { func (db *memoryDB) userIDByEmailToken(token string) (int64, error) {
db.lock.Lock() db.Lock()
defer db.lock.Unlock() defer db.Unlock()
userID, ok := db.verifyEmailTokens[token] userID, ok := db.verifyEmailTokens[token]
if !ok { if !ok {
...@@ -67,8 +67,8 @@ func (db *memoryDB) userIDByEmailToken(token string) (int64, error) { ...@@ -67,8 +67,8 @@ func (db *memoryDB) userIDByEmailToken(token string) (int64, error) {
} }
func (db *memoryDB) addEmailLoginToken(email, token string) error { func (db *memoryDB) addEmailLoginToken(email, token string) error {
db.lock.Lock() db.Lock()
defer db.lock.Unlock() defer db.Unlock()
found := false found := false
for _, user := range db.userData { for _, user := range db.userData {
...@@ -92,8 +92,8 @@ func (db *memoryDB) addEmailLoginToken(email, token string) error { ...@@ -92,8 +92,8 @@ func (db *memoryDB) addEmailLoginToken(email, token string) error {
} }
func (db *memoryDB) loginUserCandidates(token string) ([]loginCandidate, error) { func (db *memoryDB) loginUserCandidates(token string) ([]loginCandidate, error) {
db.lock.Lock() db.Lock()
defer db.lock.Unlock() defer db.Unlock()
email, ok := db.loginEmailTokens[token] email, ok := db.loginEmailTokens[token]
if !ok { if !ok {
...@@ -113,8 +113,8 @@ func (db *memoryDB) loginUserCandidates(token string) ([]loginCandidate, error) ...@@ -113,8 +113,8 @@ func (db *memoryDB) loginUserCandidates(token string) ([]loginCandidate, error)
} }
func (db *memoryDB) userIDByLoginToken(token, username string) (int64, error) { func (db *memoryDB) userIDByLoginToken(token, username string) (int64, error) {
db.lock.Lock() db.Lock()
defer db.lock.Unlock() defer db.Unlock()
email, ok := db.loginEmailTokens[token] email, ok := db.loginEmailTokens[token]
if !ok { if !ok {
...@@ -136,8 +136,8 @@ func (db *memoryDB) userIDByLoginToken(token, username string) (int64, error) { ...@@ -136,8 +136,8 @@ func (db *memoryDB) userIDByLoginToken(token, username string) (int64, error) {
} }
func (db *memoryDB) user(id int64) (user, error) { func (db *memoryDB) user(id int64) (user, error) {
db.lock.Lock() db.Lock()
defer db.lock.Unlock() defer db.Unlock()
for username, u := range db.userData { for username, u := range db.userData {
if u.id == id { if u.id == id {
var emailList []userEmail var emailList []userEmail
...@@ -166,8 +166,8 @@ func min(a, b int) int { ...@@ -166,8 +166,8 @@ func min(a, b int) int {
} }
func (db *memoryDB) logs(id int64, offset, amount int) ([]logEntry, error) { func (db *memoryDB) logs(id int64, offset, amount int) ([]logEntry, error) {
db.lock.Lock() db.Lock()
defer db.lock.Unlock() defer db.Unlock()
for _, user := range db.userData { for _, user := range db.userData {
if user.id == id { if user.id == id {
return user.logEntries[min(len(user.logEntries), offset):min(len(user.logEntries), offset+amount)], nil return user.logEntries[min(len(user.logEntries), offset):min(len(user.logEntries), offset+amount)], nil
...@@ -177,8 +177,8 @@ func (db *memoryDB) logs(id int64, offset, amount int) ([]logEntry, error) { ...@@ -177,8 +177,8 @@ func (db *memoryDB) logs(id int64, offset, amount int) ([]logEntry, error) {
} }
func (db *memoryDB) addEmail(id int64, email string) error { func (db *memoryDB) addEmail(id int64, email string) error {
db.lock.Lock() db.Lock()
defer db.lock.Unlock() defer db.Unlock()
for username, user := range db.userData { for username, user := range db.userData {
if user.id == id { if user.id == id {
user.email = append(user.email, email) user.email = append(user.email, email)
...@@ -190,8 +190,8 @@ func (db *memoryDB) addEmail(id int64, email string) error { ...@@ -190,8 +190,8 @@ func (db *memoryDB) addEmail(id int64, email string) error {
} }
func (db *memoryDB) scheduleEmailRemoval(id int64, email string, _ time.Duration) error { func (db *memoryDB) scheduleEmailRemoval(id int64, email string, _ time.Duration) error {
db.lock.Lock() db.Lock()
defer db.lock.Unlock() defer db.Unlock()
for username, user := range db.userData { for username, user := range db.userData {
if user.id == id { if user.id == id {
for i, emailv := range user.email { for i, emailv := range user.email {
...@@ -210,8 +210,8 @@ func (db *memoryDB) scheduleEmailRemoval(id int64, email string, _ time.Duration ...@@ -210,8 +210,8 @@ func (db *memoryDB) scheduleEmailRemoval(id int64, email string, _ time.Duration
} }
func (db *memoryDB) setSeen(id int64) error { func (db *memoryDB) setSeen(id int64) error {
db.lock.Lock() db.Lock()
defer db.lock.Unlock() defer db.Unlock()
for username, user := range db.userData { for username, user := range db.userData {
if user.id == id { if user.id == id {
user.lastActive = time.Now() user.lastActive = time.Now()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment