How to make gRPC Go work with standard server-side IO?

I am new to gRPC and here is my problem. I am trying to write a service to expose myOwnServiceto the gRPC service using the following service method:

rpc HighFive (stream HighRequest) returns (stream HighReply) {}

Server-side code is as follows:

func (s *server) HighFive(stream pb.Greeter_HighFiveServer) error {    
    // Oops, don't know how to do here ...
    myOwnService(stdin io.ReadCloser, stdout io.WriteCloser)
    return nil
}

func myOwnService(stdin io.ReadCloser, stdout io.WriteCloser) error {    
    // read input from stdin, do something, the  write result to stdout
    ...
    return nil
}

As you can see above, I have no idea how to streamwork with io.Readerand io.Writerin my original service, so that the caller of the HighFivegRPC service can read and write data just as usual myOwnService.

[Refresh] My current messages are similar to this, but you can change them if necessary:

message HighRequest {
  bytes content  = 1;
}

message HighReply {
  bytes content  = 1;
}
+1
source share
1 answer

Per gRPC RPC, stream Recv HighRequest, , myOwnService.

, HighRequest bytes string, myOwnService stdin, []byte bytes.NewReader.

, myOwnService io.ReadCloser. , , myOwnService , , , ioutil.NopCloser, .

:

// Next tag: 2
message HighRequest {
  bytes content = 1;
}

func (s *server) HighFive(stream pb.Greeter_HighFiveServer) error {
  for req, err := stream.Recv(); {
    if err != nil {
      if err == io.EOF {
        return nil
      }
      return err
    }
    in := ioutil.NopCloser(bytes.NewReader(req.Content))
    out := /* ... */
    if err := myOwnService(in, out); err != nil {
      return err
    }
  }
}
+1

Source: https://habr.com/ru/post/1685979/


All Articles