Merge pull request #113 from appleboy/BuntDB

Support BuntDB engine.
This commit is contained in:
Bo-Yi Wu 2016-08-03 01:25:04 -05:00 committed by GitHub
commit 1d29dadf0a
9 changed files with 233 additions and 11 deletions

View File

@ -43,6 +43,9 @@ boltdb_test: init
memory_test: init
go test -v -cover ./storage/memory/...
buntdb_test: init
go test -v -cover ./storage/buntdb/...
config_test: init
go test -v -cover ./config/...

View File

@ -45,7 +45,7 @@ A push notification micro server using [Gin](https://github.com/gin-gonic/gin) f
* Support notification queue and multiple workers.
* Support `/api/stat/app` show notification success and failure counts.
* Support `/api/config` show your [YAML](https://en.wikipedia.org/wiki/YAML) config.
* Support store app stat to memory, [Redis](http://redis.io/) or [BoltDB](https://github.com/boltdb/bolt).
* Support store app stat to memory, [Redis](http://redis.io/), [BoltDB](https://github.com/boltdb/bolt) or [BuntDB](https://github.com/tidwall/buntdb).
* Support `p12` or `pem` formtat of iOS certificate file.
* Support `/sys/stats` show response time, status code count, etc.
* Support for HTTP proxy to Google server (GCM).
@ -98,6 +98,8 @@ stat:
boltdb:
path: "gorush.db"
bucket: "gorush"
buntdb:
path: "gorush.db"
```
## Basic Usage

View File

@ -67,6 +67,7 @@ type SectionStat struct {
Engine string `yaml:"engine"`
Redis SectionRedis `yaml:"redis"`
BoltDB SectionBoltDB `yaml:"boltdb"`
BuntDB SectionBuntDB `yaml:"buntdb"`
}
// SectionRedis is sub seciont of config.
@ -82,6 +83,11 @@ type SectionBoltDB struct {
Bucket string `yaml:"bucket"`
}
// SectionBuntDB is sub seciont of config.
type SectionBuntDB struct {
Path string `yaml:"path"`
}
// BuildDefaultPushConf is default config setting.
func BuildDefaultPushConf() ConfYaml {
var conf ConfYaml
@ -130,6 +136,8 @@ func BuildDefaultPushConf() ConfYaml {
conf.Stat.BoltDB.Path = "gorush.db"
conf.Stat.BoltDB.Bucket = "gorush"
conf.Stat.BuntDB.Path = "gorush.db"
return conf
}

View File

@ -43,3 +43,5 @@ stat:
boltdb:
path: "gorush.db"
bucket: "gorush"
buntdb:
path: "gorush.db"

View File

@ -96,6 +96,8 @@ func (suite *ConfigTestSuite) TestValidateConfDefault() {
assert.Equal(suite.T(), "gorush.db", suite.ConfGorushDefault.Stat.BoltDB.Path)
assert.Equal(suite.T(), "gorush", suite.ConfGorushDefault.Stat.BoltDB.Bucket)
assert.Equal(suite.T(), "gorush.db", suite.ConfGorushDefault.Stat.BuntDB.Path)
}
func (suite *ConfigTestSuite) TestValidateConf() {
@ -142,6 +144,8 @@ func (suite *ConfigTestSuite) TestValidateConf() {
assert.Equal(suite.T(), "gorush.db", suite.ConfGorush.Stat.BoltDB.Path)
assert.Equal(suite.T(), "gorush", suite.ConfGorush.Stat.BoltDB.Bucket)
assert.Equal(suite.T(), "gorush.db", suite.ConfGorush.Stat.BuntDB.Path)
}
func TestConfigTestSuite(t *testing.T) {

39
glide.lock generated
View File

@ -1,14 +1,6 @@
hash: d182f8dc3162313af09e706a0ba4901218d9ccde609ee7d39c6aae5e891f20ee
updated: 2016-07-19T21:31:48.923940945+08:00
hash: a723ec6556a185c4a20185378b0e042fd41453232da81489e06307b0ac8feba7
updated: 2016-08-02T15:01:02.331147237+08:00
imports:
- name: github.com/appleboy/gorush
version: af78db59b4675cf932aaa71fd45443ac9283bce7
subpackages:
- config
- gorush
- storage/boltdb
- storage/memory
- storage/redis
- name: github.com/asdine/storm
version: 00b2f2df7ab7af9db746b826649395628cb5374e
subpackages:
@ -55,6 +47,33 @@ imports:
- require
- name: github.com/thoas/stats
version: 69e3c072eec2df2df41afe6214f62eb940e4cd80
- name: github.com/tidwall/btree
version: 7ebb98011b36ffcdcf7a5d8062d6e6c651a49560
- name: github.com/tidwall/buntdb
version: 912f340175491888fdd33687d7a309921b6c1e49
- name: github.com/tidwall/rtree
version: 1737a7bdeb7e5f8303905c5b385f1eeb0f7272ab
subpackages:
- dims/d1
- dims/d10
- dims/d11
- dims/d12
- dims/d13
- dims/d14
- dims/d15
- dims/d16
- dims/d17
- dims/d18
- dims/d19
- dims/d2
- dims/d20
- dims/d3
- dims/d4
- dims/d5
- dims/d6
- dims/d7
- dims/d8
- dims/d9
- name: golang.org/x/crypto
version: c2f4947f41766b144bb09066e919466da5eddeae
subpackages:

View File

@ -18,3 +18,4 @@ import:
version: v1.0.1
- package: github.com/buger/jsonparser
- package: github.com/thoas/stats
- package: github.com/tidwall/buntdb

134
storage/buntdb/buntdb.go Normal file
View File

@ -0,0 +1,134 @@
package buntdb
import (
"fmt"
"github.com/appleboy/gorush/config"
"github.com/tidwall/buntdb"
"strconv"
)
// Stat variable for redis
const (
TotalCountKey = "gorush-total-count"
IosSuccessKey = "gorush-ios-success-count"
IosErrorKey = "gorush-ios-error-count"
AndroidSuccessKey = "gorush-android-success-count"
AndroidErrorKey = "gorush-android-error-count"
)
// New func implements the storage interface for gorush (https://github.com/appleboy/gorush)
func New(config config.ConfYaml) *Storage {
return &Storage{
config: config,
}
}
// Storage is interface structure
type Storage struct {
config config.ConfYaml
}
// Init client storage.
func (s *Storage) Init() error {
return nil
}
// Reset Client storage.
func (s *Storage) Reset() {
s.setBuntDB(TotalCountKey, 0)
s.setBuntDB(IosSuccessKey, 0)
s.setBuntDB(IosErrorKey, 0)
s.setBuntDB(AndroidSuccessKey, 0)
s.setBuntDB(AndroidErrorKey, 0)
}
func (s *Storage) setBuntDB(key string, count int64) {
db, _ := buntdb.Open(s.config.Stat.BuntDB.Path)
db.Update(func(tx *buntdb.Tx) error {
tx.Set(key, fmt.Sprintf("%d", count), nil)
return nil
})
defer db.Close()
}
func (s *Storage) getBuntDB(key string, count *int64) {
db, _ := buntdb.Open(s.config.Stat.BuntDB.Path)
db.View(func(tx *buntdb.Tx) error {
val, _ := tx.Get(key)
*count, _ = strconv.ParseInt(val, 10, 64)
return nil
})
defer db.Close()
}
// AddTotalCount record push notification count.
func (s *Storage) AddTotalCount(count int64) {
total := s.GetTotalCount() + count
s.setBuntDB(TotalCountKey, total)
}
// AddIosSuccess record counts of success iOS push notification.
func (s *Storage) AddIosSuccess(count int64) {
total := s.GetIosSuccess() + count
s.setBuntDB(IosSuccessKey, total)
}
// AddIosError record counts of error iOS push notification.
func (s *Storage) AddIosError(count int64) {
total := s.GetIosError() + count
s.setBuntDB(IosErrorKey, total)
}
// AddAndroidSuccess record counts of success Android push notification.
func (s *Storage) AddAndroidSuccess(count int64) {
total := s.GetAndroidSuccess() + count
s.setBuntDB(AndroidSuccessKey, total)
}
// AddAndroidError record counts of error Android push notification.
func (s *Storage) AddAndroidError(count int64) {
total := s.GetAndroidError() + count
s.setBuntDB(AndroidErrorKey, total)
}
// GetTotalCount show counts of all notification.
func (s *Storage) GetTotalCount() int64 {
var count int64
s.getBuntDB(TotalCountKey, &count)
return count
}
// GetIosSuccess show success counts of iOS notification.
func (s *Storage) GetIosSuccess() int64 {
var count int64
s.getBuntDB(IosSuccessKey, &count)
return count
}
// GetIosError show error counts of iOS notification.
func (s *Storage) GetIosError() int64 {
var count int64
s.getBuntDB(IosErrorKey, &count)
return count
}
// GetAndroidSuccess show success counts of Android notification.
func (s *Storage) GetAndroidSuccess() int64 {
var count int64
s.getBuntDB(AndroidSuccessKey, &count)
return count
}
// GetAndroidError show error counts of Android notification.
func (s *Storage) GetAndroidError() int64 {
var count int64
s.getBuntDB(AndroidErrorKey, &count)
return count
}

View File

@ -0,0 +1,49 @@
package buntdb
import (
c "github.com/appleboy/gorush/config"
"github.com/stretchr/testify/assert"
"os"
"testing"
)
func TestBuntDBEngine(t *testing.T) {
var val int64
config := c.BuildDefaultPushConf()
if _, err := os.Stat(config.Stat.BuntDB.Path); os.IsNotExist(err) {
os.RemoveAll(config.Stat.BuntDB.Path)
}
buntDB := New(config)
buntDB.Init()
buntDB.Reset()
buntDB.AddTotalCount(10)
val = buntDB.GetTotalCount()
assert.Equal(t, int64(10), val)
buntDB.AddTotalCount(10)
val = buntDB.GetTotalCount()
assert.Equal(t, int64(20), val)
buntDB.AddIosSuccess(20)
val = buntDB.GetIosSuccess()
assert.Equal(t, int64(20), val)
buntDB.AddIosError(30)
val = buntDB.GetIosError()
assert.Equal(t, int64(30), val)
buntDB.AddAndroidSuccess(40)
val = buntDB.GetAndroidSuccess()
assert.Equal(t, int64(40), val)
buntDB.AddAndroidError(50)
val = buntDB.GetAndroidError()
assert.Equal(t, int64(50), val)
buntDB.Reset()
val = buntDB.GetAndroidError()
assert.Equal(t, int64(0), val)
}