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,47 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["service_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/infra/databus/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = ["service.go"],
importpath = "go-common/app/infra/databus/service",
tags = ["automanaged"],
deps = [
"//app/infra/databus/conf:go_default_library",
"//app/infra/databus/dao:go_default_library",
"//app/infra/databus/model:go_default_library",
"//library/log:go_default_library",
"//library/stat/prom: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"],
)

View File

@@ -0,0 +1,101 @@
package service
import (
"context"
"time"
"go-common/app/infra/databus/conf"
"go-common/app/infra/databus/dao"
"go-common/app/infra/databus/model"
"go-common/library/log"
"go-common/library/stat/prom"
)
const (
_authUpdateInterval = 1 * time.Minute
)
// Service service instance
type Service struct {
dao *dao.Dao
// auth
auths map[string]*model.Auth
// the auth of cluster changed
clusterChan chan model.Auth
// stats prom
StatProm *prom.Prom
CountProm *prom.Prom
TimeProm *prom.Prom
}
// New new and return service
func New(c *conf.Config) (s *Service) {
s = &Service{
dao: dao.New(c),
// cluster
clusterChan: make(chan model.Auth, 5),
// stats prom
StatProm: prom.New().WithState("go_databus_state", []string{"role", "group", "topic", "partition"}),
// count prom: count consumer and producer partition speed
CountProm: prom.New().WithState("go_databus_counter", []string{"operation", "group", "topic"}),
TimeProm: prom.New().WithTimer("go_databus_timer", []string{"group"}),
}
s.fillAuth()
go s.proc()
return
}
// Ping check mysql connection
func (s *Service) Ping(c context.Context) error {
return s.dao.Ping(c)
}
// Close close mysql connection
func (s *Service) Close() {
if s.dao != nil {
s.dao.Close()
}
}
func (s *Service) proc() {
for {
s.fillAuth()
time.Sleep(_authUpdateInterval)
}
}
func (s *Service) fillAuth() (err error) {
auths, err := s.dao.Auth(context.Background())
if err != nil {
log.Error("service.fillAuth error(%v)", err)
return
}
var changed []*model.Auth
// check cluster change event
for group, nw := range auths {
old, ok := s.auths[group]
if !ok {
continue
}
if old.Cluster != nw.Cluster {
changed = append(changed, old)
log.Info("cluster changed group(%s) topic(%s) oldCluster(%s) newCluster(%s)", old.Group, old.Topic, old.Cluster, nw.Cluster)
}
}
s.auths = auths
for _, ch := range changed {
s.clusterChan <- *ch
}
return
}
// AuthApp check auth from cache
func (s *Service) AuthApp(group string) (a *model.Auth, ok bool) {
a, ok = s.auths[group]
return
}
// ClusterEvent return cluster change event
func (s *Service) ClusterEvent() (group <-chan model.Auth) {
return s.clusterChan
}

View File

@@ -0,0 +1,31 @@
package service
import (
"flag"
"path/filepath"
"testing"
"go-common/app/infra/databus/conf"
"github.com/smartystreets/goconvey/convey"
)
var (
s *Service
)
func init() {
dir, _ := filepath.Abs("../cmd/databus-test.toml")
flag.Set("conf", dir)
conf.Init()
s = New(conf.Conf)
}
func TestArchive(t *testing.T) {
convey.Convey("Archive", t, func() {
a, ok := s.AuthApp("databus_test_group")
convey.So(ok, convey.ShouldBeTrue)
convey.So(a, convey.ShouldNotBeNil)
convey.Printf("%+v", a)
})
}