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,21 @@
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//app/job/main/identify/cmd:all-srcs",
"//app/job/main/identify/conf:all-srcs",
"//app/job/main/identify/dao:all-srcs",
"//app/job/main/identify/http:all-srcs",
"//app/job/main/identify/model:all-srcs",
"//app/job/main/identify/service:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,44 @@
### identify-job
#### Version 1.8.0
> 1. add auth cache deleted check
#### Version 1.7.0
> 1. identify job support databusutil method
> 2. identify service support databusutil method
#### Version 1.6.0
> 1. add auth binlog clean
#### Version 1.5.1
> 1. fix not found,keep running
#### Version 1.5.0
> 1. del "rpc cli delCache" method
#### Version 1.4.1
> 1. fix mc set proto err
#### Version 1.4.0
> 1.support app web community authn model
#### Version 1.3.0
> 1.support bm.
#### Version 1.2.0
>1. mv to main/identify
#### Version 1.2.0
> 1.增加清理memcache
#### Version 1.1.1
> 1.fix game app id return
#### Version 1.1.0
> 1.add error log for calling identify service RPC
#### Version 1.0.1
> 1.opts code
#### Version 1.0.0
> 1.passport二级缓存服务

View File

@@ -0,0 +1,10 @@
# Owner
linmiao
wanghuan01
# Author
wutao
chenjunxi
# Reviewer
wanghuan01

View File

@@ -0,0 +1,17 @@
# See the OWNERS docs at https://go.k8s.io/owners
approvers:
- chenjunxi
- linmiao
- wanghuan01
- wutao
labels:
- job
- job/main/identify
- main
options:
no_parent_owners: true
reviewers:
- chenjunxi
- wanghuan01
- wutao

View File

@@ -0,0 +1,13 @@
#### identify-job
##### 项目简介
> 1.passport二级缓存服务
##### 编译环境
> 请只用golang v1.8.x以上版本编译执行。
##### 依赖包
> 1.公共包go-common
##### 特别说明
> 1.model目录可能会被其他项目引用请谨慎请改并通知各方。

View File

