2014-08-03 15:59:56 +02:00
|
|
|
// Copyright 2014 by Sascha L. Teichmann
|
|
|
|
// Use of this source code is governed by the MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2014-08-03 00:01:14 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2014-08-06 10:52:37 +02:00
|
|
|
"runtime"
|
|
|
|
"time"
|
2014-08-22 21:54:22 +02:00
|
|
|
|
|
|
|
"bitbucket.org/s_l_teichmann/mtredisalize/common"
|
2014-08-03 00:01:14 +02:00
|
|
|
)
|
|
|
|
|
2014-08-06 10:52:37 +02:00
|
|
|
const (
|
2014-08-11 16:19:08 +02:00
|
|
|
Version = "0.3"
|
2014-08-06 10:52:37 +02:00
|
|
|
GCDuration = "24h"
|
|
|
|
)
|
2014-08-06 10:04:24 +02:00
|
|
|
|
2014-08-03 00:01:14 +02:00
|
|
|
func main() {
|
|
|
|
|
2014-08-06 10:52:37 +02:00
|
|
|
var (
|
2014-08-23 18:05:40 +02:00
|
|
|
port int
|
|
|
|
host string
|
|
|
|
driver string
|
|
|
|
cacheSize int
|
|
|
|
version bool
|
|
|
|
interleaved bool
|
|
|
|
gcDuration string
|
2014-08-06 10:52:37 +02:00
|
|
|
)
|
2014-08-03 00:01:14 +02:00
|
|
|
|
|
|
|
flag.IntVar(&port, "port", 6379, "port to bind")
|
2014-08-03 14:52:24 +02:00
|
|
|
flag.StringVar(&driver, "driver", "leveldb", "type of database (leveldb, sqlite)")
|
2014-08-03 00:01:14 +02:00
|
|
|
flag.StringVar(&host, "host", "", "host to bind")
|
|
|
|
flag.IntVar(&cacheSize, "cache", 32, "cache size in MB")
|
2014-08-06 10:04:24 +02:00
|
|
|
flag.BoolVar(&version, "version", false, "Print version and exit.")
|
2014-08-23 18:05:40 +02:00
|
|
|
flag.BoolVar(&interleaved, "interleaved", false, "Backend stores key in interleaved form.")
|
2014-08-06 10:52:37 +02:00
|
|
|
flag.StringVar(&gcDuration, "gc-duration", GCDuration, "Duration between forced GCs.")
|
2014-08-03 00:01:14 +02:00
|
|
|
flag.Parse()
|
|
|
|
|
2014-08-06 10:04:24 +02:00
|
|
|
if version {
|
|
|
|
fmt.Printf("Version: %s\n", Version)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2014-08-03 00:01:14 +02:00
|
|
|
args := flag.Args()
|
|
|
|
|
|
|
|
if len(args) < 1 {
|
|
|
|
log.Fatal("Missing path to world")
|
|
|
|
}
|
|
|
|
|
2014-08-06 10:52:37 +02:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
backend Backend
|
|
|
|
gcDur time.Duration
|
|
|
|
)
|
|
|
|
|
|
|
|
if gcDur, err = time.ParseDuration(gcDuration); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-08-03 00:01:14 +02:00
|
|
|
|
2014-08-03 15:42:12 +02:00
|
|
|
if driver == "sqlite" {
|
2014-08-23 18:05:40 +02:00
|
|
|
if backend, err = NewSqliteBackend(args[0], interleaved); err != nil {
|
2014-08-03 15:42:12 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
} else {
|
2014-08-22 22:26:03 +02:00
|
|
|
if backend, err = NewLeveDBBackend(
|
|
|
|
args[0],
|
|
|
|
common.IdentityTranscoder,
|
|
|
|
common.IdentityTranscoder,
|
|
|
|
cacheSize); err != nil {
|
2014-08-03 15:42:12 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2014-08-03 11:25:25 +02:00
|
|
|
defer backend.Shutdown()
|
2014-08-03 00:01:14 +02:00
|
|
|
|
|
|
|
var listener net.Listener
|
|
|
|
|
|
|
|
listener, err = net.Listen("tcp", fmt.Sprintf("%s:%d", host, port))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer listener.Close()
|
2014-08-03 08:10:52 +02:00
|
|
|
log.Printf("Server started at %s", listener.Addr())
|
2014-08-03 00:01:14 +02:00
|
|
|
|
|
|
|
connChan := make(chan net.Conn)
|
|
|
|
defer close(connChan)
|
|
|
|
sigChan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigChan, os.Interrupt, os.Kill)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-08-03 08:10:52 +02:00
|
|
|
log.Printf("Client accepted from: %s", conn.RemoteAddr())
|
2014-08-03 00:01:14 +02:00
|
|
|
connChan <- conn
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-08-06 10:52:37 +02:00
|
|
|
log.Printf("Doing garbage collection every: %s", gcDur)
|
|
|
|
gcChan := time.Tick(gcDur)
|
|
|
|
|
2014-08-03 00:01:14 +02:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case conn := <-connChan:
|
2014-08-06 01:11:41 +02:00
|
|
|
var session Session
|
|
|
|
if session, err = backend.NewSession(); err != nil {
|
|
|
|
log.Printf("Cannot create session: %s", err)
|
|
|
|
conn.Close()
|
|
|
|
} else {
|
|
|
|
go NewConnection(conn, session).Run()
|
|
|
|
}
|
2014-08-03 00:01:14 +02:00
|
|
|
case <-sigChan:
|
|
|
|
log.Println("Shutting down")
|
|
|
|
return
|
2014-08-06 10:52:37 +02:00
|
|
|
case <-gcChan:
|
|
|
|
log.Println("Starting garbage collection.")
|
|
|
|
runtime.GC()
|
|
|
|
log.Println("Garbage collection done.")
|
2014-08-03 00:01:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|