From 8bff28e0dcebc91ab3fe827870c5a49137a362fc Mon Sep 17 00:00:00 2001 From: Michele Date: Wed, 4 Jan 2017 17:40:59 +0100 Subject: [PATCH] ID => SHA256 --- consumer.go | 12 +++++++++++- counter.go | 2 -- main.go | 12 +++++++----- options.go | 2 ++ 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/consumer.go b/consumer.go index 8059ba0..70701bb 100644 --- a/consumer.go +++ b/consumer.go @@ -4,6 +4,8 @@ package main import ( "fmt" // "github.com/garyburd/redigo/redis" + "crypto/sha256" + "encoding/hex" "log" "strconv" "strings" @@ -19,6 +21,14 @@ type consumed struct { empty bool } +func hash(val []byte) string { + + h := sha256.New() + h.Write(val) + + return hex.EncodeToString(h.Sum(nil)) +} + func consumer() { for { @@ -71,7 +81,7 @@ func consumer() { } ml := MongoLogin{ // genera l' _ID con user e timestamp - ID: fmt.Sprintf("%s_%s", prod.user, time.Unix(date, 0).Format("20060102T150405")), + ID: hash([]byte(fmt.Sprintf("%s%s", prod.user, time.Unix(date, 0).Format("20060102T1504")))), // Format("20060102T150405") User: prod.user, Protocol: sval[0], IP: sval[2], diff --git a/counter.go b/counter.go index c7634d0..f9505d2 100644 --- a/counter.go +++ b/counter.go @@ -103,8 +103,6 @@ func (c *Counter) addErr(add int) { // DelWG ... func (c *Counter) delWG() { - c.mu.Lock() - defer c.mu.Unlock() c.wg-- } diff --git a/main.go b/main.go index b24d2ee..d2d8f93 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,7 @@ import ( ) const ( - _Version = "v3.1.0" + _Version = "v3.1.1" _Producer = 0 _Consumer = 1 _Remover = 2 @@ -90,16 +90,18 @@ func main() { count = NewCounter() - consume = make(chan produced) - remove = make(chan consumed) + consume = make(chan produced, opts.Queue) + remove = make(chan consumed, opts.Queue) loop = true done = make(chan bool) counter = make(chan Counterchan) go count.Run() go producer() - go consumer() - go remover() + for i := 0; i < opts.Queue; i++ { + go consumer() + go remover() + } <-done fmt.Printf("Done\n") diff --git a/options.go b/options.go index 9863ef9..95defde 100644 --- a/options.go +++ b/options.go @@ -30,6 +30,7 @@ type Options struct { Hostname string Month string Pidfile string + Queue int } var ( @@ -71,5 +72,6 @@ func init() { flag.BoolVar(&opts.Debug, "D", false, "Debug") flag.BoolVar(&opts.Bulk, "B", false, "Bulk") flag.IntVar(&opts.MaxError, "E", 100, "Max Mongodb Error") + flag.IntVar(&opts.Queue, "q", 2, "parallel consumer") flag.StringVar(&opts.Pidfile, "p", opts.Pidfile, "pid file") }