@@ -0,0 +1,42 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_binary",
"go_library",
)
go_binary(
name = "cmd",
embed = [":go_default_library"],
tags = ["automanaged"],
)
go_library(
name = "go_default_library",
srcs = ["main.go"],
data = ["identify-job-test.toml"],
importpath = "go-common/app/job/main/identify/cmd",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/identify/conf:go_default_library",
"//app/job/main/identify/http:go_default_library",
"//app/job/main/identify/service: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,98 @@
# This is a TOML document. Boom.
[databus]
[databus.identifySub]
key = "4ba46ba31f9a44ef"
secret = "e4c5a7fce28695209e6b4f0af8cf91c5"
group = "Passport-MainAccount-Identify-S"
topic = "Passport-T"
action = "sub"
offset = "old"
buffer = 2048
name = "identify-job-live/databus"
proto = "tcp"
addr = "172.18.33.50:6205"
idle = 1
active = 1
dialTimeout = "1s"
readTimeout = "60s"
writeTimeout = "1s"
idleTimeout = "10s"
[databus.identifySub.discovery]
domain = "api.bilibili.co"
key = "7634436ea852e3f4"
secret = "test"
[databus.authDataBus]
group = "PassportAuthBinlog-MainAccount-Identify-S"
key = "4ba46ba31f9a44ef"
secret = "e4c5a7fce28695209e6b4f0af8cf91c5"
topic = "PassportAuthBinlog-T"
action = "sub"
offset = "old"
buffer = 2048
name = "passport-auth-job/databus"
proto = "tcp"
addr = "172.18.33.50:6205"
idle = 1
active = 1
dialTimeout = "1s"
readTimeout = "60s"
writeTimeout = "1s"
idleTimeout = "10s"
[databus.authDataBus.discovery]
domain = "api.bilibili.co"
key = "7634436ea852e3f4"
secret = "test"
[databusutil]
size = 100
chan = 1024
num = 2
ticker="1s"
[memcaches]
[memcaches.identify]
prefix = "i_"
name = "test"
proto = "tcp"
addr = "127.0.0.1:11211"
active = 5
idle = 1
dialTimeout = "1s"
readTimeout = "1s"
writeTimeout = "1s"
idleTimeout = "80s"
[authDB]
addr = "127.0.0.1:3306"
dsn = "root:root@tcp(127.0.0.1:3306)/passport_auth?timeout=1s&readTimeout=1s&writeTimeout=1s&parseTime=true&loc=Local&charset=utf8,utf8mb4"
active = 5
idle = 2
queryTimeout = "1s"
execTimeout = "2s"
tranTimeout = "2s"
[db.auth.breaker]
window = "3s"
sleep = "100ms"
bucket = 10
ratio = 0.5
request = 100
[authMC]
name = "passport-auth"
proto = "tcp"
addr = "172.18.33.61:11246"
active = 50
idle = 10
dialTimeout = "1s"
readTimeout = "1s"
writeTimeout = "1s"
idleTimeout = "10s"
[checkConf]
switch = true
chanNum = 2
chanSize = 10240
ticker = "1s"
count = 10

View File

@@ -0,0 +1,46 @@
package main
import (
"flag"
"os"
"os/signal"
"syscall"
"time"
"go-common/app/job/main/identify/conf"
"go-common/app/job/main/identify/http"
"go-common/app/job/main/identify/service"
"go-common/library/log"
)
func main() {
flag.Parse()
if err := conf.Init(); err != nil {
log.Error("conf.Init() error(%v)", err)
panic(err)
}
log.Init(conf.Conf.Log)
defer log.Close()
log.Info("identify-job start")
// init
svc := service.New(conf.Conf)
http.Init(conf.Conf, svc)
// init signal
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
for {
s := <-c
log.Info("identify job get a signal %s", s.String())
switch s {
case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
svc.Close()
time.Sleep(time.Second)
log.Info("identify job exit")
return
case syscall.SIGHUP:
// TODO reload
default:
return
}
}
}

View File

@@ -0,0 +1,40 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["conf.go"],
importpath = "go-common/app/job/main/identify/conf",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//library/cache/memcache:go_default_library",
"//library/conf:go_default_library",
"//library/database/sql:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/trace:go_default_library",
"//library/queue/databus:go_default_library",
"//library/queue/databus/databusutil:go_default_library",
"//library/time:go_default_library",
"//vendor/github.com/BurntSushi/toml: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,117 @@
package conf
import (
"errors"
"flag"
"go-common/library/cache/memcache"
"go-common/library/conf"
"go-common/library/database/sql"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
"go-common/library/net/trace"
"go-common/library/queue/databus"
"go-common/library/queue/databus/databusutil"
xtime "go-common/library/time"
"github.com/BurntSushi/toml"
)
// Conf global variable.
var (
Conf = &Config{}
client *conf.Client
confPath string
)
// Config struct of conf.
type Config struct {
// base
// log
Log *log.Config
//Tracer *conf.Tracer
Tracer *trace.Config
//Databus databus
DataBus *DataBus
// Databusutil
Databusutil *databusutil.Config
// memcache
Memcaches map[string]*Memcache
// BM
BM *bm.ServerConfig
// AuthDB
AuthDB *sql.Config
// AuthMC
AuthMC *memcache.Config
// CheckConf
CheckConf *CheckConf
}
// DataBus databus.
type DataBus struct {
IdentifySub *databus.Config
AuthDataBus *databus.Config
}
// Memcache contains prefix
type Memcache struct {
Prefix string
*memcache.Config
}
// CheckConf .
type CheckConf struct {
Switch bool
ChanNum int
ChanSize int
Ticker xtime.Duration
Count int64
}
func local() (err error) {
_, err = toml.DecodeFile(confPath, &Conf)
return
}
func remote() (err error) {
if client, err = conf.New(); err != nil {
return
}
if err = load(); err != nil {
return
}
go func() {
for range client.Event() {
log.Info("config event")
}
}()
return
}
func load() (err error) {
var (
s string
ok bool
tmpConf *Config
)
if s, ok = client.Toml2(); !ok {
return errors.New("load config center error")
}
if _, err = toml.Decode(s, &tmpConf); err != nil {
return errors.New("could not decode config")
}
*Conf = *tmpConf
return
}
func init() {
flag.StringVar(&confPath, "conf", "", "default config path")
}
// Init int config
func Init() error {
if confPath != "" {
return local()
}
return remote()
}

View File

@@ -0,0 +1,58 @@
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",
"mc_test.go",
"mysql_test.go",
],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/job/main/identify/conf:go_default_library",
"//library/database/sql:go_default_library",
"//vendor/github.com/bouk/monkey:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"mc.go",
"mysql.go",
],
importpath = "go-common/app/job/main/identify/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/identify/conf:go_default_library",
"//app/job/main/identify/model:go_default_library",
"//app/service/main/passport-auth/model:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/database/sql: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,29 @@
package dao
import (
"go-common/app/job/main/identify/conf"
"go-common/library/cache/memcache"
xsql "go-common/library/database/sql"
)
// Dao dao
type Dao struct {
c *conf.Config
authDB *xsql.DB
authMC *memcache.Pool
}
// New init mysql db
func New(c *conf.Config) (dao *Dao) {
dao = &Dao{
c: c,
authDB: xsql.NewMySQL(c.AuthDB),
authMC: memcache.NewPool(c.AuthMC),
}
return
}
// Close close the resource.
func (d *Dao) Close() {
d.authDB.Close()
}

