Skip to content

Commit c20a79e

Browse files
committed
Init repo
1 parent 8312f40 commit c20a79e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+3950
-0
lines changed

BUILD.bazel

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load("@io_bazel_rules_go//go:def.bzl", "go_library")
4+
5+
go_library(
6+
name = "go_default_library",
7+
srcs = [
8+
"init.go",
9+
],
10+
importpath = "github.com/binchencoder/letsgo",
11+
deps = [
12+
"@binchencoder_letsgo/log:go_default_library",
13+
"@binchencoder_letsgo/runtime:go_default_library",
14+
"@binchencoder_letsgo/version:go_default_library",
15+
],
16+
)

GOMOD.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 使用Go mod 管理依赖模块
2+
3+
`module`是一个相关`Go`包的集合,它是源代码更替和版本控制的单元。模块由源文件形成的`go.mod`文件的根目录定义,包含go.mod文件的目录也被称为模块根。
4+
5+
`moudles`取代旧的的基于`GOPATH`方法来指定在工程中使用哪些源文件或导入包。模块路径是导入包的路径前缀,`go.mod`文件定义模块路径,并且列出了在项目构建过程中使用的特定版本。
6+
7+
## 使用步骤
8+
9+
1. 首先将你的版本更新到最新的Go版本1.11,如何更新版本可以自行百度。
10+
2. 通过go命令行,进入到你当前的工程目录下,在命令行设置临时环境变量set GO111MODULE=on;
11+
3. 执行命令`go mod init`在当前目录下生成一个go.mod文件,执行这条命令时,当前目录不能存在go.mod文件。如果之前生成过,要先删除;
12+
4. 如果你工程中存在一些不能确定版本的包,那么生成的go.mod文件可能就不完整,因此继续执行下面的命令;
13+
5. 执行`go mod tidy`命令,它会添加缺失的模块以及移除不需要的模块。执行后会生成go.sum文件(模块下载条目)。添加参数-v,例如`go mod tidy -v`可以将执行的信息,即删除和添加的包打印到命令行;
14+
6. 执行命令`go mod verify`来检查当前模块的依赖是否全部下载下来,是否下载下来被修改过。如果所有的模块都没有被修改过,那么执行这条命令之后,会打印all modules verified。
15+
7. 执行命令`go mod vendor`生成vendor文件夹,该文件夹下将会放置你go.mod文件描述的依赖包,文件夹下同时还有一个文件modules.txt,它是你整个工程的所有模块。在执行这条命令之前,如果你工程之前有vendor目录,应该先进行删除。同理`go mod vendor -v`会将添加到vendor中的模块打印出来;

WORKSPACE

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
workspace(name = "binchencoder_letsgo")
2+
3+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
4+
5+
http_archive(
6+
name = "io_bazel_rules_go",
7+
sha256 = "6776d68ebb897625dead17ae510eac3d5f6342367327875210df44dbe2aeeb19",
8+
urls = ["https://github.com/bazelbuild/rules_go/releases/download/0.17.1/rules_go-0.17.1.tar.gz"],
9+
)
10+
11+
http_archive(
12+
name = "bazel_gazelle",
13+
sha256 = "3c681998538231a2d24d0c07ed5a7658cb72bfb5fd4bf9911157c0e9ac6a2687",
14+
urls = ["https://github.com/bazelbuild/bazel-gazelle/releases/download/0.17.0/bazel-gazelle-0.17.0.tar.gz"],
15+
)
16+
17+
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
18+
19+
go_rules_dependencies()
20+
21+
go_register_toolchains()
22+
23+
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
24+
25+
gazelle_dependencies()
26+
27+
go_repository(
28+
name = "binchencoder_ease_gateway",
29+
commit = "9bf12233cd53f3be24b50c8d408ad1f5f11f6671",
30+
importpath = "github.com/binchencoder/ease-gateway",
31+
)
32+
33+
go_repository(
34+
name = "binchencoder_third_party_go",
35+
commit = "a99e7b8104bcb76ed9cc7a29c87dbc246dc6329c",
36+
importpath = "github.com/binchencoder/third-party-go",
37+
)
38+
39+
go_repository(
40+
name = "grpc_ecosystem_grpc_gateway",
41+
commit = "d63917fcb0d53f39184485b9b6a0893af18a5668",
42+
importpath = "github.com/grpc-ecosystem/grpc-gateway",
43+
)
44+
45+
go_repository(
46+
name = "com_github_fatih_color",
47+
commit = "3f9d52f7176a6927daacff70a3e8d1dc2025c53e",
48+
importpath = "github.com/fatih/color",
49+
)
50+
51+
go_repository(
52+
name = "com_github_klauspost_compress",
53+
commit = "ae52aff18558bd92cbe681549bfe9e8cbffd5903",
54+
importpath = "github.com/klauspost/compress",
55+
)
56+
57+
go_repository(
58+
name = "com_github_klauspost_cpuid",
59+
commit = "05a8198c0f5a27739aec358908d7e12c64ce6eb7",
60+
importpath = "github.com/klauspost/cpuid",
61+
)
62+
63+
go_repository(
64+
name = "com_github_golang_net",
65+
commit = "4829fb13d2c62012c17688fa7f629f371014946d",
66+
importpath = "github.com/golang/net",
67+
)
68+
69+
# Also define in Gopkg.toml
70+
go_repository(
71+
name = "org_golang_google_genproto",
72+
commit = "383e8b2c3b9e36c4076b235b32537292176bae20",
73+
importpath = "google.golang.org/genproto",
74+
)
75+
76+
# Also define in Gopkg.toml
77+
go_repository(
78+
name = "com_github_rogpeppe_fastuuid",
79+
commit = "6724a57986aff9bff1a1770e9347036def7c89f6",
80+
importpath = "github.com/rogpeppe/fastuuid",
81+
)
82+
83+
# Also define in Gopkg.toml
84+
go_repository(
85+
name = "in_gopkg_resty_v1",
86+
commit = "fa5875c0caa5c260ab78acec5a244215a730247f",
87+
importpath = "gopkg.in/resty.v1",
88+
)
89+
90+
# Also define in Gopkg.toml
91+
go_repository(
92+
name = "com_github_ghodss_yaml",
93+
commit = "0ca9ea5df5451ffdf184b4428c902747c2c11cd7",
94+
importpath = "github.com/ghodss/yaml",
95+
)
96+
97+
# Also define in Gopkg.toml
98+
go_repository(
99+
name = "in_gopkg_yaml_v2",
100+
commit = "eb3733d160e74a9c7e442f435eb3bea458e1d19f",
101+
importpath = "gopkg.in/yaml.v2",
102+
)
103+
104+
go_repository(
105+
name = "com_github_bazelbuild_buildtools",
106+
importpath = "github.com/bazelbuild/buildtools",
107+
commit = "36bd730dfa67bff4998fe897ee4bbb529cc9fbee",
108+
)
109+
110+
load("@com_github_bazelbuild_buildtools//buildifier:deps.bzl", "buildifier_dependencies")
111+
112+
buildifier_dependencies()

