Skip to content

Commit

Permalink
Merge pull request #1 from ztelur/grpc-status
Browse files Browse the repository at this point in the history
grpc status from server to client
  • Loading branch information
LaurenceLiZhixin authored Jan 8, 2021
2 parents a9b1e1c + f0ddf36 commit 3d7aa03
Show file tree
Hide file tree
Showing 22 changed files with 727 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# and modify them (or add more) to build your codes if your project
# uses a compiled language

#- run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/github-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
go-version: ${{ matrix.go_version }}
id: go

- name: Check out code into the Go module directory
- name: Check out codes into the Go module directory
uses: actions/checkout@v2

- name: Cache dependencies
Expand Down
4 changes: 2 additions & 2 deletions filter/filter_impl/hystrix_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func TestHystricFilterInvokeCircuitBreak(t *testing.T) {
resChan <- result
}()
}
//This can not always pass the test when on travis due to concurrency, you can uncomment the code below and test it locally
//This can not always pass the test when on travis due to concurrency, you can uncomment the codes below and test it locally

//var lastRest bool
//for i := 0; i < 50; i++ {
Expand Down Expand Up @@ -215,7 +215,7 @@ func TestHystricFilterInvokeCircuitBreakOmitException(t *testing.T) {
resChan <- result
}()
}
//This can not always pass the test when on travis due to concurrency, you can uncomment the code below and test it locally
//This can not always pass the test when on travis due to concurrency, you can uncomment the codes below and test it locally

