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,53 @@
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"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/app-feed/conf:go_default_library",
"//library/cache/redis:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"api.go",
"dao.go",
"redis.go",
],
importpath = "go-common/app/interface/main/app-feed/dao/black",
tags = ["automanaged"],
deps = [
"//app/interface/main/app-feed/conf:go_default_library",
"//library/cache/redis:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster: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,25 @@
package black
import (
"context"
)
const (
_blackURL = "http://172.18.7.208/privatedata/reco-deny-arcs.json"
)
// Black returns blacklist of aids
func (d *Dao) Black(c context.Context) (black map[int64]struct{}, err error) {
var res []int64
if err = d.clientAsyn.Get(c, _blackURL, "", nil, &res); err != nil {
return
}
if len(res) == 0 {
return
}
black = make(map[int64]struct{}, len(res))
for _, aid := range res {
black[aid] = struct{}{}
}
return
}

View File

@@ -0,0 +1,87 @@
package black
import (
"context"
"time"
"go-common/app/interface/main/app-feed/conf"
"go-common/library/cache/redis"
"go-common/library/log"
httpx "go-common/library/net/http/blademaster"
)
// Dao is black dao.
type Dao struct {
// http clientAsyn
clientAsyn *httpx.Client
// redis
redis *redis.Pool
expireRds int32
aCh chan func()
}
// New new a black dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
// http client
clientAsyn: httpx.NewClient(c.HTTPClientAsyn),
// redis init
redis: redis.NewPool(c.Redis.Feed.Config),
expireRds: int32(time.Duration(c.Redis.Feed.ExpireBlack) / time.Second),
aCh: make(chan func(), 1024),
}
go d.cacheproc()
return
}
// Ping Ping check redis connection
func (d *Dao) Ping(c context.Context) (err error) {
connRedis := d.redis.Get(c)
_, err = connRedis.Do("SET", "PING", "PONG")
connRedis.Close()
return
}
func (d *Dao) AddBlacklist(mid, aid int64) {
d.addCache(func() {
d.addBlackCache(context.Background(), mid, aid)
})
}
func (d *Dao) DelBlacklist(mid, aid int64) {
d.addCache(func() {
d.delBlackCache(context.Background(), mid, aid)
})
}
func (d *Dao) BlackList(c context.Context, mid int64) (aidm map[int64]struct{}, err error) {
var ok bool
if ok, err = d.expireBlackCache(c, mid); err != nil {
return
}
if ok {
aidm, err = d.blackCache(c, mid)
}
return
}
// addCache add cache to mc by goroutine
func (d *Dao) addCache(i func()) {
select {
case d.aCh <- i:
default:
log.Warn("cacheproc chan full")
}
}
// cacheproc cache proc
func (d *Dao) cacheproc() {
for {
f, ok := <-d.aCh
if !ok {
log.Warn("cache proc exit")
return
}
f()
}
}

View File

