Yann Stepienik 1 год назад
Родитель
Сommit
902c823d7d
2 измененных файлов с 5 добавлено и 3 удалено
  1. 1 1
      package.json
  2. 4 2
      src/utils/db.go

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "cosmos-server",
-  "version": "0.12.0-unstable44",
+  "version": "0.12.0-unstable45",
   "description": "",
   "main": "test-server.js",
   "bugs": {

+ 4 - 2
src/utils/db.go

@@ -107,6 +107,7 @@ func InitDBBuffers() {
 
 func flushBuffer(collectionName string) {
 	bufferLock.Lock()
+	defer bufferLock.Unlock()
 	objects, exists := writeBuffer[collectionName]
 	if exists && len(objects) > 0 {
 		collection, errG := GetCollection(GetRootAppId(), collectionName)
@@ -119,11 +120,11 @@ func flushBuffer(collectionName string) {
 		}
 		writeBuffer[collectionName] = make([]map[string]interface{}, 0)
 	}
-	bufferLock.Unlock()
 }
 
 func flushAllBuffers() {
 	bufferLock.Lock()
+	defer bufferLock.Unlock()
 	for collectionName, objects := range writeBuffer {
 		if len(objects) > 0 {
 			collection, errG := GetCollection(GetRootAppId(), collectionName)
@@ -137,7 +138,6 @@ func flushAllBuffers() {
 			writeBuffer[collectionName] = make([]map[string]interface{}, 0)
 		}
 	}
-	bufferLock.Unlock()
 }
 
 func BufferedDBWrite(collectionName string, object map[string]interface{}) {
@@ -147,6 +147,8 @@ func BufferedDBWrite(collectionName string, object map[string]interface{}) {
 	if len(writeBuffer[collectionName]) >= bufferCapacity {
 		bufferLock.Unlock()
 		flushBuffer(collectionName)
+	} else {
+		bufferLock.Unlock()
 	}
 }