Skip to content

Commit

Permalink
collector: use RWMutex
Browse files Browse the repository at this point in the history
  • Loading branch information
lenstr committed Feb 18, 2019
1 parent c8480f8 commit c42884a
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions collector.go
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c42884a

Please sign in to comment.