Skip to content

Commit

Permalink
Feature #58 support BoltDB engine
Browse files Browse the repository at this point in the history
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed Apr 23, 2016
1 parent f14d42d commit f5857f5
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ coverage.out
gorush/log/*.log
build.tar.gz
gorush.tar.gz
gorush.db
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@ log:
error_level: "error"

stat:
engine: "memory" # support memory or redis
engine: "memory" # support memory, redis or boltdb
redis:
addr: "localhost:6379"
password: ""
db: 0
boltdb:
path: "gorush.db"
bucket: "gorush"
```
## Basic Usage
Expand Down
3 changes: 3 additions & 0 deletions config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ stat:
addr: "localhost:6379"
password: ""
db: 0
boltdb:
path: "gorush.db"
bucket: "gorush"
10 changes: 10 additions & 0 deletions gorush/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type SectionLog struct {
type SectionStat struct {
Engine string `yaml:"service"`
Redis SectionRedis `yaml:"redis"`
BoltDB SectionBoltDB `yaml:"boltdb"`
}

// SectionRedis is sub seciont of config.
Expand All @@ -72,6 +73,12 @@ type SectionRedis struct {
DB int64 `yaml:"db"`
}

// SectionBoltDB is sub seciont of config.
type SectionBoltDB struct {
Path string `yaml:"path"`
Bucket string `yaml:"bucket"`
}

// BuildDefaultPushConf is default config setting.
func BuildDefaultPushConf() ConfYaml {
var conf ConfYaml
Expand Down Expand Up @@ -114,6 +121,9 @@ func BuildDefaultPushConf() ConfYaml {
conf.Stat.Redis.Password = ""
conf.Stat.Redis.DB = 0

conf.Stat.BoltDB.Path = "gorush.db"
conf.Stat.BoltDB.Bucket = "gorush"

return conf
}

Expand Down
52 changes: 47 additions & 5 deletions gorush/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"strconv"
"sync/atomic"
"github.com/asdine/storm"
)

// StatusApp is app status structure
Expand Down Expand Up @@ -69,25 +70,49 @@ func initRedis() error {
return nil
}

func initBoltDB() {
RushStatus.TotalCount = getTotalCount()
RushStatus.Ios.PushSuccess = getIosSuccess()
RushStatus.Ios.PushError = getIosError()
RushStatus.Android.PushSuccess = getAndroidSuccess()
RushStatus.Android.PushError = getAndroidError()
}

// InitAppStatus for initialize app status
func InitAppStatus() {
switch PushConf.Stat.Engine {
case "memory":
initApp()
case "redis":
initRedis()
case "boltdb":
initBoltDB()
default:
initApp()
}

}

func boltdbSet(key string, count int64) {
db, _ := storm.Open(PushConf.Stat.BoltDB.Path)
db.Set(PushConf.Stat.BoltDB.Bucket, key, count)
defer db.Close()
}

func boltdbGet(key string, count *int64) {
db, _ := storm.Open(PushConf.Stat.BoltDB.Path)
db.Get(PushConf.Stat.BoltDB.Bucket, key, count)
defer db.Close()
}

func addTotalCount(count int64) {
switch PushConf.Stat.Engine {
case "memory":
atomic.AddInt64(&RushStatus.TotalCount, count)
case "redis":
RedisClient.Set(gorushTotalCount, strconv.Itoa(int(count)), 0)
case "boltdb":
boltdbSet(gorushTotalCount, count)
default:
atomic.AddInt64(&RushStatus.TotalCount, count)
}
Expand All @@ -99,6 +124,8 @@ func addIosSuccess(count int64) {
atomic.AddInt64(&RushStatus.Ios.PushSuccess, count)
case "redis":
RedisClient.Set(gorushIosSuccess, strconv.Itoa(int(count)), 0)
case "boltdb":
boltdbSet(gorushIosSuccess, count)
default:
atomic.AddInt64(&RushStatus.Ios.PushSuccess, count)
}
Expand All @@ -110,6 +137,8 @@ func addIosError(count int64) {
atomic.AddInt64(&RushStatus.Ios.PushError, count)
case "redis":
RedisClient.Set(gorushIosError, strconv.Itoa(int(count)), 0)
case "boltdb":
boltdbSet(gorushIosError, count)
default:
atomic.AddInt64(&RushStatus.Ios.PushError, count)
}
Expand All @@ -120,8 +149,9 @@ func addAndroidSuccess(count int64) {
case "memory":
atomic.AddInt64(&RushStatus.Android.PushSuccess, count)
case "redis":

RedisClient.Set(gorushAndroidSuccess, strconv.Itoa(int(count)), 0)
case "boltdb":
boltdbSet(gorushAndroidSuccess, count)
default:
atomic.AddInt64(&RushStatus.Android.PushSuccess, count)
}
Expand All @@ -133,6 +163,8 @@ func addAndroidError(count int64) {
atomic.AddInt64(&RushStatus.Android.PushError, count)
case "redis":
RedisClient.Set(gorushAndroidError, strconv.Itoa(int(count)), 0)
case "boltdb":
boltdbSet(gorushAndroidError, count)
default:
atomic.AddInt64(&RushStatus.Android.PushError, count)
}
Expand All @@ -144,7 +176,9 @@ func getTotalCount() int64 {
case "memory":
count = atomic.LoadInt64(&RushStatus.TotalCount)
case "redis":
count = getRedisInt64Result(gorushAndroidError)
count = getRedisInt64Result(gorushTotalCount)
case "boltdb":
boltdbGet(gorushTotalCount, &count)
default:
count = atomic.LoadInt64(&RushStatus.TotalCount)
}
Expand All @@ -158,7 +192,9 @@ func getIosSuccess() int64 {
case "memory":
count = atomic.LoadInt64(&RushStatus.Ios.PushSuccess)
case "redis":
count = getRedisInt64Result(gorushAndroidError)
count = getRedisInt64Result(gorushIosSuccess)
case "boltdb":
boltdbGet(gorushIosSuccess, &count)
default:
count = atomic.LoadInt64(&RushStatus.Ios.PushSuccess)
}
Expand All @@ -172,7 +208,9 @@ func getIosError() int64 {
case "memory":
count = atomic.LoadInt64(&RushStatus.Ios.PushError)
case "redis":
count = getRedisInt64Result(gorushAndroidError)
count = getRedisInt64Result(gorushIosError)
case "boltdb":
boltdbGet(gorushIosError, &count)
default:
count = atomic.LoadInt64(&RushStatus.Ios.PushError)
}
Expand All @@ -186,7 +224,9 @@ func getAndroidSuccess() int64 {
case "memory":
count = atomic.LoadInt64(&RushStatus.Android.PushSuccess)
case "redis":
count = getRedisInt64Result(gorushAndroidError)
count = getRedisInt64Result(gorushAndroidSuccess)
case "boltdb":
boltdbGet(gorushAndroidSuccess, &count)
default:
count = atomic.LoadInt64(&RushStatus.Android.PushSuccess)
}
Expand All @@ -201,6 +241,8 @@ func getAndroidError() int64 {
count = atomic.LoadInt64(&RushStatus.Android.PushError)
case "redis":
count = getRedisInt64Result(gorushAndroidError)
case "boltdb":
boltdbGet(gorushAndroidError, &count)
default:
count = atomic.LoadInt64(&RushStatus.Android.PushError)
}
Expand Down
63 changes: 43 additions & 20 deletions gorush/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,43 +73,66 @@ func TestStatForRedisEngine(t *testing.T) {
PushConf.Stat.Redis.Addr = "localhost:6379"
InitAppStatus()

addTotalCount(1000)
addIosSuccess(1000)
addIosError(1000)
addAndroidSuccess(1000)
addAndroidError(1000)
addTotalCount(10)
addIosSuccess(20)
addIosError(30)
addAndroidSuccess(40)
addAndroidError(50)

val = getTotalCount()
assert.Equal(t, int64(1000), val)
assert.Equal(t, int64(10), val)
val = getIosSuccess()
assert.Equal(t, int64(1000), val)
assert.Equal(t, int64(20), val)
val = getIosError()
assert.Equal(t, int64(1000), val)
assert.Equal(t, int64(30), val)
val = getAndroidSuccess()
assert.Equal(t, int64(1000), val)
assert.Equal(t, int64(40), val)
val = getAndroidError()
assert.Equal(t, int64(1000), val)
assert.Equal(t, int64(50), val)
}

func TestDefaultEngine(t *testing.T) {
var val int64
PushConf.Stat.Engine = "test"
InitAppStatus()

addTotalCount(1000)
addIosSuccess(1000)
addIosError(1000)
addAndroidSuccess(1000)
addAndroidError(1000)
addTotalCount(1)
addIosSuccess(2)
addIosError(3)
addAndroidSuccess(4)
addAndroidError(5)

val = getTotalCount()
assert.Equal(t, int64(1000), val)
assert.Equal(t, int64(1), val)
val = getIosSuccess()
assert.Equal(t, int64(1000), val)
assert.Equal(t, int64(2), val)
val = getIosError()
assert.Equal(t, int64(1000), val)
assert.Equal(t, int64(3), val)
val = getAndroidSuccess()
assert.Equal(t, int64(1000), val)
assert.Equal(t, int64(4), val)
val = getAndroidError()
assert.Equal(t, int64(1000), val)
assert.Equal(t, int64(5), val)
}

func TestStatForBoltDBEngine(t *testing.T) {
var val int64
PushConf.Stat.Engine = "boltdb"
InitAppStatus()

addTotalCount(100)
addIosSuccess(200)
addIosError(300)
addAndroidSuccess(400)
addAndroidError(500)

val = getTotalCount()
assert.Equal(t, int64(100), val)
val = getIosSuccess()
assert.Equal(t, int64(200), val)
val = getIosError()
assert.Equal(t, int64(300), val)
val = getAndroidSuccess()
assert.Equal(t, int64(400), val)
val = getAndroidError()
assert.Equal(t, int64(500), val)
}

0 comments on commit f5857f5

Please sign in to comment.