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

56
library/ecode/tip/BUILD Normal file
View File

@@ -0,0 +1,56 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["tip_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = ["//library/ecode:go_default_library"],
)
go_library(
name = "go_default_library",
srcs = ["tip.go"],
importpath = "go-common/library/ecode/tip",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/time:go_default_library",
],
)
go_test(
name = "go_default_xtest",
srcs = ["example_test.go"],
tags = ["automanaged"],
deps = [
"//library/ecode/tip:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/netutil/breaker:go_default_library",
"//library/time: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,20 @@
### ecode sdk
##### Version 2.3.0
> 1. 从bussiness 移动到ecode&& ecode/tips
##### Version 2.2.1
> 1. 删除Config里面App配置
##### Version 2.2.0
> 1.规范不同部门错误码分段并将主站错误码从common里拆出来
##### Version 2.1.0
> 1. 使用新的ecode接口去平台化
##### Version 2.0.0
> 1. 通过接口获取所有错误信息,支持全量更新和增量更新,更新时间可分别配置
> 2. 支持错误发生的堆栈信息记录,通过日志还原堆栈
##### Version 1.0.0
> 1. 通过数据库读取全量错误信息每5分钟更新一次

View File

@@ -0,0 +1,11 @@
# Owner
all
# Author
zhoujixiang
chenshangqiang
# Reviewer
maojian
lintanghui
chenzhihui

12
library/ecode/tip/OWNERS Normal file
View File

@@ -0,0 +1,12 @@
# See the OWNERS docs at https://go.k8s.io/owners
approvers:
- anyone
options:
no_parent_owners: true
reviewers:
- chenshangqiang
- chenzhihui
- lintanghui
- maojian
- zhoujixiang

View File

@@ -0,0 +1,23 @@
# go-common/business/ecode
##### 项目简介
> 1. 提供所有请求的错误码及其错误信息管理,错误信息在管理平台配置,支持自动更新
> 2. 提供错误使用规范及文档,包含堆栈信息使用
##### 编译环境
> 1. 请只用golang v1.7.x以上版本编译执行。
##### 依赖包
> 1. 依赖github.com/pkg/errors当前版本v0.8.0
##### 编译执行
> 1. 启动执行 ecode.Init(conf.Conf.Ecode)初始化ecode 配置
> 2. 配置参考 http://info.bilibili.co/pages/viewpage.action?pageId=3684076 配置
##### 测试
> 1. 执行当前目录下所有测试文件,测试所有功能
##### 特别说明
> 1. common.go 里面保存所有业务的code码当有新增加code码需求时请记得一定及时更新common,并在管理平台配置对应信息
> 2. 管理平台地址 http://apm-monitor.bilibili.co/#/codes/codeslist?name=all
> 3. 按部门给错误码分大段部门内部按业务模块继续分段具体参考info地址: http://info.bilibili.co/pages/viewpage.action?pageId=5374316

View File

@@ -0,0 +1,34 @@
package tip_test
import (
"time"
"go-common/library/ecode/tip"
xhttp "go-common/library/net/http/blademaster"
"go-common/library/net/netutil/breaker"
xtime "go-common/library/time"
)
func ExampleInit() {
conf := &tip.Config{
Domain: "172.16.33.248:6401",
Diff: xtime.Duration(5 * time.Minute),
ClientConfig: &xhttp.ClientConfig{
App: &xhttp.App{
Key: "test",
Secret: "e6c4c252dc7e3d8a90805eecd7c73396",
},
Dial: xtime.Duration(time.Millisecond * 100),
Timeout: xtime.Duration(time.Second * 2),
KeepAlive: xtime.Duration(time.Second * 2),
Breaker: &breaker.Config{
Window: xtime.Duration(time.Millisecond * 10),
Sleep: xtime.Duration(time.Second * 10),
Bucket: 10,
Ratio: 0.5,
Request: 100,
},
},
}
tip.Init(conf)
}

175
library/ecode/tip/tip.go Normal file
View File

@@ -0,0 +1,175 @@
package tip
import (
"context"
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"net/url"
"strconv"
"sync/atomic"
"time"
cmcd "go-common/library/ecode"
"go-common/library/log"
xhttp "go-common/library/net/http/blademaster"
xtime "go-common/library/time"
)
const (
_codeOk = 0
_codeNotModified = -304
_checkURL = "http://%s/x/v1/msm/codes/2"
)
var (
defualtEcodes = &ecodes{}
defaultConfig = &Config{
Domain: "api.bilibili.co",
All: xtime.Duration(time.Hour),
Diff: xtime.Duration(time.Minute * 5),
ClientConfig: &xhttp.ClientConfig{
App: &xhttp.App{
Key: "3c4e41f926e51656",
Secret: "26a2095b60c24154521d24ae62b885bb",
},
Dial: xtime.Duration(time.Second),
Timeout: xtime.Duration(time.Second),
},
}
)
// Config config.
type Config struct {
// Domain server domain
Domain string
// All get all time slice
All xtime.Duration
// Diff get diff time slice
Diff xtime.Duration
//HTTPClient httpclient config
ClientConfig *xhttp.ClientConfig
}
type ecodes struct {
codes atomic.Value
client *xhttp.Client
conf *Config
}
type res struct {
Code int `json:"code"`
Message string `json:"message"`
Data *data `json:"data"`
}
type data struct {
Ver int64
MD5 string
Code map[int]string
}
// Init init ecode.
func Init(conf *Config) {
if conf == nil {
conf = defaultConfig
} else {
panic(`请删除配置文件内无用配置perf、log、trace、report、ecode、httpServer
http://info.bilibili.co/pages/viewpage.action?pageId=3671762
`)
}
defualtEcodes.conf = conf
defualtEcodes.client = xhttp.NewClient(conf.ClientConfig)
defualtEcodes.codes.Store(make(map[int]string))
ver, _ := defualtEcodes.update(0)
go defualtEcodes.updateproc(ver)
}
func (e *ecodes) updateproc(lastVer int64) {
var (
ver int64
err error
last = time.Now()
all = time.Duration(e.conf.All)
diff = time.Duration(e.conf.Diff)
)
if e.conf.All == 0 {
all = time.Hour
}
if e.conf.Diff == 0 {
diff = 5 * time.Minute
}
time.Sleep(diff)
for {
cur := time.Now()
if cur.Sub(last) > all {
if ver, err = e.update(0); err != nil {
log.Error("e.update() error(%v)", err)
time.Sleep(10 * time.Second)
continue
}
last = cur
} else {
if ver, err = e.update(lastVer); err != nil {
log.Error("e.update(%d) error(%v)", lastVer, err)
time.Sleep(10 * time.Second)
continue
}
}
lastVer = ver
time.Sleep(diff)
}
}
func (e *ecodes) update(ver int64) (lver int64, err error) {
var (
res = &res{}
bytes []byte
params = url.Values{}
)
params.Set("ver", strconv.FormatInt(ver, 10))
if err = e.client.Get(context.TODO(), fmt.Sprintf(_checkURL, e.conf.Domain), "", params, &res); err != nil {
err = fmt.Errorf("e.client.Get(%v) error(%v)", fmt.Sprintf(_checkURL, e.conf.Domain), err)
return
}
switch res.Code {
case _codeOk:
if res.Data == nil {
err = fmt.Errorf("code get() response error result: %v", res)
return
}
case _codeNotModified:
return ver, nil
default:
err = cmcd.Int(res.Code)
return
}
if bytes, err = json.Marshal(res.Data.Code); err != nil {
return
}
mb := md5.Sum(bytes)
if res.Data.MD5 != hex.EncodeToString(mb[:]) {
err = fmt.Errorf("get codes fail,error md5")
return
}
oCodes, ok := e.codes.Load().(map[int]string)
if !ok {
return
}
nCodes := copy(oCodes)
for k, v := range res.Data.Code {
nCodes[k] = v
}
cmcd.Register(nCodes)
e.codes.Store(nCodes)
return res.Data.Ver, nil
}
func copy(src map[int]string) (dst map[int]string) {
dst = make(map[int]string)
for k1, v1 := range src {
dst[k1] = v1
}
return
}

View File

@@ -0,0 +1,49 @@
package tip
import (
"sync"
"testing"
"go-common/library/ecode"
)
var (
once sync.Once
)
func initEcodes() {
once.Do(func() {
Init(nil)
})
}
func TestInit(t *testing.T) {
initEcodes()
testCodes(t)
}
func BenchmarkLookup(b *testing.B) {
initEcodes()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
b.Logf("Ecodes: lookup ServerErr: %s", ecode.ServerErr.Message())
b.Logf("Ecodes: lookup ServerErr: %s", ecode.NotModified.Message())
}
})
}
func testCodes(t *testing.T) {
if ver, err := defualtEcodes.update(1499843315); err != nil {
t.Logf("codes(%v)", err)
t.FailNow()
} else {
t.Logf("ver(%d)", ver)
}
if codes, ok := defualtEcodes.codes.Load().(map[int]string); !ok {
t.Errorf("codes load not ok")
t.FailNow()
} else {
t.Logf("%v", codes)
}
}