diff --git a/data.go b/data.go index 1302f8e..8aa2b3b 100644 --- a/data.go +++ b/data.go @@ -270,8 +270,8 @@ func collectionGarbageCycle() { randomIndex := 0 key := "" var value values - // todo, make this configurable - for _ = range time.Tick(100*time.Millisecond) { + + for _ = range time.Tick(gcInterval*time.Millisecond) { if totalKeys > 0 { randomIndex = random(0, totalKeys) diff --git a/dump.go b/dump.go index 2c66ad3..e56b4f2 100644 --- a/dump.go +++ b/dump.go @@ -33,9 +33,17 @@ func flush() { // will load the data from the disk on start up func iterate() { + + if !persistence { + fmt.Println("Sever started without persistence") + fmt.Println("we're ready to go!") + return + } + fmt.Println("Server is loading your data") fmt.Println("Please be patient") iter := disk.NewIterator(nil, nil) + for iter.Next() { k := iter.Key() v := iter.Value() @@ -53,6 +61,12 @@ func iterate() { // puts data on the batch func diskPut(key string, value interface{}) { + + // If persistence is set to false, don't take the pain + if !persistence { + return + } + err, bytes := GetBytes(value) if !err { batch.Put([]byte(key), bytes) @@ -62,6 +76,11 @@ func diskPut(key string, value interface{}) { //delete from batch func diskDel(key string) { + // If persistence is set to false, don't take the pain + if !persistence { + return + } + batch.Delete([]byte(key)) written = false } @@ -99,7 +118,7 @@ func decodeBytes(value []byte) (bool, values) { // will flush to disk every 5 minutes(configurable value) func flushingActivity() { - for _ = range time.Tick(300000*time.Millisecond) { + for _ = range time.Tick(diskWriteInterval*time.Millisecond) { flush() written = true } diff --git a/main.go b/main.go index 2f0e185..3f3d6c2 100644 --- a/main.go +++ b/main.go @@ -13,19 +13,12 @@ import ( ) var upgrader = websocket.Upgrader{} -var port = 2700 // where all the magic begins func main() { - // initialize disk storage - initializeDiskStorage() - - // initialize the basic structure, load the persistence values etc - initializeStore() - - // intialize random garbage collection - go collectionGarbageCycle() + // read the config + readConfig() http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { var conn, _ = upgrader.Upgrade(w, r, nil) @@ -33,7 +26,7 @@ func main() { }) fmt.Println("Server started on port", port) - http.ListenAndServe(":2700", nil) + http.ListenAndServe(port, nil) } // handles the client connections diff --git a/mumbo-conf.json b/mumbo-conf.json new file mode 100644 index 0000000..eff887d --- /dev/null +++ b/mumbo-conf.json @@ -0,0 +1,7 @@ +{ + "gcInterval" : 100, + "persistence" : true, + "diskWriteInterval" : 300000, + "port" : 2700 + +} \ No newline at end of file diff --git a/setup.go b/setup.go new file mode 100644 index 0000000..ad93bf5 --- /dev/null +++ b/setup.go @@ -0,0 +1,85 @@ +package main + +import ( + "encoding/json" + "io/ioutil" + "strconv" + "time" +) + +var Config map[string]interface{} +var persistence bool +var port string +var gcInterval time.Duration +var diskWriteInterval time.Duration + +// read the config file, if there's any +func readConfig() { + Config = make(map[string]interface{}) + file, e := ioutil.ReadFile("/etc/mumbo-conf.json") + + if e != nil { + extendConfig(Config) + } else { + json.Unmarshal(file, &Config) + + // extend config with defaults + extendConfig(Config) + } + + // initialize the system + initializer() +} + +// will initialize the system with/ without persistence +func initializer() { + + if persistence { + // initialize disk storage + initializeDiskStorage() + } + + + // initialize the basic structure, load the persistence values etc + initializeStore() + + // intialize random garbage collection + go collectionGarbageCycle() +} + +// will extend the config will defaults , if props not specified +func extendConfig(conf map[string]interface{}) { + _, ok := conf["persistence"] + + if ok { + persistence = Config["persistence"].(bool) + } else { + persistence = false + } + + _, ok = conf["port"] + + if ok { + Fport := Config["port"].(float64) + Iport := int(Fport) + port = ":" + strconv.Itoa(int(Iport)) + } else { + port = ":" + "2700" + } + + _, ok = conf["gcInterval"] + + if ok { + gcInterval = time.Duration(int(Config["gcInterval"].(float64))) + } else { + gcInterval = 100 + } + + _, ok = conf["diskWriteInterval"] + + if ok { + diskWriteInterval = time.Duration(int(Config["gcInterval"].(float64))) + } else { + diskWriteInterval = 100 + } +} \ No newline at end of file