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,45 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["redis_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/reply/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = ["redis.go"],
importpath = "go-common/app/job/main/reply/dao/spam",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//library/cache/redis: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,166 @@
package spam
import (
"context"
"strconv"
"go-common/library/cache/redis"
"go-common/library/log"
)
// Cache Cache
type Cache struct {
redisPool *redis.Pool
expireRp int
expireAct int
}
// NewCache NewCache
func NewCache(c *redis.Config) *Cache {
return &Cache{
redisPool: redis.NewPool(c),
expireRp: 60, // 60s
expireAct: 20, // 20s
}
}
func (c *Cache) keyRcntCnt(mid int64) string {
return "rc_" + strconv.FormatInt(mid, 10)
}
func (c *Cache) keyUpRcntCnt(mid int64) string {
return "urc_" + strconv.FormatInt(mid, 10)
}
func (c *Cache) keyDailyCnt(mid int64) string {
return "rd_" + strconv.FormatInt(mid, 10)
}
func (c *Cache) keyActRec(mid int64) string {
return "ra_" + strconv.FormatInt(mid, 10)
}
func (c *Cache) keySpamRpRec(mid int64) string {
return "sr_" + strconv.FormatInt(mid, 10)
}
func (c *Cache) keySpamRpDaily(mid int64) string {
return "sd_" + strconv.FormatInt(mid, 10)
}
func (c *Cache) keySpamActRec(mid int64) string {
return "sa_" + strconv.FormatInt(mid, 10)
}
// IncrReply incr user reply count.
func (c *Cache) IncrReply(ctx context.Context, mid int64, isUp bool) (count int, err error) {
key := c.keyRcntCnt(mid)
if isUp {
key = c.keyUpRcntCnt(mid)
}
conn := c.redisPool.Get(ctx)
defer conn.Close()
conn.Send("INCR", key)
conn.Send("EXPIRE", key, c.expireRp)
if err = conn.Flush(); err != nil {
log.Error("conn.Flush error(%v)", err)
return
}
if count, err = redis.Int(conn.Receive()); err != nil {
log.Error("conn.Receive error(%v)", key, err)
return
}
if _, err = conn.Receive(); err != nil {
log.Error("conn.Receive error(%v)", key, err)
return
}
return
}
// IncrAct incr user action count.
func (c *Cache) IncrAct(ctx context.Context, mid int64) (count int, err error) {
key := c.keyActRec(mid)
conn := c.redisPool.Get(ctx)
defer conn.Close()
conn.Send("INCR", key)
conn.Send("EXPIRE", key, c.expireAct)
if err = conn.Flush(); err != nil {
log.Error("conn.Flush error(%v)", err)
return
}
if count, err = redis.Int(conn.Receive()); err != nil {
log.Error("conn.Receive error(%v)", key, err)
return
}
if _, err = conn.Receive(); err != nil {
log.Error("conn.Receive error(%v)", key, err)
return
}
return
}
// IncrDailyReply IncrDailyReply
func (c *Cache) IncrDailyReply(ctx context.Context, mid int64) (count int, err error) {
key := c.keyDailyCnt(mid)
conn := c.redisPool.Get(ctx)
defer conn.Close()
if count, err = redis.Int(conn.Do("INCR", key)); err != nil {
log.Error("conn.Do(INCRBY, %s), error(%v)", key, err)
}
return
}
// TTLDailyReply TTLDailyReply
func (c *Cache) TTLDailyReply(ctx context.Context, mid int64) (ttl int, err error) {
key := c.keyDailyCnt(mid)
conn := c.redisPool.Get(ctx)
defer conn.Close()
if ttl, err = redis.Int(conn.Do("TTL", key)); err != nil {
log.Error("conn.Do(TTL, %s), error(%v)", key, err)
}
return
}
// ExpireDailyReply ExpireDailyReply
func (c *Cache) ExpireDailyReply(ctx context.Context, mid int64, exp int) (err error) {
key := c.keyDailyCnt(mid)
conn := c.redisPool.Get(ctx)
defer conn.Close()
if _, err = conn.Do("EXPIRE", key, exp); err != nil {
log.Error("conn.Do(EXPIRE, %s), error(%v)", key, err)
}
return
}
// SetReplyRecSpam SetReplyRecSpam
func (c *Cache) SetReplyRecSpam(ctx context.Context, mid int64, code, exp int) (err error) {
key := c.keySpamRpRec(mid)
conn := c.redisPool.Get(ctx)
defer conn.Close()
if _, err = conn.Do("SETEX", key, exp, code); err != nil {
log.Error("conn.Do error(%v)", err)
}
return
}
// SetReplyDailySpam SetReplyDailySpam
func (c *Cache) SetReplyDailySpam(ctx context.Context, mid int64, code, exp int) (err error) {
key := c.keySpamRpDaily(mid)
conn := c.redisPool.Get(ctx)
defer conn.Close()
if _, err = conn.Do("SETEX", key, exp, code); err != nil {
log.Error("conn.Do error(%v)", err)
}
return
}
// SetActionRecSpam SetActionRecSpam
func (c *Cache) SetActionRecSpam(ctx context.Context, mid int64, code, exp int) (err error) {
key := c.keySpamActRec(mid)
conn := c.redisPool.Get(ctx)
defer conn.Close()
if _, err = conn.Do("SETEX", key, exp, code); err != nil {
log.Error("conn.Do error(%v)", err)
}
return
}

View File

@@ -0,0 +1,259 @@
package spam
import (
"context"
"flag"
"go-common/app/job/main/reply/conf"
"os"
"testing"
"github.com/smartystreets/goconvey/convey"
)
var (
d *Cache
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.community.reply-job")
flag.Set("conf_token", "5deea0665f8a7670b22a719337a39c7d")
flag.Set("tree_id", "2123")
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")
} else {
flag.Set("conf", "../../cmd/reply-job-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = NewCache(conf.Conf.Redis.Config)
os.Exit(m.Run())
}
func TestSpamkeyRcntCnt(t *testing.T) {
convey.Convey("keyRcntCnt", t, func(ctx convey.C) {
var (
mid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := d.keyRcntCnt(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestSpamkeyUpRcntCnt(t *testing.T) {
convey.Convey("keyUpRcntCnt", t, func(ctx convey.C) {
var (
mid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := d.keyUpRcntCnt(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestSpamkeyDailyCnt(t *testing.T) {
convey.Convey("keyDailyCnt", t, func(ctx convey.C) {
var (
mid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := d.keyDailyCnt(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestSpamkeyActRec(t *testing.T) {
convey.Convey("keyActRec", t, func(ctx convey.C) {
var (
mid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := d.keyActRec(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestSpamkeySpamRpRec(t *testing.T) {
convey.Convey("keySpamRpRec", t, func(ctx convey.C) {
var (
mid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := d.keySpamRpRec(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestSpamkeySpamRpDaily(t *testing.T) {
convey.Convey("keySpamRpDaily", t, func(ctx convey.C) {
var (
mid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := d.keySpamRpDaily(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestSpamkeySpamActRec(t *testing.T) {
convey.Convey("keySpamActRec", t, func(ctx convey.C) {
var (
mid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := d.keySpamActRec(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestSpamIncrReply(t *testing.T) {
convey.Convey("IncrReply", t, func(ctx convey.C) {
var (
mid = int64(0)
isUp bool
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
count, err := d.IncrReply(context.Background(), mid, isUp)
ctx.Convey("Then err should be nil.count should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(count, convey.ShouldNotBeNil)
})
})
})
}
func TestSpamIncrAct(t *testing.T) {
convey.Convey("IncrAct", t, func(ctx convey.C) {
var (
mid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
count, err := d.IncrAct(context.Background(), mid)
ctx.Convey("Then err should be nil.count should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(count, convey.ShouldNotBeNil)
})
})
})
}
func TestSpamIncrDailyReply(t *testing.T) {
convey.Convey("IncrDailyReply", t, func(ctx convey.C) {
var (
mid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
count, err := d.IncrDailyReply(context.Background(), mid)
ctx.Convey("Then err should be nil.count should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(count, convey.ShouldNotBeNil)
})
})
})
}
func TestSpamTTLDailyReply(t *testing.T) {
convey.Convey("TTLDailyReply", t, func(ctx convey.C) {
var (
mid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
ttl, err := d.TTLDailyReply(context.Background(), mid)
ctx.Convey("Then err should be nil.ttl should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(ttl, convey.ShouldNotBeNil)
})
})
})
}
func TestSpamExpireDailyReply(t *testing.T) {
convey.Convey("ExpireDailyReply", t, func(ctx convey.C) {
var (
mid = int64(0)
exp = int(1)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.ExpireDailyReply(context.Background(), mid, exp)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestSpamSetReplyRecSpam(t *testing.T) {
convey.Convey("SetReplyRecSpam", t, func(ctx convey.C) {
var (
mid = int64(0)
code = int(0)
exp = int(1)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.SetReplyRecSpam(context.Background(), mid, code, exp)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestSpamSetReplyDailySpam(t *testing.T) {
convey.Convey("SetReplyDailySpam", t, func(ctx convey.C) {
var (
mid = int64(0)
code = int(0)
exp = int(1)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.SetReplyDailySpam(context.Background(), mid, code, exp)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestSpamSetActionRecSpam(t *testing.T) {
convey.Convey("SetActionRecSpam", t, func(ctx convey.C) {
var (
mid = int64(0)
code = int(0)
exp = int(1)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.SetActionRecSpam(context.Background(), mid, code, exp)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}