Skip to content

Instantly share code, notes, and snippets.

@kerinin
Created August 31, 2016 22:32
Show Gist options
  • Save kerinin/6ead62c92f7a63bada345e37798d5f80 to your computer and use it in GitHub Desktop.
Save kerinin/6ead62c92f7a63bada345e37798d5f80 to your computer and use it in GitHub Desktop.
// Code generated by protoc-gen-go.
// source: src/demo/demo.proto
// DO NOT EDIT!
/*
Package demo is a generated protocol buffer package.
It is generated from these files:
src/demo/demo.proto
It has these top-level messages:
Empty
*/
package main
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import (
context "golang.org/x/net/context"
grpc "google.golang.org/grpc"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
type Empty struct {
}
func (m *Empty) Reset() { *m = Empty{} }
func (m *Empty) String() string { return proto.CompactTextString(m) }
func (*Empty) ProtoMessage() {}
func (*Empty) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
func init() {
proto.RegisterType((*Empty)(nil), "Empty")
}
// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConn
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
const _ = grpc.SupportPackageIsVersion3
// Client API for Demo service
type DemoClient interface {
Doit(ctx context.Context, in *Empty, opts ...grpc.CallOption) (Demo_DoitClient, error)
}
type demoClient struct {
cc *grpc.ClientConn
}
func NewDemoClient(cc *grpc.ClientConn) DemoClient {
return &demoClient{cc}
}
func (c *demoClient) Doit(ctx context.Context, in *Empty, opts ...grpc.CallOption) (Demo_DoitClient, error) {
stream, err := grpc.NewClientStream(ctx, &_Demo_serviceDesc.Streams[0], c.cc, "/Demo/Doit", opts...)
if err != nil {
return nil, err
}
x := &demoDoitClient{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 Demo_DoitClient interface {
Recv() (*Empty, error)
grpc.ClientStream
}
type demoDoitClient struct {
grpc.ClientStream
}
func (x *demoDoitClient) Recv() (*Empty, error) {
m := new(Empty)
if err := x.ClientStream.RecvMsg(m); err != nil {
return nil, err
}
return m, nil
}
// Server API for Demo service
type DemoServer interface {
Doit(*Empty, Demo_DoitServer) error
}
func RegisterDemoServer(s *grpc.Server, srv DemoServer) {
s.RegisterService(&_Demo_serviceDesc, srv)
}
func _Demo_Doit_Handler(srv interface{}, stream grpc.ServerStream) error {
m := new(Empty)
if err := stream.RecvMsg(m); err != nil {
return err
}
return srv.(DemoServer).Doit(m, &demoDoitServer{stream})
}
type Demo_DoitServer interface {
Send(*Empty) error
grpc.ServerStream
}
type demoDoitServer struct {
grpc.ServerStream
}
func (x *demoDoitServer) Send(m *Empty) error {
return x.ServerStream.SendMsg(m)
}
var _Demo_serviceDesc = grpc.ServiceDesc{
ServiceName: "Demo",
HandlerType: (*DemoServer)(nil),
Methods: []grpc.MethodDesc{},
Streams: []grpc.StreamDesc{
{
StreamName: "Doit",
Handler: _Demo_Doit_Handler,
ServerStreams: true,
},
},
Metadata: fileDescriptor0,
}
func init() { proto.RegisterFile("src/demo/demo.proto", fileDescriptor0) }
var fileDescriptor0 = []byte{
// 81 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0x12, 0x2e, 0x2e, 0x4a, 0xd6,
0x4f, 0x49, 0xcd, 0xcd, 0x07, 0x13, 0x7a, 0x05, 0x45, 0xf9, 0x25, 0xf9, 0x4a, 0xec, 0x5c, 0xac,
0xae, 0xb9, 0x05, 0x25, 0x95, 0x46, 0x4a, 0x5c, 0x2c, 0x2e, 0x40, 0x61, 0x21, 0x29, 0x20, 0x9d,
0x9f, 0x59, 0x22, 0xc4, 0xa6, 0x07, 0x16, 0x97, 0x82, 0xd2, 0x4a, 0x0c, 0x06, 0x8c, 0x49, 0x6c,
0x60, 0x3d, 0xc6, 0x80, 0x00, 0x00, 0x00, 0xff, 0xff, 0x60, 0x8e, 0x15, 0xc0, 0x4a, 0x00, 0x00,
0x00,
}
syntax = "proto3";
package main;
message Empty{}
service Demo {
rpc Doit(Empty) returns (stream Empty) {}
}
package main
import (
"io"
"log"
"net"
"sync"
"time"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
var wg sync.WaitGroup
type server struct{}
func (s server) Doit(_ *Empty, stream Demo_DoitServer) error {
wg.Add(1)
defer wg.Done()
for {
err := stream.Send(&Empty{})
if err != nil {
log.Fatalf("Error has code %d: %v", grpc.Code(err), err)
return err
}
}
}
func main() {
lis, _ := net.Listen("tcp", "0.0.0.0:50051")
grpcServer := grpc.NewServer()
RegisterDemoServer(grpcServer, server{})
go grpcServer.Serve(lis)
conn, _ := grpc.Dial("0.0.0.0:50051", grpc.WithInsecure())
defer conn.Close()
client := NewDemoClient(conn)
ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
stream, _ := client.Doit(ctx, &Empty{})
for {
_, err := stream.Recv()
if err == io.EOF {
continue
}
}
wg.Wait()
}
2016/08/31 17:30:44 Error has code 2: stream error: code = 4 desc = "context deadline exceeded"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment