Skip to content
πŸ¦€ Easy request GRPC Server with reflect mode
Branch: master
Clone or download
Latest commit f3e27e8 Mar 18, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
testing
.gitignore
Gopkg.lock
Gopkg.toml
README.md update doc Feb 25, 2019
common.go
core.go add listMethods Mar 17, 2019
desc_source.go
design.jpg
enter.go
format.go add sync mode Feb 25, 2019
invoke.go
types_cache.go add proto types cache Mar 18, 2019

README.md

grpcall

grpcall is a client library easy request GRPC Server with reflect mode. and easy make grpc proxy mode middleware, like grpc-gateway.

use GRPC's reflect mode to request GRPC Server. grpcall support FileDescriptorSet and grpc/reflection mode.

some code refer: protoreflect, grpcurl

Feature

  • support event hook
  • dynamic request GRPC Server with reflect mode.
  • dynamic convert protobuf to json and json to protobuf
  • grpc client manager
  • support stream
  • recv system signal to update new descriptor.
  • ...

Usage

grpcall.SetProtoSetFiles("helloworld.protoset")
grpcall.InitDescSource()

var handler = DefaultEventHandler{}
var sendBody string

grpcEnter, err := grpcall.New(
    grpcall.SetHookHandler(&handler),
)
grpcEnter.Init()

sendBody = `{"name": "xiaorui.cc"}`
res, err := grpcEnter.Call("127.0.0.1:50051", "helloworld.Greeter", "SayHello", sendBody)
fmt.Printf("%+v \n", res)
fmt.Println("req/reply return err: ", err)

sendBody = `{"msg": "hehe world"}`
res, err = grpcEnter.Call("127.0.0.1:50051", "helloworld.SimpleService", "SimpleRPC", sendBody)
fmt.Printf("%+v \n", res)
fmt.Println("stream return err: ", err)

wg := sync.WaitGroup{}
wg.Add(2)

go func() {
    defer wg.Done()
    for {
        body := fmt.Sprintf(`{"msg": "hehe world %s"}`, time.Now().String())
        select {
        case res.SendChan <- []byte(body):
            // fmt.Println("send", body)
            time.Sleep(3 * time.Second)

        case <-res.DoneChan:
            return

        default:
            return
        }
    }
}()

go func() {
    defer wg.Done()

    for {
        select {
        case msg, ok := <-res.ResultChan:
            fmt.Println("recv data:", msg, ok)
        case err := <-res.DoneChan:
            fmt.Println("done chan: ", err)
            return
        }
    }
}()

wg.Wait()

type DefaultEventHandler struct {
    sendChan chan []byte
}

func (h *DefaultEventHandler) OnReceiveData(md metadata.MD, resp string, respErr error) {
}

func (h *DefaultEventHandler) OnReceiveTrailers(stat *status.Status, md metadata.MD) {
}

Test Example

  1. start grpc server
cd testing; go run test_server.go
  1. start grpcall client
cd testing; go run test_grpcall_client.go

Protoset Files

Protoset files contain binary encoded google.protobuf.FileDescriptorSet protos. To create a protoset file, invoke protoc with the *.proto files that define the service:

protoc --proto_path=. \
    --descriptor_set_out=myservice.protoset \
    --include_imports \
    my/custom/server/service.proto
You can’t perform that action at this time.