Skip to content
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

[WIP] feat: support gRPC reflection service #1351

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/server/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ type Options struct {

RefuseTrafficWithoutServiceName bool
EnableContextTimeout bool

// Reflection
EnableGrpcReflection bool
}

type Limit struct {
Expand Down
53 changes: 53 additions & 0 deletions pkg/reflection/grpc/adapt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2024 CloudWeGo 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 grpc

import (
"github.com/cloudwego/kitex/pkg/reflection/grpc/handler"
v1 "github.com/cloudwego/kitex/pkg/reflection/grpc/reflection/v1"
"github.com/cloudwego/kitex/pkg/reflection/grpc/reflection/v1alpha"
)

// asV1Alpha returns an implementation of the v1alpha version of the reflection
// interface that delegates all calls to the given v1 version.
func asV1Alpha(svr v1.ServerReflection) v1alpha.ServerReflection {
return v1AlphaServerImpl{svr: svr}
}

type v1AlphaServerImpl struct {
svr v1.ServerReflection
}

func (s v1AlphaServerImpl) ServerReflectionInfo(stream v1alpha.ServerReflection_ServerReflectionInfoServer) error {
return s.svr.ServerReflectionInfo(v1AlphaServerStreamAdapter{stream})
}

type v1AlphaServerStreamAdapter struct {
v1alpha.ServerReflection_ServerReflectionInfoServer
}

func (s v1AlphaServerStreamAdapter) Send(response *v1.ServerReflectionResponse) error {
return s.ServerReflection_ServerReflectionInfoServer.Send(handler.V1ToV1AlphaResponse(response))
}

func (s v1AlphaServerStreamAdapter) Recv() (*v1.ServerReflectionRequest, error) {
resp, err := s.ServerReflection_ServerReflectionInfoServer.Recv()
if err != nil {
return nil, err
}
return handler.V1AlphaToV1Request(resp), nil
}
40 changes: 40 additions & 0 deletions pkg/reflection/grpc/grpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2024 CloudWeGo 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 grpc

import (
"github.com/cloudwego/kitex/pkg/reflection/grpc/handler"
"github.com/cloudwego/kitex/pkg/reflection/grpc/reflection/v1"
reflection_v1_service "github.com/cloudwego/kitex/pkg/reflection/grpc/reflection/v1/serverreflection"
"github.com/cloudwego/kitex/pkg/reflection/grpc/reflection/v1alpha"
reflection_v1alpha_service "github.com/cloudwego/kitex/pkg/reflection/grpc/reflection/v1alpha/serverreflection"
"github.com/cloudwego/kitex/pkg/serviceinfo"
"google.golang.org/protobuf/reflect/protoregistry"
)

var (
NewV1ServiceInfo = reflection_v1_service.NewServiceInfo
NewV1AlphaServiceInfo = reflection_v1alpha_service.NewServiceInfo
)

func NewV1Handler(svcs map[string]*serviceinfo.ServiceInfo) v1.ServerReflection {
return handler.NewServerReflection(svcs, protoregistry.GlobalFiles, protoregistry.GlobalTypes)
}

func NewV1alphaHandler(svcs map[string]*serviceinfo.ServiceInfo) v1alpha.ServerReflection {
return asV1Alpha(NewV1Handler(svcs))
}
Loading
Loading