//time.Sleep(time.Second * 6)
//var lastRest bool
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ require (
go.uber.org/atomic v1.6.0
go.uber.org/zap v1.16.0
golang.org/x/net v0.0.0-20200822124328-c89045814202
google.golang.org/genproto v0.0.0-20191216164720-4f79533eabd1
google.golang.org/grpc v1.26.0
gopkg.in/yaml.v2 v2.2.8
k8s.io/api v0.16.9
Expand Down
2 changes: 1 addition & 1 deletion metadata/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type MetadataService interface {
SubscribeURL(url *common.URL) (bool, error)
// UnsubscribeURL will delete the subscribed url in metadata
UnsubscribeURL(url *common.URL) error
// PublishServiceDefinition will generate the target url's code info
// PublishServiceDefinition will generate the target url's codes info
PublishServiceDefinition(url *common.URL) error
// GetExportedURLs will get the target exported url in metadata
// the url should be unique
Expand Down
2 changes: 1 addition & 1 deletion protocol/dubbo/hessian2/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ const (

/**
* the dubbo protocol header length is 16 Bytes.
* the first 2 Bytes is magic code '0xdabb'
* the first 2 Bytes is magic codes '0xdabb'
* the next 1 Byte is message flags, in which its 16-20 bit is serial id, 21 for event, 22 for two way, 23 for request/response flag
* the next 1 Bytes is response state.
* the next 8 Bytes is package DI.
Expand Down
2 changes: 1 addition & 1 deletion protocol/dubbo/impl/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ const (

/**
* the dubbo protocol header length is 16 Bytes.
* the first 2 Bytes is magic code '0xdabb'
* the first 2 Bytes is magic codes '0xdabb'
* the next 1 Byte is message flags, in which its 16-20 bit is serial id, 21 for event, 22 for two way, 23 for request/response flag
* the next 1 Bytes is response state.
* the next 8 Bytes is package DI.
Expand Down
14 changes: 12 additions & 2 deletions protocol/dubbo3/impl/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type TripleHeader struct {
TracingRPCID string
TracingContext string
ClusterInfo string
GrpcStatus string
GrpcMessage string
}

func (t *TripleHeader) GetMethod() string {
Expand All @@ -65,6 +67,9 @@ func (t *TripleHeader) FieldToCtx() context.Context {
ctx = context.WithValue(ctx, "tri-trace-rpcid", t.TracingRPCID)
ctx = context.WithValue(ctx, "tri-trace-proto-bin", t.TracingContext)
ctx = context.WithValue(ctx, "tri-unit-info", t.ClusterInfo)
ctx = context.WithValue(ctx, "grpc-status", t.GrpcStatus)
ctx = context.WithValue(ctx, "grpc-message", t.GrpcMessage)

return ctx
}

Expand Down Expand Up @@ -94,6 +99,9 @@ func (t TripleHeaderHandler) WriteHeaderField(url *common.URL, ctx context.Conte
headerFields = append(headerFields, hpack.HeaderField{Name: "tri-trace-rpcid", Value: getCtxVaSave(ctx, "tri-trace-rpcid")})
headerFields = append(headerFields, hpack.HeaderField{Name: "tri-trace-proto-bin", Value: getCtxVaSave(ctx, "tri-trace-proto-bin")})
headerFields = append(headerFields, hpack.HeaderField{Name: "tri-unit-info", Value: getCtxVaSave(ctx, "tri-unit-info")})
headerFields = append(headerFields, hpack.HeaderField{Name: "grpc-status", Value: getCtxVaSave(ctx, "grpc-status")})
headerFields = append(headerFields, hpack.HeaderField{Name: "grpc-message", Value: getCtxVaSave(ctx, "grpc-message")})

return headerFields
}

Expand Down Expand Up @@ -132,8 +140,10 @@ func (t TripleHeaderHandler) ReadFromH2MetaHeader(frame *http2.MetaHeadersFrame)
tripleHeader.Method = f.Value
// todo: usage of these part of fields needs to be discussed later
//case "grpc-encoding":
//case "grpc-status":
//case "grpc-message":
case "grpc-status":
tripleHeader.GrpcStatus = f.Value
case "grpc-message":
tripleHeader.GrpcMessage = f.Value
//case "grpc-status-details-bin":
//case "grpc-timeout":
//case ":status":
Expand Down
14 changes: 7 additions & 7 deletions protocol/dubbo3/protoc-gen-dubbo3/plugin/dubbo3/dubbo3.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ import (
"github.com/golang/protobuf/protoc-gen-go/generator"
)

// generatedCodeVersion indicates a version of the generated code.
// It is incremented whenever an incompatibility between the generated code and
// the grpc package is introduced; the generated code references
// generatedCodeVersion indicates a version of the generated codes.
// It is incremented whenever an incompatibility between the generated codes and
// the grpc package is introduced; the generated codes references
// a constant, grpc.SupportPackageIsVersionN (where N is generatedCodeVersion).
const generatedCodeVersion = 4

// Paths for packages used by code generated in this file,
// Paths for packages used by codes generated in this file,
// relative to the import_prefix of the generator.Generator.
const (
contextPkgPath = "context"
Expand All @@ -58,7 +58,7 @@ func (g *dubboGrpc) Name() string {
return "dubbo"
}

// The names for packages imported in the generated code.
// The names for packages imported in the generated codes.
// They may vary from the final path component of the import path
// if the name is used by other packages.
var (
Expand Down Expand Up @@ -86,7 +86,7 @@ func (g *dubboGrpc) typeName(str string) string {
// P forwards to g.gen.P.
func (g *dubboGrpc) P(args ...interface{}) { g.gen.P(args...) }

// Generate generates code for the services in the given file.
// Generate generates codes for the services in the given file.
// be consistent with grpc plugin
func (g *dubboGrpc) Generate(file *generator.FileDescriptor) {
if len(file.FileDescriptorProto.Service) == 0 {
Expand Down Expand Up @@ -117,7 +117,7 @@ func unexport(s string) string { return strings.ToLower(s[:1]) + s[1:] }
// messages, fields, enums, and enum values.
var deprecationComment = "// Deprecated: Do not use."

// generateService generates all the code for the named service.
// generateService generates all the codes for the named service.
func (g *dubboGrpc) generateService(file *generator.FileDescriptor, service *pb.ServiceDescriptorProto, index int) {
path := fmt.Sprintf("6,%d", index) // 6 means service.

Expand Down
2 changes: 1 addition & 1 deletion protocol/grpc/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (g *greeterProvider) Reference() string {
return "GrpcGreeterImpl"
}

// code generated by greeter.go
// codes generated by greeter.go
type greeterProviderBase struct {
proxyImpl protocol.Invoker
}
Expand Down
14 changes: 7 additions & 7 deletions protocol/grpc/protoc-gen-dubbo/plugin/dubbo/dubbo.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ import (
"github.com/golang/protobuf/protoc-gen-go/generator"
)

// generatedCodeVersion indicates a version of the generated code.
// It is incremented whenever an incompatibility between the generated code and
// the grpc package is introduced; the generated code references
// generatedCodeVersion indicates a version of the generated codes.
// It is incremented whenever an incompatibility between the generated codes and
// the grpc package is introduced; the generated codes references
// a constant, grpc.SupportPackageIsVersionN (where N is generatedCodeVersion).
const generatedCodeVersion = 4

// Paths for packages used by code generated in this file,
// Paths for packages used by codes generated in this file,
// relative to the import_prefix of the generator.Generator.
const (
contextPkgPath = "context"
Expand All @@ -58,7 +58,7 @@ func (g *dubboGrpc) Name() string {
return "dubbo"
}

// The names for packages imported in the generated code.
// The names for packages imported in the generated codes.
// They may vary from the final path component of the import path
// if the name is used by other packages.
var (
Expand Down Expand Up @@ -86,7 +86,7 @@ func (g *dubboGrpc) typeName(str string) string {
// P forwards to g.gen.P.
func (g *dubboGrpc) P(args ...interface{}) { g.gen.P(args...) }

// Generate generates code for the services in the given file.
// Generate generates codes for the services in the given file.
// be consistent with grpc plugin
func (g *dubboGrpc) Generate(file *generator.FileDescriptor) {
if len(file.FileDescriptorProto.Service) == 0 {
Expand Down Expand Up @@ -116,7 +116,7 @@ func unexport(s string) string { return strings.ToLower(s[:1]) + s[1:] }
// messages, fields, enums, and enum values.
var deprecationComment = "// Deprecated: Do not use."

// generateService generates all the code for the named service.
// generateService generates all the codes for the named service.
func (g *dubboGrpc) generateService(file *generator.FileDescriptor, service *pb.ServiceDescriptorProto, index int) {
path := fmt.Sprintf("6,%d", index) // 6 means service.

Expand Down
2 changes: 1 addition & 1 deletion protocol/jsonrpc/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestHTTPClientCall(t *testing.T) {
reply = &User{}
err = client.Call(ctx, url, req, reply)
assert.True(t, strings.Contains(err.Error(), "500 Internal Server Error"))
assert.True(t, strings.Contains(err.Error(), "\\\"result\\\":{},\\\"error\\\":{\\\"code\\\":-32000,\\\"message\\\":\\\"error\\\"}"))
assert.True(t, strings.Contains(err.Error(), "\\\"result\\\":{},\\\"error\\\":{\\\"codes\\\":-32000,\\\"message\\\":\\\"error\\\"}"))

// call GetUser2
ctx = context.WithValue(context.Background(), constant.DUBBOGO_CTX_KEY, map[string]string{
Expand Down
6 changes: 3 additions & 3 deletions protocol/jsonrpc/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const (

// Error response Error
type Error struct {
Code int `json:"code"`
Code int `json:"codes"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
}
Expand All @@ -72,7 +72,7 @@ func (e *Error) Error() string {
if retryErr != nil {
msg = []byte("jsonrpc2.Error: json.Marshal failed")
}
return fmt.Sprintf(`{"code":%d,"message":%s}`, -32001, string(msg))
return fmt.Sprintf(`{"codes":%d,"message":%s}`, -32001, string(msg))
}
return string(buf)
}
Expand Down Expand Up @@ -362,7 +362,7 @@ func (c *ServerCodec) ReadBody(x interface{}) error {
return nil
}

// NewError creates a error with @code and @message
// NewError creates a error with @codes and @message
func NewError(code int, message string) *Error {
return &Error{Code: code, Message: message}
}
Expand Down
12 changes: 6 additions & 6 deletions protocol/jsonrpc/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func TestJsonClientCodecRead(t *testing.T) {

//error
codec.pending[1] = "GetUser"
err = codec.Read([]byte("{\"jsonrpc\":\"2.0\",\"id\":1,\"error\":{\"code\":-32000,\"message\":\"error\"}}\n"), rsp)
assert.EqualError(t, err, "{\"code\":-32000,\"message\":\"error\"}")
err = codec.Read([]byte("{\"jsonrpc\":\"2.0\",\"id\":1,\"error\":{\"codes\":-32000,\"message\":\"error\"}}\n"), rsp)
assert.EqualError(t, err, "{\"codes\":-32000,\"message\":\"error\"}")
}

func TestServerCodecWrite(t *testing.T) {
Expand All @@ -66,18 +66,18 @@ func TestServerCodecWrite(t *testing.T) {
codec.req = serverRequest{Version: "1.0", Method: "GetUser", ID: &a}
data, err := codec.Write("error", &TestData{Test: "test"})
assert.NoError(t, err)
assert.Equal(t, "{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":{\"Test\":\"test\"},\"error\":{\"code\":-32000,\"message\":\"error\"}}\n", string(data))
assert.Equal(t, "{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":{\"Test\":\"test\"},\"error\":{\"codes\":-32000,\"message\":\"error\"}}\n", string(data))

data, err = codec.Write("{\"code\":-32000,\"message\":\"error\"}", &TestData{Test: "test"})
data, err = codec.Write("{\"codes\":-32000,\"message\":\"error\"}", &TestData{Test: "test"})
assert.NoError(t, err)
assert.Equal(t, "{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":{\"Test\":\"test\"},\"error\":{\"code\":-32000,\"message\":\"error\"}}\n", string(data))
assert.Equal(t, "{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":{\"Test\":\"test\"},\"error\":{\"codes\":-32000,\"message\":\"error\"}}\n", string(data))
}

func TestServerCodecRead(t *testing.T) {
codec := newServerCodec()
header := map[string]string{}
err := codec.ReadHeader(header, []byte("{\"jsonrpc\":\"2.0\",\"method\":\"GetUser\",\"params\":[\"args\",2],\"id\":1}\n"))
assert.EqualError(t, err, "{\"code\":-32601,\"message\":\"Method not found\"}")
assert.EqualError(t, err, "{\"codes\":-32601,\"message\":\"Method not found\"}")

header["HttpMethod"] = "POST"
err = codec.ReadHeader(header, []byte("{\"jsonrpc\":\"2.0\",\"method\":\"GetUser\",\"params\":[\"args\",2],\"id\":1}\n"))
Expand Down
Loading

0 comments on commit 3d7aa03

Please sign in to comment.