View File

@@ -0,0 +1,54 @@
package dao
import (
"flag"
"os"
"reflect"
"testing"
"go-common/app/job/main/identify/conf"
"go-common/library/database/sql"
"github.com/bouk/monkey"
"github.com/smartystreets/goconvey/convey"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.account.identify-job")
flag.Set("conf_token", "138ecf5e1bc269922614e4b549b261bb")
flag.Set("tree_id", "18436")
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/identify-job-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
os.Exit(m.Run())
}
func TestDaoClose(t *testing.T) {
convey.Convey("Close", t, func(ctx convey.C) {
monkey.PatchInstanceMethod(reflect.TypeOf(d.authDB), "Close", func(_ *sql.DB) error {
return nil
})
defer monkey.UnpatchAll()
var err error
d.Close()
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}

View File

@@ -0,0 +1,89 @@
package dao
import (
"context"
"fmt"
"go-common/app/service/main/passport-auth/model"
"go-common/library/cache/memcache"
"go-common/library/log"
)
func ckKey(session string) string {
return fmt.Sprintf("ck_%s", session)
}
func akKey(token string) string {
return fmt.Sprintf("ak_%s", token)
}
// CookieCache get cookie info from cache
func (d *Dao) CookieCache(c context.Context, session string) (res *model.Cookie, err error) {
key := ckKey(session)
conn := d.authMC.Get(c)
defer conn.Close()
var item *memcache.Item
if item, err = conn.Get(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
log.Error("conn.Get(%s) error(%v)", key, err)
return
}
res = new(model.Cookie)
if err = conn.Scan(item, res); err != nil {
log.Error("conn.Scan(%v) error(%v)", string(item.Value), err)
}
return
}
// DelCookieCache del cache.
func (d *Dao) DelCookieCache(c context.Context, session string) (err error) {
conn := d.authMC.Get(c)
defer conn.Close()
if err = conn.Delete(ckKey(session)); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
log.Error("conn.Delete(%s) error(%v)", ckKey(session), err)
}
return
}
// TokenCache get token from cache
func (d *Dao) TokenCache(c context.Context, sd string) (res *model.Token, err error) {
key := akKey(sd)
conn := d.authMC.Get(c)
defer conn.Close()
r, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
log.Error("conn.Get(%s) error(%v)", key, err)
return
}
res = new(model.Token)
if err = conn.Scan(r, res); err != nil {
log.Error("conn.Scan(%v) error(%v)", string(r.Value), err)
}
return
}
// DelTokenCache del cache.
func (d *Dao) DelTokenCache(c context.Context, token string) (err error) {
key := akKey(token)
conn := d.authMC.Get(c)
defer conn.Close()
if err = conn.Delete(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
log.Error("conn.Delete(%s) error(%v)", key, err)
}
return
}

View File

