mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-05 16:57:51 +08:00
added rwmutex to db calls
This commit is contained in:
@@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user