mirror of
https://github.com/linka-cloud/grpc.git
synced 2025-06-22 17:22:26 +00:00
add grpc-proxy (github.com/mwitkow/grpc-proxy)
Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
9
proxy/testservice/Makefile
Normal file
9
proxy/testservice/Makefile
Normal file
@ -0,0 +1,9 @@
|
||||
|
||||
all: test_go
|
||||
|
||||
test_go: test.proto
|
||||
protoc --go_out=. --go_opt=paths=source_relative \
|
||||
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
|
||||
test.proto
|
||||
|
||||
|
168
proxy/testservice/ping.go
Normal file
168
proxy/testservice/ping.go
Normal file
@ -0,0 +1,168 @@
|
||||
package testservice
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"golang.org/x/sync/errgroup"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"google.golang.org/grpc/status"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
)
|
||||
|
||||
var DefaultTestServiceServer = defaultPingServer{}
|
||||
|
||||
const (
|
||||
PingHeader = "ping-header"
|
||||
PingHeaderCts = "Arbitrary header text"
|
||||
PingTrailer = "ping-trailer"
|
||||
PingTrailerCts = "Arbitrary trailer text"
|
||||
PingEchoHeader = "ping-echo-header"
|
||||
PingEchoTrailer = "ping-echo-trailer"
|
||||
)
|
||||
|
||||
// defaultPingServer is the canonical implementation of a TestServiceServer.
|
||||
type defaultPingServer struct {
|
||||
UnsafeTestServiceServer
|
||||
}
|
||||
|
||||
func (s defaultPingServer) PingEmpty(ctx context.Context, empty *emptypb.Empty) (*PingResponse, error) {
|
||||
if err := s.sendHeader(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.setTrailer(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &PingResponse{}, nil
|
||||
}
|
||||
|
||||
func (s defaultPingServer) Ping(ctx context.Context, request *PingRequest) (*PingResponse, error) {
|
||||
if err := s.sendHeader(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.setTrailer(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &PingResponse{Value: request.Value}, nil
|
||||
}
|
||||
|
||||
func (s defaultPingServer) PingError(ctx context.Context, request *PingRequest) (*emptypb.Empty, error) {
|
||||
if err := s.sendHeader(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.setTrailer(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, status.Error(codes.Unknown, "Something is wrong and this is a message that describes it")
|
||||
}
|
||||
|
||||
func (s defaultPingServer) PingList(request *PingRequest, server TestService_PingListServer) error {
|
||||
if err := s.sendHeader(server.Context()); err != nil {
|
||||
return err
|
||||
}
|
||||
s.setStreamTrailer(server)
|
||||
for i := 0; i < 10; i++ {
|
||||
if err := server.Send(&PingResponse{
|
||||
Value: request.Value,
|
||||
Counter: int32(i),
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s defaultPingServer) PingStream(server TestService_PingStreamServer) error {
|
||||
g, ctx := errgroup.WithContext(context.Background())
|
||||
|
||||
if err := s.sendHeader(server.Context()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pings := make(chan *PingRequest)
|
||||
g.Go(func() error {
|
||||
defer close(pings)
|
||||
for {
|
||||
m, err := server.Recv()
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
select {
|
||||
case pings <- m:
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
})
|
||||
g.Go(func() error {
|
||||
var i int32
|
||||
for m := range pings {
|
||||
if err := server.Send(&PingResponse{
|
||||
Value: m.Value,
|
||||
Counter: i,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
i++
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
return g.Wait()
|
||||
}
|
||||
|
||||
func (s *defaultPingServer) sendHeader(ctx context.Context) error {
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
if !ok {
|
||||
md = metadata.New(nil)
|
||||
}
|
||||
|
||||
if tvs := md.Get(PingEchoHeader); len(tvs) > 0 {
|
||||
md.Append(PingEchoHeader, tvs...)
|
||||
}
|
||||
|
||||
md.Append(PingHeader, PingHeaderCts)
|
||||
|
||||
if err := grpc.SendHeader(ctx, md); err != nil {
|
||||
return fmt.Errorf("setting header: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *defaultPingServer) setTrailer(ctx context.Context) error {
|
||||
md := s.buildTrailer(ctx)
|
||||
|
||||
if err := grpc.SetTrailer(ctx, md); err != nil {
|
||||
return fmt.Errorf("setting trailer: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *defaultPingServer) buildTrailer(ctx context.Context) metadata.MD {
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
if !ok {
|
||||
md = metadata.New(nil)
|
||||
}
|
||||
|
||||
if tvs := md.Get(PingEchoTrailer); len(tvs) > 0 {
|
||||
md.Append(PingEchoTrailer, tvs...)
|
||||
}
|
||||
|
||||
md.Append(PingTrailer, PingTrailerCts)
|
||||
|
||||
return md
|
||||
}
|
||||
|
||||
func (s defaultPingServer) setStreamTrailer(server grpc.ServerStream) {
|
||||
server.SetTrailer(s.buildTrailer(server.Context()))
|
||||
}
|
||||
|
||||
var _ TestServiceServer = (*defaultPingServer)(nil)
|
54
proxy/testservice/server/main.go
Normal file
54
proxy/testservice/server/main.go
Normal file
@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"go.linka.cloud/grpc-toolkit/proxy/testservice"
|
||||
)
|
||||
|
||||
var (
|
||||
port = flag.Uint("port", 8080, "Port to listen to")
|
||||
)
|
||||
|
||||
func main() {
|
||||
srv := grpc.NewServer()
|
||||
lis, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", *port))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
testservice.RegisterTestServiceServer(srv, testservice.DefaultTestServiceServer)
|
||||
|
||||
errs := make(chan error)
|
||||
|
||||
go func() {
|
||||
log.Printf("listening on %s", lis.Addr().String())
|
||||
errs <- srv.Serve(lis)
|
||||
}()
|
||||
|
||||
sigs := make(chan os.Signal, 1)
|
||||
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
go func() {
|
||||
sig := <-sigs
|
||||
log.Printf("shutdown due to %s", sig)
|
||||
srv.GracefulStop()
|
||||
}()
|
||||
|
||||
if err := <-errs; err != nil {
|
||||
log.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func init() {
|
||||
flag.Parse()
|
||||
}
|
255
proxy/testservice/test.pb.go
Normal file
255
proxy/testservice/test.pb.go
Normal file
@ -0,0 +1,255 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.25.0-devel
|
||||
// protoc v3.15.5
|
||||
// source: test.proto
|
||||
|
||||
package testservice
|
||||
|
||||
import (
|
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||
emptypb "google.golang.org/protobuf/types/known/emptypb"
|
||||
reflect "reflect"
|
||||
sync "sync"
|
||||
)
|
||||
|
||||
const (
|
||||
// Verify that this generated code is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||
)
|
||||
|
||||
type PingRequest struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Value string `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"`
|
||||
}
|
||||
|
||||
func (x *PingRequest) Reset() {
|
||||
*x = PingRequest{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_test_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *PingRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*PingRequest) ProtoMessage() {}
|
||||
|
||||
func (x *PingRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_test_proto_msgTypes[0]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use PingRequest.ProtoReflect.Descriptor instead.
|
||||
func (*PingRequest) Descriptor() ([]byte, []int) {
|
||||
return file_test_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
func (x *PingRequest) GetValue() string {
|
||||
if x != nil {
|
||||
return x.Value
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type PingResponse struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Value string `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"`
|
||||
Counter int32 `protobuf:"varint,2,opt,name=counter,proto3" json:"counter,omitempty"`
|
||||
}
|
||||
|
||||
func (x *PingResponse) Reset() {
|
||||
*x = PingResponse{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_test_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *PingResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*PingResponse) ProtoMessage() {}
|
||||
|
||||
func (x *PingResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_test_proto_msgTypes[1]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use PingResponse.ProtoReflect.Descriptor instead.
|
||||
func (*PingResponse) Descriptor() ([]byte, []int) {
|
||||
return file_test_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
func (x *PingResponse) GetValue() string {
|
||||
if x != nil {
|
||||
return x.Value
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *PingResponse) GetCounter() int32 {
|
||||
if x != nil {
|
||||
return x.Counter
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
var File_test_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_test_proto_rawDesc = []byte{
|
||||
0x0a, 0x0a, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x11, 0x6d, 0x77,
|
||||
0x69, 0x74, 0x6b, 0x6f, 0x77, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a,
|
||||
0x1b, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66,
|
||||
0x2f, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x23, 0x0a, 0x0b,
|
||||
0x50, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x76,
|
||||
0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75,
|
||||
0x65, 0x22, 0x3e, 0x0a, 0x0c, 0x50, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73,
|
||||
0x65, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09,
|
||||
0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x75, 0x6e, 0x74,
|
||||
0x65, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x07, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x65,
|
||||
0x72, 0x32, 0x8d, 0x03, 0x0a, 0x0b, 0x54, 0x65, 0x73, 0x74, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63,
|
||||
0x65, 0x12, 0x46, 0x0a, 0x09, 0x50, 0x69, 0x6e, 0x67, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x12, 0x16,
|
||||
0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66,
|
||||
0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x1a, 0x1f, 0x2e, 0x6d, 0x77, 0x69, 0x74, 0x6b, 0x6f, 0x77,
|
||||
0x2e, 0x74, 0x65, 0x73, 0x74, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50, 0x69, 0x6e, 0x67, 0x52,
|
||||
0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x49, 0x0a, 0x04, 0x50, 0x69, 0x6e,
|
||||
0x67, 0x12, 0x1e, 0x2e, 0x6d, 0x77, 0x69, 0x74, 0x6b, 0x6f, 0x77, 0x2e, 0x74, 0x65, 0x73, 0x74,
|
||||
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73,
|
||||
0x74, 0x1a, 0x1f, 0x2e, 0x6d, 0x77, 0x69, 0x74, 0x6b, 0x6f, 0x77, 0x2e, 0x74, 0x65, 0x73, 0x74,
|
||||
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e,
|
||||
0x73, 0x65, 0x22, 0x00, 0x12, 0x45, 0x0a, 0x09, 0x50, 0x69, 0x6e, 0x67, 0x45, 0x72, 0x72, 0x6f,
|
||||
0x72, 0x12, 0x1e, 0x2e, 0x6d, 0x77, 0x69, 0x74, 0x6b, 0x6f, 0x77, 0x2e, 0x74, 0x65, 0x73, 0x74,
|
||||
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50, 0x69, 0x6e, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73,
|
||||
0x74, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
|
||||
0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x22, 0x00, 0x12, 0x4f, 0x0a, 0x08, 0x50,
|
||||
0x69, 0x6e, 0x67, 0x4c, 0x69, 0x73, 0x74, 0x12, 0x1e, 0x2e, 0x6d, 0x77, 0x69, 0x74, 0x6b, 0x6f,
|
||||
0x77, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50, 0x69, 0x6e, 0x67,
|
||||
0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1f, 0x2e, 0x6d, 0x77, 0x69, 0x74, 0x6b, 0x6f,
|
||||
0x77, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50, 0x69, 0x6e, 0x67,
|
||||
0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x30, 0x01, 0x12, 0x53, 0x0a, 0x0a,
|
||||
0x50, 0x69, 0x6e, 0x67, 0x53, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x12, 0x1e, 0x2e, 0x6d, 0x77, 0x69,
|
||||
0x74, 0x6b, 0x6f, 0x77, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50,
|
||||
0x69, 0x6e, 0x67, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1f, 0x2e, 0x6d, 0x77, 0x69,
|
||||
0x74, 0x6b, 0x6f, 0x77, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x50,
|
||||
0x69, 0x6e, 0x67, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x28, 0x01, 0x30,
|
||||
0x01, 0x42, 0x2b, 0x5a, 0x29, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f,
|
||||
0x6d, 0x77, 0x69, 0x74, 0x6b, 0x6f, 0x77, 0x2f, 0x67, 0x72, 0x70, 0x63, 0x2d, 0x70, 0x72, 0x6f,
|
||||
0x78, 0x79, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x73, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x62, 0x06,
|
||||
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
file_test_proto_rawDescOnce sync.Once
|
||||
file_test_proto_rawDescData = file_test_proto_rawDesc
|
||||
)
|
||||
|
||||
func file_test_proto_rawDescGZIP() []byte {
|
||||
file_test_proto_rawDescOnce.Do(func() {
|
||||
file_test_proto_rawDescData = protoimpl.X.CompressGZIP(file_test_proto_rawDescData)
|
||||
})
|
||||
return file_test_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_test_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
|
||||
var file_test_proto_goTypes = []interface{}{
|
||||
(*PingRequest)(nil), // 0: mwitkow.testproto.PingRequest
|
||||
(*PingResponse)(nil), // 1: mwitkow.testproto.PingResponse
|
||||
(*emptypb.Empty)(nil), // 2: google.protobuf.Empty
|
||||
}
|
||||
var file_test_proto_depIdxs = []int32{
|
||||
2, // 0: mwitkow.testproto.TestService.PingEmpty:input_type -> google.protobuf.Empty
|
||||
0, // 1: mwitkow.testproto.TestService.Ping:input_type -> mwitkow.testproto.PingRequest
|
||||
0, // 2: mwitkow.testproto.TestService.PingError:input_type -> mwitkow.testproto.PingRequest
|
||||
0, // 3: mwitkow.testproto.TestService.PingList:input_type -> mwitkow.testproto.PingRequest
|
||||
0, // 4: mwitkow.testproto.TestService.PingStream:input_type -> mwitkow.testproto.PingRequest
|
||||
1, // 5: mwitkow.testproto.TestService.PingEmpty:output_type -> mwitkow.testproto.PingResponse
|
||||
1, // 6: mwitkow.testproto.TestService.Ping:output_type -> mwitkow.testproto.PingResponse
|
||||
2, // 7: mwitkow.testproto.TestService.PingError:output_type -> google.protobuf.Empty
|
||||
1, // 8: mwitkow.testproto.TestService.PingList:output_type -> mwitkow.testproto.PingResponse
|
||||
1, // 9: mwitkow.testproto.TestService.PingStream:output_type -> mwitkow.testproto.PingResponse
|
||||
5, // [5:10] is the sub-list for method output_type
|
||||
0, // [0:5] is the sub-list for method input_type
|
||||
0, // [0:0] is the sub-list for extension type_name
|
||||
0, // [0:0] is the sub-list for extension extendee
|
||||
0, // [0:0] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_test_proto_init() }
|
||||
func file_test_proto_init() {
|
||||
if File_test_proto != nil {
|
||||
return
|
||||
}
|
||||
if !protoimpl.UnsafeEnabled {
|
||||
file_test_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*PingRequest); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_test_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*PingResponse); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: file_test_proto_rawDesc,
|
||||
NumEnums: 0,
|
||||
NumMessages: 2,
|
||||
NumExtensions: 0,
|
||||
NumServices: 1,
|
||||
},
|
||||
GoTypes: file_test_proto_goTypes,
|
||||
DependencyIndexes: file_test_proto_depIdxs,
|
||||
MessageInfos: file_test_proto_msgTypes,
|
||||
}.Build()
|
||||
File_test_proto = out.File
|
||||
file_test_proto_rawDesc = nil
|
||||
file_test_proto_goTypes = nil
|
||||
file_test_proto_depIdxs = nil
|
||||
}
|
30
proxy/testservice/test.proto
Normal file
30
proxy/testservice/test.proto
Normal file
@ -0,0 +1,30 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package mwitkow.testproto;
|
||||
|
||||
option go_package="go.linka.cloud/grpc-toolkit/proxy/testservice";
|
||||
|
||||
import "google/protobuf/empty.proto";
|
||||
|
||||
message PingRequest {
|
||||
string value = 1;
|
||||
}
|
||||
|
||||
message PingResponse {
|
||||
string value = 1;
|
||||
int32 counter = 2;
|
||||
}
|
||||
|
||||
service TestService {
|
||||
rpc PingEmpty(google.protobuf.Empty) returns (PingResponse) {}
|
||||
|
||||
rpc Ping(PingRequest) returns (PingResponse) {}
|
||||
|
||||
rpc PingError(PingRequest) returns (google.protobuf.Empty) {}
|
||||
|
||||
rpc PingList(PingRequest) returns (stream PingResponse) {}
|
||||
|
||||
rpc PingStream(stream PingRequest) returns (stream PingResponse) {}
|
||||
|
||||
}
|
||||
|
306
proxy/testservice/test_grpc.pb.go
Normal file
306
proxy/testservice/test_grpc.pb.go
Normal file
@ -0,0 +1,306 @@
|
||||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||
|
||||
package testservice
|
||||
|
||||
import (
|
||||
context "context"
|
||||
grpc "google.golang.org/grpc"
|
||||
codes "google.golang.org/grpc/codes"
|
||||
status "google.golang.org/grpc/status"
|
||||
emptypb "google.golang.org/protobuf/types/known/emptypb"
|
||||
)
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
// Requires gRPC-Go v1.32.0 or later.
|
||||
const _ = grpc.SupportPackageIsVersion7
|
||||
|
||||
// TestServiceClient is the client API for TestService service.
|
||||
//
|
||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
|
||||
type TestServiceClient interface {
|
||||
PingEmpty(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*PingResponse, error)
|
||||
Ping(ctx context.Context, in *PingRequest, opts ...grpc.CallOption) (*PingResponse, error)
|
||||
PingError(ctx context.Context, in *PingRequest, opts ...grpc.CallOption) (*emptypb.Empty, error)
|
||||
PingList(ctx context.Context, in *PingRequest, opts ...grpc.CallOption) (TestService_PingListClient, error)
|
||||
PingStream(ctx context.Context, opts ...grpc.CallOption) (TestService_PingStreamClient, error)
|
||||
}
|
||||
|
||||
type testServiceClient struct {
|
||||
cc grpc.ClientConnInterface
|
||||
}
|
||||
|
||||
func NewTestServiceClient(cc grpc.ClientConnInterface) TestServiceClient {
|
||||
return &testServiceClient{cc}
|
||||
}
|
||||
|
||||
func (c *testServiceClient) PingEmpty(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*PingResponse, error) {
|
||||
out := new(PingResponse)
|
||||
err := c.cc.Invoke(ctx, "/mwitkow.testproto.TestService/PingEmpty", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *testServiceClient) Ping(ctx context.Context, in *PingRequest, opts ...grpc.CallOption) (*PingResponse, error) {
|
||||
out := new(PingResponse)
|
||||
err := c.cc.Invoke(ctx, "/mwitkow.testproto.TestService/Ping", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *testServiceClient) PingError(ctx context.Context, in *PingRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) {
|
||||
out := new(emptypb.Empty)
|
||||
err := c.cc.Invoke(ctx, "/mwitkow.testproto.TestService/PingError", in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *testServiceClient) PingList(ctx context.Context, in *PingRequest, opts ...grpc.CallOption) (TestService_PingListClient, error) {
|
||||
stream, err := c.cc.NewStream(ctx, &TestService_ServiceDesc.Streams[0], "/mwitkow.testproto.TestService/PingList", opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
x := &testServicePingListClient{stream}
|
||||
if err := x.ClientStream.SendMsg(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := x.ClientStream.CloseSend(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return x, nil
|
||||
}
|
||||
|
||||
type TestService_PingListClient interface {
|
||||
Recv() (*PingResponse, error)
|
||||
grpc.ClientStream
|
||||
}
|
||||
|
||||
type testServicePingListClient struct {
|
||||
grpc.ClientStream
|
||||
}
|
||||
|
||||
func (x *testServicePingListClient) Recv() (*PingResponse, error) {
|
||||
m := new(PingResponse)
|
||||
if err := x.ClientStream.RecvMsg(m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (c *testServiceClient) PingStream(ctx context.Context, opts ...grpc.CallOption) (TestService_PingStreamClient, error) {
|
||||
stream, err := c.cc.NewStream(ctx, &TestService_ServiceDesc.Streams[1], "/mwitkow.testproto.TestService/PingStream", opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
x := &testServicePingStreamClient{stream}
|
||||
return x, nil
|
||||
}
|
||||
|
||||
type TestService_PingStreamClient interface {
|
||||
Send(*PingRequest) error
|
||||
Recv() (*PingResponse, error)
|
||||
grpc.ClientStream
|
||||
}
|
||||
|
||||
type testServicePingStreamClient struct {
|
||||
grpc.ClientStream
|
||||
}
|
||||
|
||||
func (x *testServicePingStreamClient) Send(m *PingRequest) error {
|
||||
return x.ClientStream.SendMsg(m)
|
||||
}
|
||||
|
||||
func (x *testServicePingStreamClient) Recv() (*PingResponse, error) {
|
||||
m := new(PingResponse)
|
||||
if err := x.ClientStream.RecvMsg(m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// TestServiceServer is the server API for TestService service.
|
||||
// All implementations must embed UnimplementedTestServiceServer
|
||||
// for forward compatibility
|
||||
type TestServiceServer interface {
|
||||
PingEmpty(context.Context, *emptypb.Empty) (*PingResponse, error)
|
||||
Ping(context.Context, *PingRequest) (*PingResponse, error)
|
||||
PingError(context.Context, *PingRequest) (*emptypb.Empty, error)
|
||||
PingList(*PingRequest, TestService_PingListServer) error
|
||||
PingStream(TestService_PingStreamServer) error
|
||||
mustEmbedUnimplementedTestServiceServer()
|
||||
}
|
||||
|
||||
// UnimplementedTestServiceServer must be embedded to have forward compatible implementations.
|
||||
type UnimplementedTestServiceServer struct {
|
||||
}
|
||||
|
||||
func (UnimplementedTestServiceServer) PingEmpty(context.Context, *emptypb.Empty) (*PingResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method PingEmpty not implemented")
|
||||
}
|
||||
func (UnimplementedTestServiceServer) Ping(context.Context, *PingRequest) (*PingResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method Ping not implemented")
|
||||
}
|
||||
func (UnimplementedTestServiceServer) PingError(context.Context, *PingRequest) (*emptypb.Empty, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method PingError not implemented")
|
||||
}
|
||||
func (UnimplementedTestServiceServer) PingList(*PingRequest, TestService_PingListServer) error {
|
||||
return status.Errorf(codes.Unimplemented, "method PingList not implemented")
|
||||
}
|
||||
func (UnimplementedTestServiceServer) PingStream(TestService_PingStreamServer) error {
|
||||
return status.Errorf(codes.Unimplemented, "method PingStream not implemented")
|
||||
}
|
||||
func (UnimplementedTestServiceServer) mustEmbedUnimplementedTestServiceServer() {}
|
||||
|
||||
// UnsafeTestServiceServer may be embedded to opt out of forward compatibility for this service.
|
||||
// Use of this interface is not recommended, as added methods to TestServiceServer will
|
||||
// result in compilation errors.
|
||||
type UnsafeTestServiceServer interface {
|
||||
mustEmbedUnimplementedTestServiceServer()
|
||||
}
|
||||
|
||||
func RegisterTestServiceServer(s grpc.ServiceRegistrar, srv TestServiceServer) {
|
||||
s.RegisterService(&TestService_ServiceDesc, srv)
|
||||
}
|
||||
|
||||
func _TestService_PingEmpty_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(emptypb.Empty)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(TestServiceServer).PingEmpty(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/mwitkow.testproto.TestService/PingEmpty",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(TestServiceServer).PingEmpty(ctx, req.(*emptypb.Empty))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _TestService_Ping_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(PingRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(TestServiceServer).Ping(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/mwitkow.testproto.TestService/Ping",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(TestServiceServer).Ping(ctx, req.(*PingRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _TestService_PingError_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(PingRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(TestServiceServer).PingError(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/mwitkow.testproto.TestService/PingError",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(TestServiceServer).PingError(ctx, req.(*PingRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _TestService_PingList_Handler(srv interface{}, stream grpc.ServerStream) error {
|
||||
m := new(PingRequest)
|
||||
if err := stream.RecvMsg(m); err != nil {
|
||||
return err
|
||||
}
|
||||
return srv.(TestServiceServer).PingList(m, &testServicePingListServer{stream})
|
||||
}
|
||||
|
||||
type TestService_PingListServer interface {
|
||||
Send(*PingResponse) error
|
||||
grpc.ServerStream
|
||||
}
|
||||
|
||||
type testServicePingListServer struct {
|
||||
grpc.ServerStream
|
||||
}
|
||||
|
||||
func (x *testServicePingListServer) Send(m *PingResponse) error {
|
||||
return x.ServerStream.SendMsg(m)
|
||||
}
|
||||
|
||||
func _TestService_PingStream_Handler(srv interface{}, stream grpc.ServerStream) error {
|
||||
return srv.(TestServiceServer).PingStream(&testServicePingStreamServer{stream})
|
||||
}
|
||||
|
||||
type TestService_PingStreamServer interface {
|
||||
Send(*PingResponse) error
|
||||
Recv() (*PingRequest, error)
|
||||
grpc.ServerStream
|
||||
}
|
||||
|
||||
type testServicePingStreamServer struct {
|
||||
grpc.ServerStream
|
||||
}
|
||||
|
||||
func (x *testServicePingStreamServer) Send(m *PingResponse) error {
|
||||
return x.ServerStream.SendMsg(m)
|
||||
}
|
||||
|
||||
func (x *testServicePingStreamServer) Recv() (*PingRequest, error) {
|
||||
m := new(PingRequest)
|
||||
if err := x.ServerStream.RecvMsg(m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// TestService_ServiceDesc is the grpc.ServiceDesc for TestService service.
|
||||
// It's only intended for direct use with grpc.RegisterService,
|
||||
// and not to be introspected or modified (even as a copy)
|
||||
var TestService_ServiceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "mwitkow.testproto.TestService",
|
||||
HandlerType: (*TestServiceServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "PingEmpty",
|
||||
Handler: _TestService_PingEmpty_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "Ping",
|
||||
Handler: _TestService_Ping_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "PingError",
|
||||
Handler: _TestService_PingError_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{
|
||||
{
|
||||
StreamName: "PingList",
|
||||
Handler: _TestService_PingList_Handler,
|
||||
ServerStreams: true,
|
||||
},
|
||||
{
|
||||
StreamName: "PingStream",
|
||||
Handler: _TestService_PingStream_Handler,
|
||||
ServerStreams: true,
|
||||
ClientStreams: true,
|
||||
},
|
||||
},
|
||||
Metadata: "test.proto",
|
||||
}
|
158
proxy/testservice/testping.go
Normal file
158
proxy/testservice/testping.go
Normal file
@ -0,0 +1,158 @@
|
||||
package testservice
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
const (
|
||||
returnHeader = "test-client-header"
|
||||
)
|
||||
|
||||
// TestTestServiceServerImpl can be called to test the underlying TestServiceServer.
|
||||
func TestTestServiceServerImpl(t *testing.T, client TestServiceClient) {
|
||||
t.Run("Unary ping", func(t *testing.T) {
|
||||
want := "hello, world"
|
||||
hdr := metadata.MD{}
|
||||
res, err := client.Ping(context.TODO(), &PingRequest{Value: want}, grpc.Header(&hdr))
|
||||
if err != nil {
|
||||
t.Errorf("want no err; got %v", err)
|
||||
return
|
||||
}
|
||||
checkHeaders(t, hdr)
|
||||
t.Logf("got %v (%d)", res.Value, res.Counter)
|
||||
if got := res.Value; got != want {
|
||||
t.Errorf("res.Value = %q; want %q", got, want)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Error ping", func(t *testing.T) {
|
||||
_, err := client.PingError(context.TODO(), &PingRequest{})
|
||||
if err == nil {
|
||||
t.Errorf("want err; got %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Server streaming ping", func(t *testing.T) {
|
||||
want := "hello, world"
|
||||
stream, err := client.PingList(context.TODO(), &PingRequest{Value: want})
|
||||
if err != nil {
|
||||
t.Errorf("want no err; got %v", err)
|
||||
if err := stream.CloseSend(); err != nil {
|
||||
t.Fatalf("closing send channel: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
hdr, err := stream.Header()
|
||||
if err != nil {
|
||||
t.Errorf("reading headers: %v", err)
|
||||
}
|
||||
checkHeaders(t, hdr)
|
||||
|
||||
for {
|
||||
res, err := stream.Recv()
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
checkTrailers(t, stream.Trailer())
|
||||
return
|
||||
}
|
||||
t.Errorf("want no err; got %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("got %v (%d)", res.Value, res.Counter)
|
||||
if got := res.Value; got != want {
|
||||
t.Errorf("res.Value = %q; want %q", got, want)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Bidirectional pinging", func(t *testing.T) {
|
||||
want := "hello, world"
|
||||
stream, err := client.PingStream(context.TODO())
|
||||
if err != nil {
|
||||
t.Errorf("want no err; got %v", err)
|
||||
if err := stream.CloseSend(); err != nil {
|
||||
t.Fatalf("closing send channel: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
d := make(chan struct{})
|
||||
go func() {
|
||||
hdr, err := stream.Header()
|
||||
if err != nil {
|
||||
t.Errorf("reading headers: %v", err)
|
||||
}
|
||||
checkHeaders(t, hdr)
|
||||
close(d)
|
||||
}()
|
||||
|
||||
for i := 0; i < 25; i++ {
|
||||
if err := stream.Send(&PingRequest{Value: want}); err != nil {
|
||||
t.Errorf("want no err; got %v", err)
|
||||
return
|
||||
}
|
||||
res, err := stream.Recv()
|
||||
if err != nil {
|
||||
t.Errorf("receiving full duplex stream: %w", err)
|
||||
return
|
||||
}
|
||||
t.Logf("got %v (%d)", res.Value, res.Counter)
|
||||
if got := res.Value; got != want {
|
||||
t.Errorf("res.Value = %q; want %q", got, want)
|
||||
}
|
||||
if got, want := res.Counter, int32(i); got != want {
|
||||
t.Errorf("res.Counter = %d; want %d", got, want)
|
||||
}
|
||||
}
|
||||
if err := stream.CloseSend(); err != nil {
|
||||
t.Errorf("closing full duplex stream: %v", err)
|
||||
}
|
||||
<-d
|
||||
})
|
||||
|
||||
t.Run("Unary ping with headers", func(t *testing.T) {
|
||||
want := "hello, world"
|
||||
req := &PingRequest{Value: want}
|
||||
|
||||
ctx := metadata.AppendToOutgoingContext(context.Background(), returnHeader, "I like turtles.")
|
||||
inHeader := make(metadata.MD)
|
||||
|
||||
res, err := client.Ping(ctx, req, grpc.Header(&inHeader))
|
||||
if err != nil {
|
||||
t.Errorf("want no err; got %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("got %v (%d)", res.Value, res.Counter)
|
||||
if !reflect.DeepEqual(inHeader.Get(returnHeader), []string{"I like turtles."}) {
|
||||
t.Errorf("did not receive correct return headers")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func checkTrailers(t *testing.T, md metadata.MD) {
|
||||
vs := md.Get(PingTrailer)
|
||||
if want, got := 1, len(vs); want != got {
|
||||
t.Errorf("trailer %q not present", PingTrailer)
|
||||
return
|
||||
}
|
||||
if want, got := []string{PingTrailerCts}, vs; !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("trailer mismatch; want %q, got %q", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func checkHeaders(t *testing.T, md metadata.MD) {
|
||||
vs := md.Get(PingHeader)
|
||||
if want, got := 1, len(vs); want != got {
|
||||
t.Errorf("header %q not present", PingHeader)
|
||||
return
|
||||
}
|
||||
if want, got := []string{PingHeaderCts}, vs; !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("header mismatch; want %q, got %q", want, got)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user