@@ -0,0 +1,74 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoCookieCache(t *testing.T) {
convey.Convey("CookieCache", t, func(ctx convey.C) {
var (
c = context.Background()
session = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.CookieCache(c, session)
ctx.Convey("Then res should be nil.", func(ctx convey.C) {
ctx.So(res, convey.ShouldBeNil)
})
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoDelCookieCache(t *testing.T) {
convey.Convey("DelCookieCache", t, func(ctx convey.C) {
var (
c = context.Background()
session = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.DelCookieCache(c, session)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoTokenCache(t *testing.T) {
convey.Convey("TokenCache", t, func(ctx convey.C) {
var (
c = context.Background()
session = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.TokenCache(c, session)
ctx.Convey("Then res should be nil.", func(ctx convey.C) {
ctx.So(res, convey.ShouldBeNil)
})
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoDelTokenCache(t *testing.T) {
convey.Convey("DelTokenCache", t, func(ctx convey.C) {
var (
c = context.Background()
session = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.DelTokenCache(c, session)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,71 @@
package dao
import (
"context"
"encoding/hex"
"fmt"
"go-common/app/job/main/identify/model"
xsql "go-common/library/database/sql"
"go-common/library/log"
)
const (
_newSessionBinByteLen = 16
_getCookieDeletedSQL = "SELECT id,mid,session FROM user_cookie_deleted_%s where id > ? limit ?"
_getTokenDeletedSQL = "SELECT id,mid,token FROM user_token_deleted_%s where id > ? limit ?"
)
// CookieDeleted get cookie deleted
func (d *Dao) CookieDeleted(c context.Context, start, count int64, suffix string) (res []*model.AuthCookie, err error) {
var rows *xsql.Rows
if rows, err = d.authDB.Query(c, fmt.Sprintf(_getCookieDeletedSQL, suffix), start, count); err != nil {
log.Error("fail to get CookieDeleted, dao.authDB.Query(%s) error(%v)", _getCookieDeletedSQL, err)
return
}
defer rows.Close()
for rows.Next() {
var session []byte
a := new(model.AuthCookie)
if err = rows.Scan(&a.ID, &a.Mid, &session); err != nil {
log.Error("row.Scan() error(%v)", err)
res = nil
return
}
a.Session = encodeSession(session)
res = append(res, a)
}
return
}
// TokenDeleted get token deleted
func (d *Dao) TokenDeleted(c context.Context, start, count int64, suffix string) (res []*model.AuthToken, err error) {
var rows *xsql.Rows
if rows, err = d.authDB.Query(c, fmt.Sprintf(_getTokenDeletedSQL, suffix), start, count); err != nil {
log.Error("fail to get TokenDeleted, dao.authDB.Query(%s) error(%v)", _getTokenDeletedSQL, err)
return
}
defer rows.Close()
for rows.Next() {
var token []byte
a := new(model.AuthToken)
if err = rows.Scan(&a.ID, &a.Mid, &token); err != nil {
log.Error("row.Scan() error(%v)", err)
res = nil
return
}
a.Token = hex.EncodeToString(token)
res = append(res, a)
}
return
}
func encodeSession(b []byte) (s string) {
// format new
if len(b) == _newSessionBinByteLen {
return hex.EncodeToString(b)
}
// or format old
return string(b)
}

View File

@@ -0,0 +1,49 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoCookieDeleted(t *testing.T) {
convey.Convey("CookieDeleted", t, func(ctx convey.C) {
var (
c = context.Background()
suffix = "201810"
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.CookieDeleted(c, 0, 100, suffix)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoTokenDeleted(t *testing.T) {
convey.Convey("TokenDeleted", t, func(ctx convey.C) {
var (
c = context.Background()
suffix = "201810"
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.TokenDeleted(c, 0, 100, suffix)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDao_encodeSession(t *testing.T) {
convey.Convey("encodeSession", t, func(ctx convey.C) {
res := encodeSession([]byte{1})
ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
ctx.So(res, convey.ShouldNotBeNil)
})
})
}

View File

@@ -0,0 +1,34 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["http.go"],
importpath = "go-common/app/job/main/identify/http",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/identify/conf:go_default_library",
"//app/job/main/identify/service:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster: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,41 @@
package http
import (
"net/http"
"go-common/app/job/main/identify/conf"
"go-common/app/job/main/identify/service"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
)
var (
srv *service.Service
)
// Init init http sever instance.
func Init(c *conf.Config, s *service.Service) {
srv = s
// init inner router
// engine
engIn := bm.DefaultServer(c.BM)
innerRouter(engIn)
// init inner server
if err := engIn.Start(); err != nil {
log.Error("bm.DefaultServer error(%v)", err)
panic(err)
}
}
// innerRouter init inner router.
func innerRouter(e *bm.Engine) {
e.Ping(ping)
}
// ping check server ok.
func ping(c *bm.Context) {
if err := srv.Ping(c); err != nil {
log.Error("ping error(%v)", err)
c.AbortWithStatus(http.StatusServiceUnavailable)
}
}

View File

@@ -0,0 +1,28 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["message.go"],
importpath = "go-common/app/job/main/identify/model",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
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,53 @@
package model
import "encoding/json"
// BMsg databus binlog message.
type BMsg struct {
Action string `json:"action"`
Table string `json:"table"`
New json.RawMessage `json:"new"`
Old json.RawMessage `json:"old"`
}
// Token info.
type Token struct {
Mid int64 `json:"mid"`
APPID int64 `json:"appid"`
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
APPSubID int64 `json:"app_subid"`
Expires int32 `json:"expires"`
Permission string `json:"permission"`
TP int8 `json:"type"`
Version string `json:"version"`
}
// Cookie info.
type Cookie struct {
Mid int64 `json:"mid"`
SessionData string `json:"session_data"`
CSRFToken string `json:"csrf_token"`
TP uint8 `json:"type"`
ExpireTime int32 `json:"expire_time"`
}
// AuthCookie for auth
type AuthCookie struct {
ID int64 `json:"id"`
Mid int64 `json:"mid"`
Session string `json:"session"`
CSRF string `json:"csrf"`
Type int64 `json:"type"`
Expires int64 `json:"expires"`
}
// AuthToken for auth
type AuthToken struct {
ID int64 `json:"id"`
Mid int64 `json:"mid"`
AppID int64 `json:"appid"`
Token string `json:"token"`
Expires int64 `json:"expires"`
Type int64 `json:"type"`
}

View File

@@ -0,0 +1,60 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"identify_test.go",
"service_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/identify/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"auth.go",
"check_task.go",
"identify.go",
"service.go",
],
importpath = "go-common/app/job/main/identify/service",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/identify/conf:go_default_library",
"//app/job/main/identify/dao:go_default_library",
"//app/job/main/identify/model:go_default_library",
"//app/service/main/identify/model:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/log:go_default_library",
"//library/queue/databus:go_default_library",
"//library/queue/databus/databusutil: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,116 @@
package service
import (
"encoding/base64"
"encoding/hex"
"encoding/json"
"strings"
"go-common/app/job/main/identify/model"
mdl "go-common/app/service/main/identify/model"
"go-common/library/log"
"go-common/library/queue/databus"
)
func (s *Service) new(msg *databus.Message) (interface{}, error) {
bmsg := new(model.BMsg)
if err := json.Unmarshal(msg.Value, bmsg); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(msg.Value), err)
return nil, err
}
if bmsg.Action != "delete" {
return bmsg, nil
}
if strings.HasPrefix(bmsg.Table, "user_token_") {
t := new(model.AuthToken)
if err := json.Unmarshal(bmsg.New, t); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(bmsg.New), err)
return nil, err
}
log.Info("identifyconsumeproc table:%s key:%s partition:%d offset:%d", bmsg.Table, msg.Key, msg.Partition, msg.Offset)
return t, nil
} else if strings.HasPrefix(bmsg.Table, "user_cookie_") {
t := new(model.AuthCookie)
if err := json.Unmarshal(bmsg.New, t); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(bmsg.New), err)
return nil, err
}
log.Info("identifyconsumeproc table:%s key:%s partition:%d offset:%d", bmsg.Table, msg.Key, msg.Partition, msg.Offset)
return t, nil
}
return bmsg, nil
}
func (s *Service) spilt(msg *databus.Message, data interface{}) int {
switch t := data.(type) {
case *model.AuthToken:
return int(t.Mid)
case *model.AuthCookie:
return int(t.Mid)
default:
return 0
}
}
func (s *Service) processAuthBinlog2(bmsgs []interface{}) {
for _, msg := range bmsgs {
switch t := msg.(type) {
case *model.AuthToken:
var (
bytes []byte
err error
)
if bytes, err = base64.StdEncoding.DecodeString(t.Token); err != nil {
log.Error("cleanCookieCache base64 decode err %v", err)
err = nil
return
}
info := &mdl.IdentifyInfo{
Mid: t.Mid,
Expires: int32(t.Expires),
}
if ok := isGameAppID(t.AppID); ok {
continue
}
for {
log.Info("auth service process token databus, key(%s)", hex.EncodeToString(bytes))
if err := s.processIdentify("delete", hex.EncodeToString(bytes), info); err != nil {
continue
}
break
}
case *model.AuthCookie:
var (
bytes []byte
bytesCSRF []byte
err error
)
if bytes, err = base64.StdEncoding.DecodeString(t.Session); err != nil {
log.Error("cleanCookieCache base64 decode err %v", err)
err = nil
return
}
if bytesCSRF, err = base64.StdEncoding.DecodeString(t.CSRF); err != nil {
log.Error("cleanCookieCache base64 decode err %v", err)
err = nil
return
}
info := &mdl.IdentifyInfo{
Mid: t.Mid,
Csrf: hex.EncodeToString(bytesCSRF),
Expires: int32(t.Expires),
}
for {
log.Info("auth service process cookie databus, key(%s)", string(bytes))
if err := s.processIdentify("delete", string(bytes), info); err != nil {
continue
}
break
}
default:
return
}
}
}

View File

@@ -0,0 +1,223 @@
package service
import (
"context"
"sync"
"time"
"go-common/app/job/main/identify/model"
"go-common/library/cache/memcache"
"go-common/library/log"
"go-common/library/stat/prom"
)
func (s *Service) queryCookieDeleted() {
var wg sync.WaitGroup
ticker := time.NewTicker(time.Duration(s.c.CheckConf.Ticker))
for {
now := time.Now()
for i := 0; i < 2; i++ {
wg.Add(1)
go func(i int, now time.Time) {
defer wg.Done()
s.readCookieData(dateFormat(addMonth(now, -i)))
}(i, now)
}
wg.Wait()
<-ticker.C
}
}
func (s *Service) readCookieData(moth string) {
var start int64
for {
cookies, err := s.d.CookieDeleted(context.Background(), start, s.c.CheckConf.Count, moth)
if err != nil {
log.Error("fail to get CookieDeleted error(%+v)", err)
time.Sleep(100 * time.Millisecond)
continue
}
if len(cookies) == 0 {
log.Info("check cookie(%s) finished!", moth)
return
}
maxID := int64(0)
for _, a := range cookies {
s.cookieCh[a.ID%int64(s.c.CheckConf.ChanNum)] <- a
if maxID < a.ID {
maxID = a.ID
}
}
start = maxID
}
}
func (s *Service) queryTokenDeleted() {
var wg sync.WaitGroup
ticker := time.NewTicker(time.Duration(s.c.CheckConf.Ticker))
for {
now := time.Now()
for i := 0; i < 3; i++ {
wg.Add(1)
go func(i int, now time.Time) {
defer wg.Done()
s.readTokenData(dateFormat(addMonth(now, -i)))
}(i, now)
}
wg.Wait()
<-ticker.C
}
}
func (s *Service) readTokenData(moth string) {
var start int64
for {
tokens, err := s.d.TokenDeleted(context.Background(), start, s.c.CheckConf.Count, moth)
if err != nil {
log.Error("fail to get TokenDeleted error(%+v)", err)
time.Sleep(100 * time.Millisecond)
continue
}
if len(tokens) == 0 {
log.Info("check token(%s) finished!", moth)
return
}
maxID := int64(0)
for _, a := range tokens {
s.tokenCh[a.ID%int64(s.c.CheckConf.ChanNum)] <- a
if maxID < a.ID {
maxID = a.ID
}
}
start = maxID
}
}
func (s *Service) checkCookie(c chan *model.AuthCookie) {
count := 0
for {
cookie, ok := <-c
if !ok {
log.Error("cookieChan closed")
return
}
count = count + 1
if count%10000 == 0 {
count = 0
time.Sleep(100 * time.Millisecond)
}
// auth
for {
res, err := s.d.CookieCache(context.Background(), cookie.Session)
if err != nil {
log.Error("fail to get cookie(%+v) cache from auth , error(%+v)", cookie, err)
time.Sleep(100 * time.Millisecond)
continue
}
if res != nil {
prom.BusinessErrCount.Incr("auth:cacheNotDeleted")
log.Error("auth cache not deleted, session(%s) mid(%d)", cookie.Session, cookie.Mid)
for {
err = s.d.DelCookieCache(context.Background(), cookie.Session)
if err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
}
break
}
// identify
s.checkIdentifyCache(cookie.Session, cookie.Mid)
}
}
func (s *Service) checkToken(c chan *model.AuthToken) {
count := 0
for {
token, ok := <-c
if !ok {
log.Error("tokenChan closed")
return
}
count = count + 1
if count%10000 == 0 {
count = 0
time.Sleep(100 * time.Millisecond)
}
// auth
for {
res, err := s.d.TokenCache(context.Background(), token.Token)
if err != nil {
log.Error("fail to get token(%+v) cache from auth , error(%+v)", token, err)
time.Sleep(100 * time.Millisecond)
continue
}
if res != nil {
prom.BusinessErrCount.Incr("auth:cacheNotDeleted")
log.Error("auth cache not deleted, token(%s) mid(%d)", token.Token, token.Mid)
for {
err = s.d.DelTokenCache(context.Background(), token.Token)
if err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
}
break
}
// identify
s.checkIdentifyCache(token.Token, token.Mid)
}
}
func (s *Service) checkIdentifyCache(k string, mid int64) {
for name, p := range s.poolm {
mcc, ok := s.c.Memcaches[name]
if !ok || mcc == nil {
return
}
key := mcc.Prefix + k
for {
conn := p.Get(context.Background())
res, err := conn.Get(key)
conn.Close()
if err != nil {
if err == memcache.ErrNotFound {
break
}
log.Error("fail to get cache(%s) from identify , error(%+v)", key, err)
time.Sleep(100 * time.Millisecond)
continue
}
if res != nil {
prom.BusinessErrCount.Incr("identify:cacheNotDeleted")
log.Error("identify cache not deleted, key(%s) mid(%d) cache(%s)", key, mid, name)
for {
conn := p.Get(context.Background())
err := conn.Delete(key)
conn.Close()
if err == nil || err == memcache.ErrNotFound {
break
}
log.Error("dao.DelCache(%s) error(%+v)", key, err)
time.Sleep(100 * time.Millisecond)
}
}
break
}
}
}
func dateFormat(t time.Time) string {
return t.Format("200601")
}
func addMonth(t time.Time, delta int) time.Time {
if delta == 0 {
return t
}
year, month, _ := t.Date()
thisMonthFirstDay := time.Date(year, month, 1, 1, 1, 1, 1, t.Location())
return thisMonthFirstDay.AddDate(0, delta, 0)
}

View File

@@ -0,0 +1,157 @@
package service
import (
"context"
"encoding/json"
"strings"
"time"
"go-common/app/job/main/identify/model"
mdl "go-common/app/service/main/identify/model"
"go-common/library/cache/memcache"
"go-common/library/log"
"go-common/library/queue/databus"
)
type actionToken struct {
model.Token
Action string
}
type actionCookie struct {
model.Cookie
Action string
}
func (s *Service) identifyNew(msg *databus.Message) (interface{}, error) {
bmsg := new(model.BMsg)
if err := json.Unmarshal(msg.Value, bmsg); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(msg.Value), err)
return nil, err
}
log.Info("identify service process databus message, table(%s)", bmsg.Table)
if strings.HasPrefix(bmsg.Table, _tokenTable) {
t := new(actionToken)
if err := json.Unmarshal(bmsg.New, t); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(bmsg.New), err)
return nil, err
}
log.Info("process databus message, table(%s), action(%s)", bmsg.Table, bmsg.Action)
t.Action = bmsg.Action
return t, nil
}
if strings.HasPrefix(bmsg.Table, _cookieTable) {
t := new(actionCookie)
if err := json.Unmarshal(bmsg.New, t); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(bmsg.New), err)
return nil, err
}
log.Info("process databus message, table(%s), action(%s)", bmsg.Table, bmsg.Action)
t.Action = bmsg.Action
return t, nil
}
return bmsg, nil
}
func (s *Service) identifySplit(msg *databus.Message, data interface{}) int {
mergeNum := int64(s.c.Databusutil.Num)
mid := int64(0)
switch t := data.(type) {
case *model.Cookie:
mid = t.Mid
case *model.Token:
mid = t.Mid
}
return int(mid % mergeNum)
}
func (s *Service) processIdentifyInfo(bmsgs []interface{}) {
for _, msg := range bmsgs {
switch t := msg.(type) {
case *actionToken:
info := &mdl.IdentifyInfo{
Mid: t.Mid,
Expires: t.Expires,
}
if ok := isGameAppID(t.APPID); ok {
continue
}
s.processIdentify(t.Action, t.AccessToken, info)
case *actionCookie:
info := &mdl.IdentifyInfo{
Mid: t.Mid,
Csrf: t.CSRFToken,
Expires: t.ExpireTime,
}
s.processIdentify(t.Action, t.SessionData, info)
}
}
}
func (s *Service) processIdentify(action, key string, info *mdl.IdentifyInfo) (err error) {
if err = s.processMc(action, key, info); err != nil {
return
}
log.Info("identify process action(%s) mid(%d) csrf(%s) key(%s) success", action, info.Mid, info.Csrf, key[:8])
return
}
// processMc .
func (s *Service) processMc(action, k string, info *mdl.IdentifyInfo) (err error) {
if len(s.poolm) == 0 {
return
}
if _insertAction == action {
expire := info.Expires
if expire < int32(time.Now().Unix()) {
log.Error("identify expire error(%d,%d)", info.Expires, time.Now().Unix())
return
}
for name, p := range s.poolm {
mcc, ok := s.c.Memcaches[name]
if !ok || mcc == nil {
return
}
key := mcc.Prefix + k
conn := p.Get(context.Background())
err = conn.Set(&memcache.Item{Key: key, Object: info, Flags: memcache.FlagProtobuf, Expiration: expire})
conn.Close()
if err != nil {
log.Error("old identify set error(%s,%d,%v)", key, info.Expires, err)
err = nil
}
}
return
}
if _delteAction == action {
for name, p := range s.poolm {
mcc, ok := s.c.Memcaches[name]
if !ok || mcc == nil {
return
}
key := mcc.Prefix + k
// if delete failed, retry until success
for {
conn := p.Get(context.Background())
err := conn.Delete(key)
if err == nil || err == memcache.ErrNotFound {
conn.Close()
break
}
log.Error("dao.DelCache(%s) error(%v)", key, err)
conn.Close()
time.Sleep(time.Second)
}
}
}
return
}
func isGameAppID(appid int64) (res bool) {
for i := 0; i < len(_gameAppID); i++ {
if _gameAppID[i] == appid {
return true
}
}
return false
}

View File

@@ -0,0 +1,56 @@
package service
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestServie_Ping(t *testing.T) {
Convey("test identify consumer proc", t, func() {
once.Do(startService)
s.Ping(context.Background())
})
}
func TestService_identify(t *testing.T) {
Convey("test identify consumer proc", t, func() {
once.Do(startService)
var (
bmsg interface{}
err error
res int
)
msg := <-s.identifySub.Messages()
bmsg, err = s.identifyNew(msg)
So(err, ShouldBeNil)
So(bmsg, ShouldNotBeNil)
res = s.identifySplit(msg, bmsg)
So(res, ShouldNotBeNil)
bmsgs := []interface{}{bmsg}
s.processIdentifyInfo(bmsgs)
})
}
func TestService_auth(t *testing.T) {
Convey("test identify consumer proc", t, func() {
once.Do(startService)
var (
m interface{}
err error
res int
)
msg := <-s.authDataBus.Messages()
m, err = s.new(msg)
So(err, ShouldBeNil)
So(m, ShouldNotBeNil)
res = s.spilt(msg, m)
So(res, ShouldNotBeNil)
ms := []interface{}{m}
s.processIdentifyInfo(ms)
})
}

View File

@@ -0,0 +1,100 @@
package service
import (
"context"
"go-common/app/job/main/identify/conf"
"go-common/app/job/main/identify/dao"
"go-common/app/job/main/identify/model"
"go-common/library/cache/memcache"
"go-common/library/queue/databus"
"go-common/library/queue/databus/databusutil"
)
const (
_tokenTable = "aso_app_perm"
_cookieTable = "aso_cookie_token"
_insertAction = "insert"
_delteAction = "delete"
)
var (
_gameAppID = [3]int64{432, 876, 849}
)
// Service is a identify service.
type Service struct {
c *conf.Config
d *dao.Dao
identifySub *databus.Databus
authDataBus *databus.Databus
// mc
poolm map[string]*memcache.Pool
// databus group
authGroup *databusutil.Group
identifyGroup *databusutil.Group
cookieCh []chan *model.AuthCookie
tokenCh []chan *model.AuthToken
}
// New new a identify service.
func New(c *conf.Config) (s *Service) {
s = &Service{
c: c,
d: dao.New(c),
identifySub: databus.New(c.DataBus.IdentifySub),
authDataBus: databus.New(c.DataBus.AuthDataBus),
cookieCh: make([]chan *model.AuthCookie, c.CheckConf.ChanNum),
tokenCh: make([]chan *model.AuthToken, c.CheckConf.ChanNum),
}
if len(s.c.Memcaches) > 0 {
pm := make(map[string]*memcache.Pool, len(s.c.Memcaches))
for name, mcc := range s.c.Memcaches {
p := memcache.NewPool(mcc.Config)
pm[name] = p
}
s.poolm = pm
}
s.authGroup = databusutil.NewGroup(c.Databusutil, s.authDataBus.Messages())
s.authGroup.New = s.new
s.authGroup.Split = s.spilt
s.authGroup.Do = s.processAuthBinlog2
s.authGroup.Start()
s.identifyGroup = databusutil.NewGroup(c.Databusutil, s.identifySub.Messages())
s.identifyGroup.New = s.identifyNew
s.identifyGroup.Split = s.identifySplit
s.identifyGroup.Do = s.processIdentifyInfo
s.identifyGroup.Start()
if c.CheckConf.Switch {
for i := 0; i < c.CheckConf.ChanNum; i++ {
cookie := make(chan *model.AuthCookie, c.CheckConf.ChanSize)
token := make(chan *model.AuthToken, c.CheckConf.ChanSize)
s.cookieCh[i] = cookie
s.tokenCh[i] = token
go s.checkCookie(cookie)
go s.checkToken(token)
}
go s.queryCookieDeleted()
go s.queryTokenDeleted()
}
return
}
// Ping .
func (s *Service) Ping(c context.Context) (err error) {
return nil
}
// Close close.
func (s *Service) Close() (err error) {
s.identifySub.Close()
s.authDataBus.Close()
s.authGroup.Close()
s.identifyGroup.Close()
return nil
}

View File

@@ -0,0 +1,18 @@
package service
import (
"go-common/app/job/main/identify/conf"
"sync"
)
var (
once sync.Once
s *Service
)
func startService() {
if err := conf.Init(); err != nil {
panic(err)
}
s = New(conf.Conf)
}