Added cyclic garbage collection for a defined interval. Defaults to 24h

This commit is contained in:
Sascha L. Teichmann 2014-08-06 10:52:37 +02:00
parent 66b85f199c
commit e38d8eb9fc
2 changed files with 34 additions and 9 deletions

2
README
View File

@ -51,7 +51,9 @@ Usage:
-cache=32: cache size in MB # Cache size for LevelDB -cache=32: cache size in MB # Cache size for LevelDB
-driver="leveldb": type of database (leveldb, sqlite) -driver="leveldb": type of database (leveldb, sqlite)
-host="": host to bind -host="": host to bind
-gc-duration="24h": Duration between forced GCs.
-port=6379: port to bind -port=6379: port to bind
-version=false: Print version and exit.
To use mtredisalize with your particular world you have to modify the To use mtredisalize with your particular world you have to modify the
world.mt of the world. world.mt of the world.

39
main.go
View File

@ -11,23 +11,32 @@ import (
"net" "net"
"os" "os"
"os/signal" "os/signal"
"runtime"
"time"
) )
const Version = "pre0.1" const (
Version = "pre0.1"
GCDuration = "24h"
)
func main() { func main() {
var port int var (
var host string port int
var driver string host string
var cacheSize int driver string
var version bool cacheSize int
version bool
gcDuration string
)
flag.IntVar(&port, "port", 6379, "port to bind") flag.IntVar(&port, "port", 6379, "port to bind")
flag.StringVar(&driver, "driver", "leveldb", "type of database (leveldb, sqlite)") flag.StringVar(&driver, "driver", "leveldb", "type of database (leveldb, sqlite)")
flag.StringVar(&host, "host", "", "host to bind") flag.StringVar(&host, "host", "", "host to bind")
flag.IntVar(&cacheSize, "cache", 32, "cache size in MB") flag.IntVar(&cacheSize, "cache", 32, "cache size in MB")
flag.BoolVar(&version, "version", false, "Print version and exit.") flag.BoolVar(&version, "version", false, "Print version and exit.")
flag.StringVar(&gcDuration, "gc-duration", GCDuration, "Duration between forced GCs.")
flag.Parse() flag.Parse()
if version { if version {
@ -41,8 +50,15 @@ func main() {
log.Fatal("Missing path to world") log.Fatal("Missing path to world")
} }
var err error var (
var backend Backend err error
backend Backend
gcDur time.Duration
)
if gcDur, err = time.ParseDuration(gcDuration); err != nil {
log.Fatal(err)
}
if driver == "sqlite" { if driver == "sqlite" {
if backend, err = NewSqliteBackend(args[0]); err != nil { if backend, err = NewSqliteBackend(args[0]); err != nil {
@ -80,6 +96,9 @@ func main() {
} }
}() }()
log.Printf("Doing garbage collection every: %s", gcDur)
gcChan := time.Tick(gcDur)
for { for {
select { select {
case conn := <-connChan: case conn := <-connChan:
@ -93,6 +112,10 @@ func main() {
case <-sigChan: case <-sigChan:
log.Println("Shutting down") log.Println("Shutting down")
return return
case <-gcChan:
log.Println("Starting garbage collection.")
runtime.GC()
log.Println("Garbage collection done.")
} }
} }
} }