grpc/BUILD.bazel

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
4+
5+
go_library(
6+
name = "go_default_library",
7+
srcs = glob(
8+
["*.go"],
9+
exclude = ["*_test.go"],
10+
),
11+
importpath = "github.com/binchencoder/letsgo/grpc",
12+
deps = [
13+
"@binchencoder_letsgo//hashring:go_default_library",
14+
"@binchencoder_letsgo//ident:go_default_library",
15+
"@binchencoder_letsgo//trace:go_default_library",
16+
"@binchencoder_third_party_go//vendor/github.com/cenkalti/backoff:go_default_library",
17+
"@binchencoder_third_party_go//vendor/github.com/golang/glog:go_default_library",
18+
"@binchencoder_third_party_go//vendor/github.com/golang/protobuf/jsonpb:go_default_library",
19+
"@binchencoder_third_party_go//vendor/github.com/opentracing/opentracing-go:go_default_library",
20+
"@binchencoder_third_party_go//vendor/github.com/uber/jaeger-client-go:go_default_library",
21+
"@binchencoder_third_party_go//vendor/github.com/uber/jaeger-client-go/config:go_default_library",
22+
"@binchencoder_third_party_go//vendor/github.com/uber/jaeger-client-go/rpcmetrics:go_default_library",
23+
"@binchencoder_third_party_go//vendor/github.com/uber/jaeger-lib/metrics/go-kit:go_default_library",
24+
"@binchencoder_third_party_go//vendor/github.com/uber/jaeger-lib/metrics/go-kit/expvar:go_default_library",
25+
"@binchencoder_third_party_go//vendor/golang.org/x/net/context:go_default_library",
26+
"@binchencoder_third_party_go//vendor/google.golang.org/grpc:go_default_library",
27+
"@binchencoder_third_party_go//vendor/google.golang.org/grpc/codes:go_default_library",
28+
"@binchencoder_third_party_go//vendor/google.golang.org/grpc/metadata:go_default_library",
29+
"@binchencoder_third_party_go//vendor/google.golang.org/grpc/status:go_default_library",
30+
"@binchencoder_ease_gateway//proto/data:go_default_library",
31+
"@binchencoder_ease_gateway//proto/frontend:go_default_library",
32+
],
33+
)
34+
35+
go_test(
36+
name = "tests",
37+
size = "small",
38+
srcs = glob(["*_test.go"]),
39+
embed = [
40+
":go_default_library",
41+
],
42+
deps = [
43+
"@binchencoder_letsgo//hashring:go_default_library",
44+
"@binchencoder_letsgo//ident:go_default_library",
45+
"@binchencoder_letsgo//trace:go_default_library",
46+
"@binchencoder_third_party_go//vendor/github.com/cenkalti/backoff:go_default_library",
47+
"@binchencoder_third_party_go//vendor/golang.org/x/net/context:go_default_library",
48+
"@binchencoder_third_party_go//vendor/google.golang.org/grpc:go_default_library",
49+
"@binchencoder_third_party_go//vendor/google.golang.org/grpc/codes:go_default_library",
50+
"@binchencoder_third_party_go//vendor/google.golang.org/grpc/metadata:go_default_library",
51+
"@binchencoder_ease_gateway//proto/data:go_default_library",
52+
"@binchencoder_ease_gateway//proto/frontend:go_default_library",
53+
],
54+
)

