llmongodb/main.go

135 lines
2.6 KiB
Go
Raw Normal View History

2015-07-09 17:50:13 +02:00
// llmongo.go
package main
import (
"flag"
"fmt"
2015-07-10 12:16:39 +02:00
"log"
2015-07-30 15:55:32 +02:00
"os"
2015-11-19 17:12:53 +01:00
"os/signal"
"path"
"path/filepath"
2015-11-19 17:12:53 +01:00
"syscall"
2015-07-10 14:35:35 +02:00
"time"
2015-07-09 17:50:13 +02:00
)
type Options struct {
RedisTTL time.Duration
CurrentPath string
Exe string
LogFile string
Timeout bool
Debug bool
Version bool
2015-11-19 17:12:53 +01:00
BufferSize int
2015-07-09 17:50:13 +02:00
}
const (
2015-11-19 17:12:53 +01:00
_VERSION = "v2.1.0"
)
2015-07-09 17:50:13 +02:00
var (
opts = Options{
RedisTTL: time.Hour * 11688, // 16 mesi
LogFile: "log/llmongo.log",
2015-07-09 17:50:13 +02:00
}
loop = true
ttl = time.Second * 55
done = make(chan bool)
2015-11-19 17:12:53 +01:00
msgs chan string
count = 0
errCount = 0
2015-07-09 17:50:13 +02:00
)
func usage() {
2015-11-19 17:12:53 +01:00
fmt.Println("Usage: llmongo -m <mongo uri> -r <redis uri> -t <ttl> -l <logfile> -b <buffer size> -T -D -v\n")
2015-07-09 17:50:13 +02:00
os.Exit(0)
}
func init() {
var err error
opts.CurrentPath, err = filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
opts.LogFile = path.Join(opts.CurrentPath, opts.LogFile)
2015-07-30 15:55:32 +02:00
pid.PIDFile = path.Join(opts.CurrentPath, "run", path.Base(os.Args[0])+".pid")
opts.Exe = path.Base(os.Args[0])
2015-07-30 10:21:01 +02:00
flag.StringVar(&dbs.MongoUri, "m", dbs.MongoUri, "Mongodb")
flag.StringVar(&dbs.RedisUri, "r", dbs.RedisUri, "Redis")
2015-07-10 12:16:39 +02:00
flag.StringVar(&opts.LogFile, "l", opts.LogFile, "Logs filename")
flag.DurationVar(&opts.RedisTTL, "t", opts.RedisTTL, "Redis TTL")
flag.BoolVar(&opts.Version, "v", false, "Version")
flag.BoolVar(&opts.Timeout, "T", false, "Timeout")
flag.BoolVar(&opts.Debug, "D", false, "Debug")
2015-11-19 17:12:53 +01:00
flag.IntVar(&opts.BufferSize, "b", 1, "Buffer size")
}
func stopLoop() {
loop = false
2015-07-09 17:50:13 +02:00
}
func main() {
flag.Usage = usage
2015-07-10 14:35:35 +02:00
flag.Parse()
2015-07-09 17:50:13 +02:00
if opts.Version {
fmt.Println(os.Args[0], _VERSION)
os.Exit(0)
}
2015-11-19 17:12:53 +01:00
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
signal.Notify(c, syscall.SIGTERM)
go func() {
<-c
stopLoop()
}()
2015-07-10 12:16:39 +02:00
fs, err := os.OpenFile(opts.LogFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
fmt.Println("Log file error: ", err.Error())
os.Exit(-4)
}
2015-07-30 10:21:01 +02:00
defer fs.Close()
2015-07-10 14:35:35 +02:00
2015-07-10 12:16:39 +02:00
log.SetOutput(fs)
pid.Write(true)
defer pid.Remove()
2015-07-10 12:16:39 +02:00
start := time.Now()
fmt.Printf("Start: %+v\n", opts)
log.Printf("Start: %+v\n", opts)
2015-07-09 17:50:13 +02:00
dbs.poolRedis()
2015-07-30 10:21:01 +02:00
defer dbs.rdb.Close()
2015-07-09 17:50:13 +02:00
2015-07-30 10:21:01 +02:00
dbs.connectMongo()
defer dbs.mdb.Close()
2015-07-09 17:50:13 +02:00
if opts.Timeout {
time.AfterFunc(ttl, stopLoop)
}
2015-11-19 17:12:53 +01:00
msgs = make(chan string, opts.BufferSize)
go producer()
2015-07-10 14:35:35 +02:00
2015-11-19 17:12:53 +01:00
for loop {
start := time.Now()
user := <-msgs
fmt.Printf("Wait: %v\n\r", time.Since(start))
if user != "" {
go consumer(user)
}
}
fmt.Printf("Stop %v - login: %d - errors: %d - conn: %d\n\r", time.Since(start), count, errCount, dbs.rdb.ActiveCount())
log.Printf("Stop %v - login: %d - errors: %d - conn: %d\n\r", time.Since(start), count, errCount, dbs.rdb.ActiveCount())
2015-07-10 14:35:35 +02:00
}