-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove call to proto.Clone() in http2Server.WriteStatus. #2842
Changes from 13 commits
36ed5dd
dffc6d6
a92e19d
e51c0c3
65e8212
44d0c79
8ffe677
1c29ad5
f5d99a4
57e1ed6
8b06ff0
3e49ea2
57b05be
e2bdc77
a775841
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* | ||
* Copyright 2019 gRPC authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package testutils | ||
|
||
import ( | ||
"github.com/gogo/protobuf/proto" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
// StatusErrEqual returns true iff both err1 and err2 wrap status.Status errors | ||
// and their underlying status protos are equal. | ||
func StatusErrEqual(err1, err2 error) bool { | ||
status1, ok := status.FromError(err1) | ||
if !ok { | ||
return false | ||
} | ||
status2, ok := status.FromError(err2) | ||
if !ok { | ||
return false | ||
} | ||
return proto.Equal(status1.Proto(), status2.Proto()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* | ||
* Copyright 2019 gRPC authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package testutils | ||
|
||
import ( | ||
"testing" | ||
|
||
anypb "github.com/golang/protobuf/ptypes/any" | ||
spb "google.golang.org/genproto/googleapis/rpc/status" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
var statusErr = status.ErrorProto(&spb.Status{ | ||
Code: int32(codes.DataLoss), | ||
Message: "error for testing", | ||
Details: []*anypb.Any{{ | ||
TypeUrl: "url", | ||
Value: []byte{6, 0, 0, 6, 1, 3}, | ||
}}, | ||
}) | ||
|
||
func TestStatusErrEqual(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
err1 error | ||
err2 error | ||
wantEqual bool | ||
}{ | ||
{"nil errors", nil, nil, true}, | ||
{"equal OK status", status.New(codes.OK, "").Err(), status.New(codes.OK, "").Err(), true}, | ||
{"equal status errors", statusErr, statusErr, true}, | ||
{"different status errors", statusErr, status.New(codes.OK, "").Err(), false}, | ||
} | ||
|
||
for _, test := range tests { | ||
if gotEqual := StatusErrEqual(test.err1, test.err2); gotEqual != test.wantEqual { | ||
t.Errorf("%v: StatusErrEqual(%v, %v) = %v, want %v", test.name, test.err1, test.err2, gotEqual, test.wantEqual) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,9 +35,11 @@ import ( | |
"golang.org/x/net/http2" | ||
"golang.org/x/net/http2/hpack" | ||
|
||
spb "google.golang.org/genproto/googleapis/rpc/status" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/credentials" | ||
"google.golang.org/grpc/grpclog" | ||
"google.golang.org/grpc/internal" | ||
"google.golang.org/grpc/internal/channelz" | ||
"google.golang.org/grpc/internal/grpcrand" | ||
"google.golang.org/grpc/keepalive" | ||
|
@@ -817,7 +819,8 @@ func (t *http2Server) WriteStatus(s *Stream, st *status.Status) error { | |
headerFields = append(headerFields, hpack.HeaderField{Name: "grpc-status", Value: strconv.Itoa(int(st.Code()))}) | ||
headerFields = append(headerFields, hpack.HeaderField{Name: "grpc-message", Value: encodeGrpcMessage(st.Message())}) | ||
|
||
if p := st.Proto(); p != nil && len(p.Details) > 0 { | ||
srp := internal.StatusRawProto.(func(*status.Status) *spb.Status) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be (a tiny bit) better to declare this globally and avoid the type assertion every time status is written: var statusRawProto = internal.StatusRawProto.(func(*status.Status) *spb.Status) (Also it would fail at init time if the types are wrong, instead of runtime, which is also a win.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Thanks. |
||
if p := srp(st); p != nil && len(p.Details) > 0 { | ||
stBytes, err := proto.Marshal(p) | ||
if err != nil { | ||
// TODO: return error instead, when callers are able to handle it. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not use gogo/proto
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Thanks.
Some of the tests are unusually flaky today. Unable to make travis happy as of now.