@@ -0,0 +1,228 @@
package black
import (
"context"
"reflect"
"testing"
"go-common/app/interface/main/app-feed/conf"
"go-common/library/cache/redis"
httpx "go-common/library/net/http/blademaster"
. "github.com/smartystreets/goconvey/convey"
)
func TestNew(t *testing.T) {
type args struct {
c *conf.Config
}
tests := []struct {
name string
args args
wantD *Dao
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotD := New(tt.args.c); !reflect.DeepEqual(gotD, tt.wantD) {
t.Errorf("New() = %v, want %v", gotD, tt.wantD)
}
})
Convey(tt.name, func(t *testing.T) {
gotD := New(tt.args.c)
So(gotD, ShouldEqual, tt.wantD)
})
}
}
func TestDao_Ping(t *testing.T) {
type fields struct {
clientAsyn *httpx.Client
redis *redis.Pool
expireRds int32
aCh chan func()
}
type args struct {
c context.Context
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &Dao{
clientAsyn: tt.fields.clientAsyn,
redis: tt.fields.redis,
expireRds: tt.fields.expireRds,
aCh: tt.fields.aCh,
}
if err := d.Ping(tt.args.c); (err != nil) != tt.wantErr {
t.Errorf("Dao.Ping() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestDao_AddBlacklist(t *testing.T) {
type fields struct {
clientAsyn *httpx.Client
redis *redis.Pool
expireRds int32
aCh chan func()
}
type args struct {
mid int64
aid int64
}
tests := []struct {
name string
fields fields
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &Dao{
clientAsyn: tt.fields.clientAsyn,
redis: tt.fields.redis,
expireRds: tt.fields.expireRds,
aCh: tt.fields.aCh,
}
d.AddBlacklist(tt.args.mid, tt.args.aid)
})
}
}
func TestDao_DelBlacklist(t *testing.T) {
type fields struct {
clientAsyn *httpx.Client
redis *redis.Pool
expireRds int32
aCh chan func()
}
type args struct {
mid int64
aid int64
}
tests := []struct {
name string
fields fields
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &Dao{
clientAsyn: tt.fields.clientAsyn,
redis: tt.fields.redis,
expireRds: tt.fields.expireRds,
aCh: tt.fields.aCh,
}
d.DelBlacklist(tt.args.mid, tt.args.aid)
})
}
}
func TestDao_BlackList(t *testing.T) {
type fields struct {
clientAsyn *httpx.Client
redis *redis.Pool
expireRds int32
aCh chan func()
}
type args struct {
c context.Context
mid int64
}
tests := []struct {
name string
fields fields
args args
wantAidm map[int64]struct{}
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &Dao{
clientAsyn: tt.fields.clientAsyn,
redis: tt.fields.redis,
expireRds: tt.fields.expireRds,
aCh: tt.fields.aCh,
}
gotAidm, err := d.BlackList(tt.args.c, tt.args.mid)
if (err != nil) != tt.wantErr {
t.Errorf("Dao.BlackList() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotAidm, tt.wantAidm) {
t.Errorf("Dao.BlackList() = %v, want %v", gotAidm, tt.wantAidm)
}
})
}
}
func TestDao_addCache(t *testing.T) {
type fields struct {
clientAsyn *httpx.Client
redis *redis.Pool
expireRds int32
aCh chan func()
}
type args struct {
i func()
}
tests := []struct {
name string
fields fields
args args
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &Dao{
clientAsyn: tt.fields.clientAsyn,
redis: tt.fields.redis,
expireRds: tt.fields.expireRds,
aCh: tt.fields.aCh,
}
d.addCache(tt.args.i)
})
}
}
func TestDao_cacheproc(t *testing.T) {
type fields struct {
clientAsyn *httpx.Client
redis *redis.Pool
expireRds int32
aCh chan func()
}
tests := []struct {
name string
fields fields
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &Dao{
clientAsyn: tt.fields.clientAsyn,
redis: tt.fields.redis,
expireRds: tt.fields.expireRds,
aCh: tt.fields.aCh,
}
d.cacheproc()
})
}
}

View File

@@ -0,0 +1,82 @@
package black
import (
"context"
"strconv"
"go-common/library/cache/redis"
"github.com/pkg/errors"
)
const (
_prefixBlack = "b_"
)
func keyBlack(mid int64) string {
return _prefixBlack + strconv.FormatInt(mid, 10)
}
func (d *Dao) blackCache(c context.Context, mid int64) (aidm map[int64]struct{}, err error) {
var aids []int64
conn := d.redis.Get(c)
key := keyBlack(mid)
defer conn.Close()
if aids, err = redis.Int64s(conn.Do("ZREVRANGE", key, 0, -1)); err != nil {
err = errors.Wrapf(err, "conn.Do(ZREVRANGE,%s,0,-1)", key)
return
}
aidm = make(map[int64]struct{}, len(aids))
for _, aid := range aids {
aidm[aid] = struct{}{}
}
return
}
func (d *Dao) addBlackCache(c context.Context, mid int64, aids ...int64) (err error) {
if len(aids) == 0 {
return
}
key := keyBlack(mid)
conn := d.redis.Get(c)
defer conn.Close()
for _, aid := range aids {
if err = conn.Send("ZADD", key, aid, aid); err != nil {
err = errors.Wrapf(err, "conn.Send(ZADD,%s,%d,%d)", key, aid, aid)
return
}
}
if err = conn.Send("EXPIRE", key, d.expireRds); err != nil {
err = errors.Wrapf(err, "conn.Send(EXPIRE,%s,%d)", key, d.expireRds)
return
}
if err = conn.Flush(); err != nil {
return
}
for i := 0; i < len(aids)+1; i++ {
if _, err = conn.Receive(); err != nil {
return
}
}
return
}
func (d *Dao) delBlackCache(c context.Context, mid, aid int64) (err error) {
key := keyBlack(mid)
conn := d.redis.Get(c)
defer conn.Close()
if _, err = conn.Do("ZREM", key, aid); err != nil {
err = errors.Wrapf(err, "conn.Do(ZREM,%s,%d)", key, aid)
}
return
}
func (d *Dao) expireBlackCache(c context.Context, mid int64) (ok bool, err error) {
key := keyBlack(mid)
conn := d.redis.Get(c)
defer conn.Close()
if ok, err = redis.Bool(conn.Do("EXPIRE", key, d.expireRds)); err != nil {
err = errors.Wrapf(err, "conn.Do(EXPIRE,%s,%d)", key, d.expireRds)
}
return
}