grpc/backoff.go

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package grpc
2+
3+
import (
4+
"time"
5+
6+
"github.com/cenkalti/backoff"
7+
"golang.org/x/net/context"
8+
"google.golang.org/grpc"
9+
)
10+
11+
type expBackOffKey struct{}
12+
13+
// ExpBackoffUnaryClientInterceptor is a gRPC client-side interceptor that
14+
// provides retry with backoff for unary RPCs.
15+
func ExpBackoffUnaryClientInterceptor(ctx context.Context, method string, req, reply interface{},
16+
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
17+
18+
if ebo := GetExpBackOff(ctx); ebo != nil {
19+
op := func() error {
20+
err := invoker(ctx, method, req, reply, cc, opts...)
21+
// Either nil or PermanentError will stop the retry.
22+
return err
23+
}
24+
25+
return backoff.Retry(op, ebo)
26+
}
27+
28+
return invoker(ctx, method, req, reply, cc, opts...)
29+
}
30+
31+
// WithExpBackOff returns a copy of parent context with the given
32+
// ExponentialBackOff.
33+
func WithExpBackOff(parent context.Context, ebo *backoff.ExponentialBackOff) context.Context {
34+
return context.WithValue(parent, expBackOffKey{}, ebo)
35+
}
36+
37+
// GetExpBackOff returns ExponentialBackOff from context.
38+
func GetExpBackOff(ctx context.Context) *backoff.ExponentialBackOff {
39+
ebo, ok := ctx.Value(expBackOffKey{}).(*backoff.ExponentialBackOff)
40+
if !ok {
41+
return nil
42+
}
43+
44+
return ebo
45+
}
46+
47+
// GetCustomizedExpBackOff returns a customized ExponentialBackOff.
48+
func GetCustomizedExpBackOff(initialInterval, maxInterval, maxElapsedTime time.Duration,
49+
randFactor, multiplier float64) *backoff.ExponentialBackOff {
50+
51+
b := &backoff.ExponentialBackOff{
52+
InitialInterval: initialInterval,
53+
RandomizationFactor: randFactor,
54+
Multiplier: multiplier,
55+
MaxInterval: maxInterval,
56+
MaxElapsedTime: maxElapsedTime,
57+
Clock: backoff.SystemClock,
58+
}
59+
b.Reset()
60+
return b
61+
}
62+
63+
// GetFastExpBackOff returns an ExponentialBackOff for fast requests.
64+
// For latency of 30ms request, this will do max 3~4 retries, with max latency
65+
// of 250~380ms.
66+
func GetFastExpBackOff() *backoff.ExponentialBackOff {
67+
b := &backoff.ExponentialBackOff{
68+
InitialInterval: 50 * time.Millisecond,
69+
RandomizationFactor: 0.5,
70+
Multiplier: 2,
71+
MaxInterval: 150 * time.Millisecond,
72+
MaxElapsedTime: 250 * time.Millisecond,
73+
Clock: backoff.SystemClock,
74+
}
75+
b.Reset()
76+
return b
77+
}
78+
79+
// GetMediumExpBackOff returns an ExponentialBackOff for medium speed requests.
80+
// For latency of 100ms request, this will do max 5 retries, with max latency
81+
// of 1~1.5s.
82+
func GetMediumExpBackOff() *backoff.ExponentialBackOff {
83+
b := &backoff.ExponentialBackOff{
84+
InitialInterval: 100 * time.Millisecond,
85+
RandomizationFactor: 0.5,
86+
Multiplier: 1.5,
87+
MaxInterval: 500 * time.Millisecond,
88+
MaxElapsedTime: 1 * time.Second,
89+
Clock: backoff.SystemClock,
90+
}
91+
b.Reset()
92+
return b
93+
}
94+
95+
// GetSlowExpBackOff returns an ExponentialBackOff for slow requests.
96+
// For latency of 300ms request, this will do max 8-9 retries, with max latency
97+
// of 10-14s.
98+
func GetSlowExpBackOff() *backoff.ExponentialBackOff {
99+
b := &backoff.ExponentialBackOff{
100+
InitialInterval: 100 * time.Millisecond,
101+
RandomizationFactor: 0.5,
102+
Multiplier: 2,
103+
MaxInterval: 3 * time.Second,
104+
MaxElapsedTime: 10 * time.Second,
105+
Clock: backoff.SystemClock,
106+
}
107+
b.Reset()
108+
return b
109+
}

0 commit comments

Comments
 (0)