From e38d8eb9fc02d9bdb09f2e5ed715f89797fe54e3 Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Wed, 6 Aug 2014 10:52:37 +0200 Subject: [PATCH] Added cyclic garbage collection for a defined interval. Defaults to 24h --- README | 4 +++- main.go | 39 +++++++++++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/README b/README index f1237f3..8c22b4d 100644 --- a/README +++ b/README @@ -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 \ No newline at end of file +(c) 2014 by Sascha L. Teichmann diff --git a/main.go b/main.go index a8a75df..44994a2 100644 --- a/main.go +++ b/main.go @@ -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.") } } }