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,60 @@
load(
"@io_bazel_rules_go//proto:def.bzl",
"go_proto_library",
)
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
proto_library(
name = "StreamEvent_proto",
srcs = ["StreamEvent.proto"],
tags = ["automanaged"],
deps = ["@gogo_special_proto//github.com/gogo/protobuf/gogoproto"],
)
go_proto_library(
name = "StreamEvent_go_proto",
compilers = ["@io_bazel_rules_go//proto:gogofast_grpc"],
importpath = "go-common/app/service/ops/log-agent/output/lancergrpc/lancergateway",
proto = ":StreamEvent_proto",
tags = ["automanaged"],
deps = ["@com_github_gogo_protobuf//gogoproto:go_default_library"],
)
go_library(
name = "go_default_library",
srcs = ["client.go"],
embed = [":StreamEvent_go_proto"],
importpath = "go-common/app/service/ops/log-agent/output/lancergrpc/lancergateway",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//library/naming/discovery:go_default_library",
"//library/net/rpc/warden/balancer/wrr:go_default_library",
"//library/net/rpc/warden/resolver:go_default_library",
"//library/time:go_default_library",
"@com_github_gogo_protobuf//gogoproto:go_default_library",
"@com_github_gogo_protobuf//proto:go_default_library",
"@org_golang_google_grpc//:go_default_library",
"@org_golang_x_net//context: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"],
)

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,36 @@
syntax = "proto3";
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
option java_multiple_files = true;
option java_package = "com.bilibili.gateway2.common.protobuf";
service Gateway2Server {
rpc sendList (EventList) returns (Response) {
}
rpc send (SimpleEvent) returns (Response) {
}
}
enum StatusCode {
NULL = 0;
SUCCESS = 200;
LOAD_FULL = 429;
}
message SimpleEvent {
string logId = 1;
string outerId = 2;
map<string, string> header = 3;
bytes data = 4;
}
message EventList {
repeated SimpleEvent events = 1;
}
message Response {
StatusCode code = 1;
string msg = 2;
}

View File

@@ -0,0 +1,61 @@
package lancergateway
import (
"time"
"errors"
"fmt"
"go-common/library/net/rpc/warden/resolver"
"go-common/library/net/rpc/warden/balancer/wrr"
"go-common/library/naming/discovery"
xtime "go-common/library/time"
"google.golang.org/grpc"
)
type Config struct {
AppId string `toml:"appId"`
Timeout xtime.Duration `toml:"timeout"`
Subset int `toml:"subset"`
}
func (c *Config) ConfigValidate() (error) {
if c == nil {
return errors.New("config of LancerGateway can't be nil")
}
if c.AppId == "" {
c.AppId = "datacenter.lancer.gateway2-server"
}
if c.Timeout == 0 {
c.Timeout = xtime.Duration(time.Second * 5)
}
if c.Subset == 0 {
c.Subset = 5
}
return nil
}
func init() {
resolver.Register(discovery.Builder())
}
// NewClient new member grpc client
func NewClient(c *Config) (Gateway2ServerClient, error) {
opts := []grpc.DialOption{
grpc.WithInsecure(),
grpc.WithBalancerName(wrr.Name),
}
if c.Timeout != 0 {
opts = append(opts, grpc.WithTimeout(time.Duration(c.Timeout)))
}
conn, err := grpc.Dial(fmt.Sprintf("discovery://default/%s?subset=%d", c.AppId, c.Subset), opts...)
if err != nil {
return nil, err
}
return NewGateway2ServerClient(conn), nil
}

View File

