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,55 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"dao_test.go",
"memcache_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/app-player/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"memcache.go",
],
importpath = "go-common/app/interface/main/app-player/dao/archive",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/app-player/conf:go_default_library",
"//app/interface/main/app-player/model/archive:go_default_library",
"//app/service/main/archive/api:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/log:go_default_library",
"//vendor/github.com/pkg/errors: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,125 @@
package archive
import (
"context"
"fmt"
"runtime"
"go-common/app/interface/main/app-player/conf"
"go-common/app/interface/main/app-player/model/archive"
arcrpc "go-common/app/service/main/archive/api"
"go-common/library/cache/memcache"
"go-common/library/log"
)
// Dao is archive dao.
type Dao struct {
// memcache
arcMc *memcache.Pool
// chan
mCh chan func()
// rpc
arcRPC arcrpc.ArchiveClient
}
// New new a archive dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
// memcache
arcMc: memcache.NewPool(c.Memcache),
// mc proc
mCh: make(chan func(), 1024),
}
var err error
d.arcRPC, err = arcrpc.NewClient(c.ArchiveClient)
if err != nil {
panic(fmt.Sprintf("archive NewClient error(%v)", err))
}
for i := 0; i < runtime.NumCPU(); i++ {
go d.cacheproc()
}
return
}
// Ping ping check memcache connection
func (d *Dao) Ping(c context.Context) (err error) {
return d.pingMC(c)
}
// addCache add archive to mc or redis
func (d *Dao) addCache(f func()) {
select {
case d.mCh <- f:
default:
log.Warn("cacheproc chan full")
}
}
// cacheproc write memcache and stat redis use goroutine
func (d *Dao) cacheproc() {
for {
f, ok := <-d.mCh
if !ok {
return
}
f()
}
}
// ArchiveCache is
func (d *Dao) ArchiveCache(c context.Context, aid int64) (arc *archive.Info, err error) {
if arc, err = d.archiveCache(c, aid); err != nil {
log.Error("%+v", err)
err = nil
}
if arc != nil {
return
}
var (
view *arcrpc.ViewReply
cids []int64
)
if view, err = d.arcRPC.View(c, &arcrpc.ViewRequest{Aid: aid}); err != nil {
log.Error("d.arcRPC.View3(%d) error(%+v)", aid, err)
return
}
for _, p := range view.Pages {
cids = append(cids, p.Cid)
}
arc = &archive.Info{
Aid: view.Arc.Aid,
State: view.Arc.State,
Mid: view.Arc.Author.Mid,
Cids: cids,
Attribute: view.Arc.Attribute,
}
d.addCache(func() {
d.addArchiveCache(context.Background(), aid, arc)
})
return
}
// Views is
func (d *Dao) Views(c context.Context, aids []int64) (arcs map[int64]*archive.Info, err error) {
var reply *arcrpc.ViewsReply
if reply, err = d.arcRPC.Views(c, &arcrpc.ViewsRequest{Aids: aids}); err != nil {
return
}
arcs = make(map[int64]*archive.Info)
for _, v := range reply.Views {
var (
info = new(archive.Info)
cids []int64
)
info.Aid = v.Arc.Aid
info.State = v.Arc.State
info.Mid = v.Arc.Author.Mid
info.Attribute = v.Arc.Attribute
for _, p := range v.Pages {
cids = append(cids, p.Cid)
}
info.Cids = cids
arcs[info.Aid] = info
}
return
}

View File

@@ -0,0 +1,77 @@
package archive
import (
"context"
"flag"
"fmt"
"os"
"testing"
"go-common/app/interface/main/app-player/conf"
"github.com/smartystreets/goconvey/convey"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.app-svr.app-player")
flag.Set("conf_token", "e477d98a7c5689623eca4f32f6af735c")
flag.Set("tree_id", "52581")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "uat")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}
func TestPing(t *testing.T) {
var (
c = context.Background()
)
convey.Convey("Ping", t, func(ctx convey.C) {
err := d.Ping(c)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestArchiveCache(t *testing.T) {
var (
c = context.Background()
aid = int64(10110670)
)
convey.Convey("ArchiveCache", t, func(ctx convey.C) {
arc, err := d.ArchiveCache(c, aid)
fmt.Printf("%#v", arc)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestViews(t *testing.T) {
var (
c = context.Background()
aids = []int64{10110670}
)
convey.Convey("Views", t, func(ctx convey.C) {
_, err := d.Views(c, aids)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,59 @@
package archive
import (
"context"
"strconv"
"go-common/app/interface/main/app-player/model/archive"
"go-common/library/cache/memcache"
"go-common/library/log"
"github.com/pkg/errors"
)
const (
_prefixArc = "p_"
)
func keyArc(aid int64) string {
return _prefixArc + strconv.FormatInt(aid, 10)
}
func (d *Dao) archiveCache(c context.Context, aid int64) (arcMc *archive.Info, err error) {
conn := d.arcMc.Get(c)
key := keyArc(aid)
defer conn.Close()
r, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
err = errors.Wrapf(err, "conn.Get(%s)", key)
return
}
arcMc = &archive.Info{}
if err = conn.Scan(r, arcMc); err != nil {
err = errors.Wrapf(err, "conn.Scan(%s)", r.Value)
}
return
}
// addArchiveCache add archive cache.
func (d *Dao) addArchiveCache(c context.Context, aid int64, arc *archive.Info) (err error) {
conn := d.arcMc.Get(c)
key := keyArc(aid)
item := &memcache.Item{Key: key, Object: arc, Flags: memcache.FlagProtobuf}
if err = conn.Set(item); err != nil {
log.Error("conn.Set(%s, %v) error(%v)", key, arc, err)
}
conn.Close()
return
}
func (d *Dao) pingMC(c context.Context) (err error) {
conn := d.arcMc.Get(c)
err = conn.Set(&memcache.Item{Key: "ping", Value: []byte{1}, Flags: memcache.FlagRAW, Expiration: 0})
conn.Close()
return
}

View File

@@ -0,0 +1,20 @@
package archive
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestPingMc(t *testing.T) {
var (
c = context.Background()
)
convey.Convey("Ping", t, func(ctx convey.C) {
err := d.Ping(c)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}