Skip to content

Commit

Permalink
Honouring a config file
Browse files Browse the repository at this point in the history
  • Loading branch information
GavinDmello committed Feb 26, 2017
1 parent 734416c commit 0f90e04
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 13 deletions.
4 changes: 2 additions & 2 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
21 changes: 20 additions & 1 deletion dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
13 changes: 3 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,20 @@ 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)
go handleConnection(conn)
})

fmt.Println("Server started on port", port)
http.ListenAndServe(":2700", nil)
http.ListenAndServe(port, nil)
}

// handles the client connections
Expand Down
7 changes: 7 additions & 0 deletions mumbo-conf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"gcInterval" : 100,
"persistence" : true,
"diskWriteInterval" : 300000,
"port" : 2700

}
85 changes: 85 additions & 0 deletions setup.go
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 0f90e04

Please sign in to comment.