Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"aso_bin_log.go",
"service.go",
],
importpath = "go-common/app/job/main/passport-game-local/service",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/passport-game-local/conf:go_default_library",
"//app/job/main/passport-game-local/model:go_default_library",
"//library/log:go_default_library",
"//library/queue/databus:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
go_test(
name = "go_default_test",
srcs = [
"aso_bin_log_test.go",
"service_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/passport-game-local/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,140 @@
package service
import (
"context"
"encoding/json"
"strconv"
"time"
"go-common/app/job/main/passport-game-local/model"
"go-common/library/log"
"go-common/library/queue/databus"
)
func (s *Service) asobinlogconsumeproc() {
mergeNum := int64(s.c.Group.AsoBinLog.Num)
for {
msg, ok := <-s.dsAsoBinLogSub.Messages()
if !ok {
log.Error("asobinlogconsumeproc closed")
return
}
// marked head to first commit
m := &message{data: msg}
s.mu.Lock()
if s.head == nil {
s.head = m
s.last = m
} else {
s.last.next = m
s.last = m
}
s.mu.Unlock()
bmsg := new(model.BMsg)
if err := json.Unmarshal(msg.Value, bmsg); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(msg.Value), err)
continue
}
mid := int64(0)
if bmsg.Table == _asoAccountTable {
t := new(model.AsoAccount)
if err := json.Unmarshal(bmsg.New, t); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(bmsg.New), err)
}
mid = t.Mid
m.object = bmsg
log.Info("asobinlogconsumeproc table:%s key:%s partition:%d offset:%d", bmsg.Table, msg.Key, msg.Partition, msg.Offset)
} else {
continue
}
s.merges[mid%mergeNum] <- m
}
}
func (s *Service) asobinlogcommitproc() {
for {
done := <-s.done
commits := make(map[int32]*databus.Message)
for _, d := range done {
d.done = true
}
s.mu.Lock()
for ; s.head != nil && s.head.done; s.head = s.head.next {
commits[s.head.data.Partition] = s.head.data
}
s.mu.Unlock()
for _, m := range commits {
m.Commit()
}
}
}
func (s *Service) asobinlogmergeproc(c chan *message) {
var (
max = s.c.Group.AsoBinLog.Size
merges = make([]*model.BMsg, 0, max)
marked = make([]*message, 0, max)
ticker = time.NewTicker(time.Duration(s.c.Group.AsoBinLog.Ticker))
)
for {
select {
case msg, ok := <-c:
if !ok {
log.Error("asobinlogmergeproc closed")
return
}
p, assertOk := msg.object.(*model.BMsg)
if assertOk && p.Action != "" && (p.Table == _asoAccountTable) {
merges = append(merges, p)
}
marked = append(marked, msg)
if len(marked) < max && len(merges) < max {
continue
}
case <-ticker.C:
}
if len(merges) > 0 {
s.processAsoAccLogInfo(merges)
merges = make([]*model.BMsg, 0, max)
}
if len(marked) > 0 {
s.done <- marked
marked = make([]*message, 0, max)
}
}
}
func (s *Service) processAsoAccLogInfo(bmsgs []*model.BMsg) {
for _, msg := range bmsgs {
s.processAsoAccLog(msg)
}
}
func (s *Service) processAsoAccLog(msg *model.BMsg) {
aso := new(model.OriginAsoAccount)
if err := json.Unmarshal(msg.New, aso); err != nil {
log.Error("failed to parse binlog new, json.Unmarshal(%s) error(%v)", string(msg.New), err)
return
}
pmsg := new(model.PMsg)
if "update" == msg.Action {
old := new(model.AsoAccount)
if err := json.Unmarshal(msg.Old, old); err != nil {
log.Error("failed to parse binlog new, json.Unmarshal(%s) error(%v)", string(msg.New), err)
return
}
if old.Pwd != aso.Pwd {
pmsg.Flag = 1
}
}
pmsg.Action = msg.Action
pmsg.Table = msg.Table
pmsg.Data = model.Default(aso)
key := strconv.FormatInt(aso.Mid, 10)
for {
if err := s.dsAsoEncryptTransPub.Send(context.TODO(), key, pmsg); err == nil {
return
}
time.Sleep(time.Second)
}
}

View File

@@ -0,0 +1,14 @@
package service
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestService_Asobinlogconsumeproc(t *testing.T) {
once.Do(startService)
Convey("asobinlogcommitproc running", t, func() {
So(s.asobinlogcommitproc, ShouldBeNil)
})
}

View File

@@ -0,0 +1,71 @@
package service
import (
"context"
"sync"
"go-common/app/job/main/passport-game-local/conf"
"go-common/library/log"
"go-common/library/queue/databus"
)
const (
// table and duration
_asoAccountTable = "aso_account"
)
// Service service.
type Service struct {
c *conf.Config
// aso encrypt trans pub databus
dsAsoEncryptTransPub *databus.Databus
// aso binlog databus
dsAsoBinLogSub *databus.Databus
merges []chan *message
done chan []*message
// proc
head, last *message
mu sync.Mutex
}
type message struct {
next *message
data *databus.Message
object interface{}
done bool
}
// New new a service instance.
func New(c *conf.Config) (s *Service) {
s = &Service{
c: c,
dsAsoEncryptTransPub: databus.New(c.DataBus.EncryptTransPub),
dsAsoBinLogSub: databus.New(c.DataBus.AsoBinLogSub),
merges: make([]chan *message, c.Group.AsoBinLog.Num),
done: make(chan []*message, c.Group.AsoBinLog.Chan),
}
go s.asobinlogcommitproc()
for i := 0; i < c.Group.AsoBinLog.Num; i++ {
ch := make(chan *message, c.Group.AsoBinLog.Chan)
s.merges[i] = ch
go s.asobinlogmergeproc(ch)
}
go s.asobinlogconsumeproc()
return
}
// Ping check server ok.
func (s *Service) Ping(c context.Context) (err error) {
return
}
// Close close service, including databus and outer service.
func (s *Service) Close() (err error) {
if err = s.dsAsoBinLogSub.Close(); err != nil {
log.Error("srv.asoBinLog.Close() error(%v)", err)
}
if err = s.dsAsoEncryptTransPub.Close(); err != nil {
log.Error("srv.dsAsoEncryptTransPub.Close() error(%v)", err)
}
return
}

View File

@@ -0,0 +1,29 @@
package service
import (
"sync"
"testing"
"go-common/app/job/main/passport-game-local/conf"
. "github.com/smartystreets/goconvey/convey"
)
var (
once sync.Once
s *Service
)
func startService() {
if err := conf.Init(); err != nil {
panic(err)
}
s = New(conf.Conf)
}
func TestNew(t *testing.T) {
once.Do(startService)
Convey("ping should ok", t, func() {
So(s.Ping, ShouldBeNil)
})
}