Fixing synchronization bug in ctl functionality

This commit is contained in:
Farhan Khan 2021-08-07 19:45:47 +00:00
parent c827375640
commit 4a3b5379b9

View File

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"io" "io"
"net" "net"
// "fmt"
) )
func startctl() { func startctl() {
@ -68,21 +69,18 @@ func handleClient(commandClient net.Conn) {
responseback.Message = "Ok" responseback.Message = "Ok"
case "add": case "add":
logConn("Manually added instance: " + commandmap.Endpoint) logConn("Manually added instance: " + commandmap.Endpoint)
ri_mutex.Lock() //ri_mutex.Lock()
_, exists := runninginstances[commandmap.Endpoint] _, exists := GetRunner(commandmap.Endpoint)
if exists == true { if exists == true {
logConn("Already exists: " + commandmap.Endpoint) logConn("Already exists: " + commandmap.Endpoint)
responseback.Message = "Exists: " + commandmap.Endpoint responseback.Message = "Exists: " + commandmap.Endpoint
} else { } else {
responseback.Message = "Adding: " + commandmap.Endpoint responseback.Message = "Adding: " + commandmap.Endpoint
o := RunningInstance{} ri_mutex.Lock()
o.recentactivities = newUniqueFifo(10)
o.recentactors = newUniqueFifo(10)
o.client = BuildClient(commandmap.Endpoint)
runninginstances[commandmap.Endpoint] = o
go StartInstance(commandmap.Endpoint) go StartInstance(commandmap.Endpoint)
ri_mutex.Unlock()
} }
ri_mutex.Unlock() //ri_mutex.Unlock()
case "suspend": case "suspend":
logFatal.Fatal("Suspend") logFatal.Fatal("Suspend")
case "resume": case "resume":
@ -94,7 +92,9 @@ func handleClient(commandClient net.Conn) {
responseback.Type = "status" responseback.Type = "status"
responseback.RunningInstances = runninginstances responseback.RunningInstances = runninginstances
ri_mutex.Lock()
responsebytes, err := json.Marshal(responseback) responsebytes, err := json.Marshal(responseback)
ri_mutex.Unlock()
if err != nil { if err != nil {
logFatal.Fatal(err) logFatal.Fatal(err)
} }
@ -107,7 +107,9 @@ func handleClient(commandClient net.Conn) {
logFatal.Fatal("Error on write:", err) logFatal.Fatal("Error on write:", err)
} }
ri_mutex.Lock()
responsebyte, err := json.Marshal(responseback) responsebyte, err := json.Marshal(responseback)
ri_mutex.Unlock()
if err != nil { if err != nil {
logFatal.Fatal("Error response back: ", err) logFatal.Fatal("Error response back: ", err)
} }