@@ -0,0 +1,135 @@
#!/bin/bash
DEFAULT_PROTOC_GEN="gogofast"
DEFAULT_PROTOC="protoc"
GO_COMMON_DIR_NAME="go-common"
USR_INCLUDE_DIR="/usr/local/include"
function _install_protoc() {
osname=$(uname -s)
echo "install protoc ..."
case $osname in
"Darwin" )
brew install protobuf
;;
*)
echo "unknown operating system, need install protobuf manual see: https://developers.google.com/protocol-buffers"
exit 1
;;
esac
}
function _install_protoc_gen() {
local protoc_gen=$1
case $protoc_gen in
"gofast" )
echo "install gofast from github.com/gogo/protobuf/protoc-gen-gofast"
go get github.com/gogo/protobuf/protoc-gen-gofast
;;
"gogofast" )
echo "install gogofast from github.com/gogo/protobuf/protoc-gen-gogofast"
go get github.com/gogo/protobuf/protoc-gen-gogofast
;;
"gogo" )
echo "install gogo from github.com/gogo/protobuf/protoc-gen-gogo"
go get github.com/gogo/protobuf/protoc-gen-gogo
;;
"go" )
echo "install protoc-gen-go from github.com/golang/protobuf"
go get github.com/golang/protobuf/{proto,protoc-gen-go}
;;
*)
echo "can't install protoc-gen-${protoc_gen} automatic !"
exit 1;
;;
esac
}
function _find_go_common_dir() {
local go_common_dir_name=$1
local current_dir=$(pwd)
while [[ "$(basename $current_dir)" != "$go_common_dir_name" ]]; do
current_dir=$(dirname $current_dir)
if [[ "$current_dir" == "/" || "$current_dir" == "." || -z "$current_dir" ]]; then
return 1
fi
done
echo $current_dir
}
function _fix_pb_file() {
local target_dir=$1
echo "fix pb file"
local pb_files=$(find $target_dir -name "*.pb.go" -type f)
local pkg_name_esc=$(echo "$target_dir" | sed 's_/_\\/_g')
for file in $pb_files; do
echo "fix pb file $file"
if [[ $(uname -s) == 'Darwin' ]]; then
sed -i "" -e "s/^import \(.*\) \"app\/\(.*\)\"/import \1 \"go-common\/app\/\2\"/g" $file
else
sed -i"" -E "s/^import\s*(.*)\s*\"app\/(.*)\"/import\1\"go-common\/app\/\2\"/g" $file
fi
done
}
function _esc_string() {
echo $(echo "$1" | sed 's_/_\\/_g')
}
function _run_protoc() {
local proto_dir=$1
local proto_files=$(find $proto_dir -maxdepth 1 -name "*.proto")
if [[ -z $proto_files ]]; then
return
fi
local protoc_cmd="$PROTOC -I$PROTO_PATH --${PROTOC_GEN}_out=plugins=grpc:. ${proto_files}"
echo $protoc_cmd
$protoc_cmd
}
if [[ -z $PROTOC ]]; then
PROTOC=${DEFAULT_PROTOC}
which $PROTOC
if [[ "$?" -ne "0" ]]; then
_install_protoc
fi
fi
if [[ -z $PROTOC_GEN ]]; then
PROTOC_GEN=${DEFAULT_PROTOC_GEN}
which protoc-gen-$PROTOC_GEN
if [[ "$?" -ne "0" ]]; then
_install_protoc_gen $PROTOC_GEN
fi
fi
GO_COMMON_DIR=$(_find_go_common_dir $GO_COMMON_DIR_NAME)
if [[ "$?" != "0" ]]; then
echo "can't find go-common directoy"
exit 1
fi
if [[ -z $PROTO_PATH ]]; then
PROTO_PATH=$GO_COMMON_DIR:$GO_COMMON_DIR/vendor:$USR_INCLUDE_DIR
else
PROTO_PATH=$PROTO_PATH:$GO_COMMON_DIR:$GO_COMMON_DIR/vendor:$USR_INCLUDE_DIR
fi
if [[ ! -z $1 ]]; then
cd $1
fi
TARGET_DIR=$(pwd)
GO_COMMON_DIR_ESC=$(_esc_string "$GO_COMMON_DIR/")
TARGET_DIR=${TARGET_DIR//$GO_COMMON_DIR_ESC/}
# switch to go_common
cd $GO_COMMON_DIR
DIRS=$(find $TARGET_DIR -type d)
for dir in $DIRS; do
echo "run protoc in $dir"
_run_protoc $dir
done
_fix_pb_file $TARGET_DIR