added rwmutex to db calls

This commit is contained in:
0xdcarns
2023-02-09 19:30:59 -05:00
parent d412287aed
commit d6b1fb4b17

View File

@@ -4,6 +4,7 @@ import (
"crypto/rand" "crypto/rand"
"encoding/json" "encoding/json"
"errors" "errors"
"sync"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
@@ -84,6 +85,8 @@ const (
isConnected = "isconnected" isConnected = "isconnected"
) )
var dbMutex sync.RWMutex
func getCurrentDB() map[string]interface{} { func getCurrentDB() map[string]interface{} {
switch servercfg.GetDB() { switch servercfg.GetDB() {
case "rqlite": case "rqlite":
@@ -150,6 +153,8 @@ func IsJSONString(value string) bool {
// Insert - inserts object into db // Insert - inserts object into db
func Insert(key string, value string, tableName string) error { func Insert(key string, value string, tableName string) error {
dbMutex.Lock()
defer dbMutex.Unlock()
if key != "" && value != "" && IsJSONString(value) { if key != "" && value != "" && IsJSONString(value) {
return getCurrentDB()[INSERT].(func(string, string, string) error)(key, value, tableName) return getCurrentDB()[INSERT].(func(string, string, string) error)(key, value, tableName)
} else { } else {
@@ -159,6 +164,8 @@ func Insert(key string, value string, tableName string) error {
// InsertPeer - inserts peer into db // InsertPeer - inserts peer into db
func InsertPeer(key string, value string) error { func InsertPeer(key string, value string) error {
dbMutex.Lock()
defer dbMutex.Unlock()
if key != "" && value != "" && IsJSONString(value) { if key != "" && value != "" && IsJSONString(value) {
return getCurrentDB()[INSERT_PEER].(func(string, string) error)(key, value) return getCurrentDB()[INSERT_PEER].(func(string, string) error)(key, value)
} else { } else {
@@ -168,11 +175,15 @@ func InsertPeer(key string, value string) error {
// DeleteRecord - deletes a record from db // DeleteRecord - deletes a record from db
func DeleteRecord(tableName string, key string) error { func DeleteRecord(tableName string, key string) error {
dbMutex.Lock()
defer dbMutex.Unlock()
return getCurrentDB()[DELETE].(func(string, string) error)(tableName, key) return getCurrentDB()[DELETE].(func(string, string) error)(tableName, key)
} }
// DeleteAllRecords - removes a table and remakes // DeleteAllRecords - removes a table and remakes
func DeleteAllRecords(tableName string) error { func DeleteAllRecords(tableName string) error {
dbMutex.Lock()
defer dbMutex.Unlock()
err := getCurrentDB()[DELETE_ALL].(func(string) error)(tableName) err := getCurrentDB()[DELETE_ALL].(func(string) error)(tableName)
if err != nil { if err != nil {
return err return err
@@ -198,6 +209,8 @@ func FetchRecord(tableName string, key string) (string, error) {
// FetchRecords - fetches all records in given table // FetchRecords - fetches all records in given table
func FetchRecords(tableName string) (map[string]string, error) { func FetchRecords(tableName string) (map[string]string, error) {
dbMutex.RLock()
defer dbMutex.RUnlock()
return getCurrentDB()[FETCH_ALL].(func(string) (map[string]string, error))(tableName) return getCurrentDB()[FETCH_ALL].(func(string) (map[string]string, error))(tableName)
} }