From c42884af4f8904d2c4c8d6fa1236d9977750fec8 Mon Sep 17 00:00:00 2001 From: Kirill Lyubimov Date: Mon, 18 Feb 2019 17:09:03 +0300 Subject: [PATCH] collector: use RWMutex --- collector.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/collector.go b/collector.go index 7c72b07..c21e518 100644 --- a/collector.go +++ b/collector.go @@ -25,7 +25,7 @@ type Table struct { // Collector - query collector type Collector struct { Tables map[string]*Table - mu sync.Mutex + mu sync.RWMutex Count int FlushInterval int Sender Sender @@ -142,22 +142,31 @@ func (c *Collector) AddTable(name string) { c.addTable(name) } -func (c *Collector) addTable(name string) { +func (c *Collector) addTable(name string) *Table { t := NewTable(name, c.Sender, c.Count, c.FlushInterval) c.Tables[name] = t t.RunTimer() + return t } // Push - adding query to collector with query params (with query) and rows func (c *Collector) Push(params string, content string) { + c.mu.RLock() + table, ok := c.Tables[params] + if ok { + table.Add(content) + c.mu.RUnlock() + return + } + c.mu.RUnlock() c.mu.Lock() - defer c.mu.Unlock() - _, ok := c.Tables[params] + table, ok = c.Tables[params] if !ok { //log.Printf("'%+v'\n", params) - c.addTable(params) + table = c.addTable(params) } - c.Tables[params].Add(content) + table.Add(content) + c.mu.Unlock() } // ParseQuery - parsing inbound query to unified format (params/query), content (query data)