counter usa i 'chanel'

This commit is contained in:
Michele 2016-12-20 15:37:32 +01:00
parent a3a02eac55
commit 36f6aa8a83
6 changed files with 127 additions and 43 deletions

View file

@ -6,6 +6,11 @@ import (
"time"
)
type Counterchan struct {
tipo string
val int
}
// Counter structure
type Counter struct {
mu sync.Mutex
@ -33,57 +38,71 @@ func NewCounter() *Counter {
}
}
func (c *Counter) Run() {
for {
tocount := <-counter
switch tocount.tipo {
case "user":
c.addUser()
case "dup":
c.addDuplicate(tocount.val)
case "ins":
c.addInsert(tocount.val)
case "log":
c.addLog(tocount.val)
case "rem":
c.addRem(tocount.val)
case "wg":
if tocount.val > 0 {
c.addWG()
} else {
c.delWG()
}
case "err":
c.addErr(tocount.val)
}
}
}
// AddUser increment number of users managed
func (c *Counter) AddUser() {
c.mu.Lock()
defer c.mu.Unlock()
func (c *Counter) addUser() {
c.user++
}
// AddDuplicate increment number of duplicates log
func (c *Counter) AddDuplicate(add int) {
c.mu.Lock()
defer c.mu.Unlock()
func (c *Counter) addDuplicate(add int) {
c.dup += add
}
// AddInsert increment number of inserted rows
func (c *Counter) AddInsert(add int) {
c.mu.Lock()
defer c.mu.Unlock()
func (c *Counter) addInsert(add int) {
c.insert += add
}
// AddLog increment number of log's rows managed
func (c *Counter) AddLog(add int) {
c.mu.Lock()
defer c.mu.Unlock()
func (c *Counter) addLog(add int) {
c.log += add
}
//AddRem increment removed logs row
func (c *Counter) AddRem(add int) {
c.mu.Lock()
defer c.mu.Unlock()
func (c *Counter) addRem(add int) {
c.rem += add
}
// AddWG ...
func (c *Counter) AddWG() {
c.mu.Lock()
defer c.mu.Unlock()
func (c *Counter) addWG() {
c.wg++
}
// AddErr ...
func (c *Counter) AddErr(add int) {
c.mu.Lock()
defer c.mu.Unlock()
func (c *Counter) addErr(add int) {
c.err += add
}
// DelWG ...
func (c *Counter) DelWG() {
func (c *Counter) delWG() {
c.mu.Lock()
defer c.mu.Unlock()
c.wg--