Browse Source

Race condition fixed

Loweel 4 years ago
parent
commit
923554d3ae
2 changed files with 23 additions and 16 deletions
  1. 16 16
      file.go
  2. 7 0
      matrix.go

+ 16 - 16
file.go

@@ -16,21 +16,29 @@ type dumpStruct struct {
 	MEH    []MEH   `json:"MEH"`
 	STATS  []STATS `json:"STATS"`
 }
+
+//GOOD is a record for good token
 type GOOD struct {
 	Token    string `json:"Token"`
 	LastSeen string `json:"LastSeen"`
 	Delta    string `json:"Age"`
 }
+
+//BAD is a record for good token
 type BAD struct {
 	Token    string `json:"Token"`
 	LastSeen string `json:"LastSeen"`
 	Delta    string `json:"Age"`
 }
+
+//MEH is a record for good token
 type MEH struct {
 	Token    string `json:"Token"`
 	LastSeen string `json:"LastSeen"`
 	Delta    string `json:"Age"`
 }
+
+//STATS is a record for good token
 type STATS struct {
 	Decision string `json:"Decision"`
 	Amount   int64  `json:"Amount"`
@@ -61,10 +69,10 @@ func handlepanic() {
 
 func saveBayesToFile() {
 
-	var tmpJSON, tmpBayes string
-	var DumpJson = new(dumpStruct)
+	var tmpJSON string
+	var DumpJSON = new(dumpStruct)
 
-	DumpJson.Update = time.Now().String()
+	DumpJSON.Update = time.Now().String()
 
 	log.Println("Trying to write json file")
 
@@ -74,7 +82,7 @@ func saveBayesToFile() {
 		t.Token = key.(string)
 		t.LastSeen = time.Unix(0, v).String()
 		t.Delta = time.Since(time.Unix(0, v)).String()
-		DumpJson.BAD = append(DumpJson.BAD, t)
+		DumpJSON.BAD = append(DumpJSON.BAD, t)
 		return true
 	})
 
@@ -84,7 +92,7 @@ func saveBayesToFile() {
 		t.Token = key.(string)
 		t.LastSeen = time.Unix(0, v).String()
 		t.Delta = time.Since(time.Unix(0, v)).String()
-		DumpJson.GOOD = append(DumpJson.GOOD, t)
+		DumpJSON.GOOD = append(DumpJSON.GOOD, t)
 		return true
 	})
 
@@ -94,7 +102,7 @@ func saveBayesToFile() {
 		t.Token = key.(string)
 		t.LastSeen = time.Unix(0, v).String()
 		t.Delta = time.Since(time.Unix(0, v)).String()
-		DumpJson.MEH = append(DumpJson.MEH, t)
+		DumpJSON.MEH = append(DumpJSON.MEH, t)
 		return true
 	})
 
@@ -102,25 +110,17 @@ func saveBayesToFile() {
 		var t STATS
 		t.Decision = key.(string)
 		t.Amount = value.(int64)
-		DumpJson.STATS = append(DumpJson.STATS, t)
+		DumpJSON.STATS = append(DumpJSON.STATS, t)
 
 		return true
 	})
 
-	if tmpJ, e := json.MarshalIndent(DumpJson, "", " "); e == nil {
+	if tmpJ, e := json.MarshalIndent(DumpJSON, "", " "); e == nil {
 		tmpJSON = fmt.Sprintf("%s", tmpJ)
 	} else {
 		tmpJSON = e.Error()
 	}
 
-	if tmpB, er := json.MarshalIndent(Classifier.bayez.Matrix, "", " "); er == nil {
-		tmpBayes = fmt.Sprintf("%s", tmpB)
-	} else {
-		tmpBayes = er.Error()
-	}
-
-	tmpJSON = fmt.Sprintf("%s\n%s", tmpJSON, tmpBayes)
-
 	dumpfile := os.Getenv("DUMPFILE")
 	if dumpfile == "" {
 		dumpfile = "bayes.json"

+ 7 - 0
matrix.go

@@ -123,6 +123,13 @@ func (c *ByClassifier) Janitor(size int) {
 //RefreshBayes refresh the bayesian using values we stored
 func (c *ByClassifier) RefreshBayes() {
 
+	defer func() {
+
+		if a := recover(); a != nil {
+			fmt.Println("OPS!: Recovering from:", a)
+		}
+	}()
+
 	log.Println("RefreshBayes Thread started")
 
 	ticker := time.NewTicker(5 * time.Minute)