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,54 @@
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/notify/conf:go_default_library",
"//app/infra/notify/model:go_default_library",
"//library/ecode:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"pub.go",
"service.go",
],
importpath = "go-common/app/infra/notify/service",
tags = ["automanaged"],
deps = [
"//app/infra/notify/conf:go_default_library",
"//app/infra/notify/dao:go_default_library",
"//app/infra/notify/model:go_default_library",
"//app/infra/notify/notify:go_default_library",
"//library/conf/env:go_default_library",
"//library/ecode:go_default_library",
"//library/log: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,36 @@
package service
import (
"context"
"go-common/app/infra/notify/model"
"go-common/app/infra/notify/notify"
"go-common/library/ecode"
)
// Pub pub message.
func (s *Service) Pub(c context.Context, arg *model.ArgPub) (err error) {
pc, ok := s.pubConfs[key(arg.Group, arg.Topic)]
if !ok {
err = ecode.AccessDenied
return
}
s.plock.RLock()
pub, ok := s.pubs[key(arg.Group, arg.Topic)]
s.plock.RUnlock()
if !ok {
pub, err = notify.NewPub(pc, s.c)
if err != nil {
return
}
s.plock.Lock()
s.pubs[key(arg.Group, arg.Topic)] = pub
s.plock.Unlock()
}
if !pub.Auth(arg.AppSecret) {
err = ecode.AccessDenied
return
}
err = pub.Send([]byte(arg.Key), []byte(arg.Msg))
return
}

View File

@@ -0,0 +1,142 @@
package service
import (
"context"
"fmt"
"sync"
"time"
"go-common/app/infra/notify/conf"
"go-common/app/infra/notify/dao"
"go-common/app/infra/notify/model"
"go-common/app/infra/notify/notify"
"go-common/library/conf/env"
"go-common/library/log"
)
// Service struct
type Service struct {
c *conf.Config
dao *dao.Dao
plock sync.RWMutex
pubConfs map[string]*model.Pub
subs map[string]*notify.Sub
pubs map[string]*notify.Pub
}
// New init
func New(c *conf.Config) (s *Service) {
s = &Service{
c: c,
dao: dao.New(c),
pubConfs: make(map[string]*model.Pub),
subs: make(map[string]*notify.Sub),
pubs: make(map[string]*notify.Pub),
}
err := s.loadNotify()
if err != nil {
return
}
go s.notifyproc()
go s.loadPub()
go s.retryproc()
return s
}
func (s *Service) loadPub() {
for {
pubs, err := s.dao.LoadPub(context.TODO())
if err != nil {
log.Error("load pub info err %v", err)
time.Sleep(time.Minute)
continue
}
ps := make(map[string]*model.Pub, len(pubs))
for _, p := range pubs {
ps[key(p.Group, p.Topic)] = p
}
s.pubConfs = ps
time.Sleep(time.Minute)
}
}
// TODO():auto reload and update.
func (s *Service) loadNotify() (err error) {
watcher, err := s.dao.LoadNotify(context.TODO(), env.Zone)
if err != nil {
log.Error("load notify err %v", err)
return
}
subs := make(map[string]*notify.Sub, len(watcher))
for _, w := range watcher {
if sub, ok := s.subs[key(w.Group, w.Topic)]; ok && !sub.Closed() && !sub.IsUpdate(w) {
subs[key(w.Group, w.Topic)] = sub
} else {
n, err := s.newSub(w)
if err != nil {
log.Error("create notify topic(%s) group(%s) err(%v)", w.Topic, w.Group, err)
continue
}
subs[key(w.Group, w.Topic)] = n
log.Info("new sub %s %s", w.Group, w.Topic)
}
}
// close subs not subscribe any more.
for k, sub := range s.subs {
if _, ok := subs[k]; !ok {
sub.Close()
log.Info("close sub not subscribe any %s", k)
}
}
s.subs = subs
return
}
func (s *Service) newSub(w *model.Watcher) (*notify.Sub, error) {
var err error
if w.Filter {
w.Filters, err = s.dao.Filters(context.TODO(), w.ID)
if err != nil {
log.Error("s.dao.Filters err(%v)", err)
}
}
return notify.NewSub(w, s.dao, s.c)
}
func (s *Service) notifyproc() {
for {
time.Sleep(time.Minute)
s.loadNotify()
}
}
func key(group, topic string) string {
return fmt.Sprintf("%s_%s", group, topic)
}
// Ping Service
func (s *Service) Ping(c context.Context) (err error) {
return s.dao.Ping(c)
}
// Close Service
func (s *Service) Close() {
s.dao.Close()
}
func (s *Service) retryproc() {
for {
fs, err := s.dao.LoadFailBk(context.TODO())
if err != nil {
log.Error("s.loadFailBk err (%v)", err)
time.Sleep(time.Minute)
continue
}
for _, f := range fs {
if n, ok := s.subs[key(f.Group, f.Topic)]; ok && !n.Closed() {
n.AddRty(f.Msg, f.ID, f.Index)
}
}
time.Sleep(time.Minute * 10)
}
}

View File

@@ -0,0 +1,41 @@
package service
import (
"context"
"flag"
"log"
"testing"
"go-common/app/infra/notify/conf"
"go-common/app/infra/notify/model"
"go-common/library/ecode"
. "github.com/smartystreets/goconvey/convey"
)
var (
s *Service
)
func TestMain(m *testing.M) {
var err error
flag.Set("conf", "../cmd/notify-test.toml")
if err = conf.Init(); err != nil {
log.Println(err)
return
}
s = New(conf.Conf)
m.Run()
}
func TestPub(t *testing.T) {
s.pubConfs = map[string]*model.Pub{
"test-test": &model.Pub{
Topic: "test",
Group: "test",
},
}
Convey("test pub", t, func() {
err := s.Pub(context.TODO(), &model.ArgPub{Topic: "test", Group: "test", AppSecret: "test"})
So(err, ShouldEqual, ecode.AccessDenied)
})
}