Browse Source

Fixed race condition

Loweel 4 years ago
parent
commit
cafeec6749
1 changed files with 7 additions and 3 deletions
  1. 7 3
      matrix.go

+ 7 - 3
matrix.go

@@ -31,6 +31,7 @@ type ByClassifier struct {
 	MEH   sync.Map
 	STATS sync.Map
 	bayez *multibayes.Classifier
+	busy  sync.Mutex
 }
 
 //AddStats adds the statistics after proper blocking.
@@ -101,8 +102,9 @@ func (c *ByClassifier) Posterior(hdr string) (ff map[string]float64) {
 		}
 	}()
 
+	c.busy.Lock()
 	ff = c.bayez.Posterior(hdr)
-
+	defer c.busy.Unlock()
 	return ff
 
 }
@@ -140,12 +142,11 @@ func (c *ByClassifier) RefreshBayes() {
 
 		c.bayez = multibayes.NewClassifier()
 		c.bayez.MinClassSize = 0
-
+		c.busy.Lock()
 		c.BAD.Range(func(key interface{}, value interface{}) bool {
 			c.bayez.Add(key.(string), []string{"BAD"})
 			return true
 		})
-
 		c.GOOD.Range(func(key interface{}, value interface{}) bool {
 			c.bayez.Add(key.(string), []string{"GOOD"})
 			return true
@@ -155,6 +156,7 @@ func (c *ByClassifier) RefreshBayes() {
 			c.bayez.Add(key.(string), []string{"GOOD", "BAD"})
 			return true
 		})
+		c.busy.Unlock()
 
 	}
 
@@ -184,8 +186,10 @@ func (c *ByClassifier) enroll() {
 	ControPlane.GoodTokens = make(chan string, 2048)
 	ControPlane.StatsTokens = make(chan string, 2048)
 
+	c.busy.Lock()
 	c.bayez = multibayes.NewClassifier()
 	c.bayez.MinClassSize = 0
+	c.busy.Unlock()
 
 	c.readInitList("blacklist.txt", "BAD")
 	c.readInitList("whitelist.txt", "GOOD")