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

4
README
View File

@ -51,7 +51,9 @@ Usage:
-cache=32: cache size in MB # Cache size for LevelDB
-driver="leveldb": type of database (leveldb, sqlite)
-host="": host to bind
-gc-duration="24h": Duration between forced GCs.
-port=6379: port to bind
-version=false: Print version and exit.
To use mtredisalize with your particular world you have to modify the
world.mt of the world.
@ -75,4 +77,4 @@ The mandatory path is the path to the database file: map.sqlite in case
of SQLite3 and the directoy map.db in case of LevelDB right beside the world.mt file.
This is Free Software under the terms of the MIT license. See LICENSE file for details.
(c) 2014 by Sascha L. Teichmann
(c) 2014 by Sascha L. Teichmann

39
main.go
View File

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