Skip to content

Commit

Permalink
grpcutil: extract IsGRPCRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
tamird committed Feb 17, 2016
1 parent f100245 commit a853ded
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions util/grpcutil/grpc_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,22 @@ func ListenAndServeGRPC(stopper *stop.Stopper, server *grpc.Server, addr net.Add
// connections or otherHandler otherwise.
func GRPCHandlerFunc(grpcServer *grpc.Server, otherHandler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// TODO(tamird): point to merged gRPC code rather than a PR.
// This is a partial recreation of gRPC's internal checks https://github.com/grpc/grpc-go/pull/514/files#diff-95e9a25b738459a2d3030e1e6fa2a718R61
if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
if IsGRPCRequest(r) {
grpcServer.ServeHTTP(w, r)
} else {
otherHandler.ServeHTTP(w, r)
}
})
}

// IsGRPCRequest returns true if r came from a grpc client.
//
// Its logic is a partial recreation of gRPC's internal checks, see
// https://github.com/grpc/grpc-go/blob/01de3de/transport/handler_server.go#L61:L69
func IsGRPCRequest(r *http.Request) bool {
return r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc")
}

// IsClosedConnection returns true if err is an error produced by gRPC on closed connections.
func IsClosedConnection(err error) bool {
if err == context.Canceled || err == transport.ErrConnClosing || grpc.Code(err) == codes.Canceled {
Expand Down

0 comments on commit a853ded

Please sign in to comment.