From 053d2d13751c00d8da297657f5550e14a1b1c55e Mon Sep 17 00:00:00 2001 From: Christophe de Vienne Date: Wed, 18 Jul 2018 19:51:41 +0200 Subject: [PATCH] Fix #47 - Streamed replies with subject params --- examples/alloptions/alloptions.nrpc.go | 63 ++++++++++++++++++++++++++ examples/alloptions/alloptions.pb.go | 63 +++++++++++++------------- examples/alloptions/alloptions.proto | 5 ++ examples/alloptions/alloptions_test.go | 27 +++++++++++ protoc-gen-nrpc/tmpl.go | 9 ++-- 5 files changed, 133 insertions(+), 34 deletions(-) diff --git a/examples/alloptions/alloptions.nrpc.go b/examples/alloptions/alloptions.nrpc.go index fc2f2d8..380d968 100644 --- a/examples/alloptions/alloptions.nrpc.go +++ b/examples/alloptions/alloptions.nrpc.go @@ -387,6 +387,7 @@ func (c *SvcCustomSubjectClient) MtVoidReqStreamedReply( // SvcSubjectParams should implement. type SvcSubjectParamsServer interface { MtWithSubjectParams(ctx context.Context, mp1 string, mp2 string) (resp SimpleStringReply, err error) + MtStreamedReplyWithSubjectParams(ctx context.Context, mp1 string, mp2 string, pushRep func(SimpleStringReply)) (err error) MtNoReply(ctx context.Context) } @@ -410,6 +411,40 @@ func (h *SvcSubjectParamsHandler) Subject() string { return "root.*.svcsubjectparams.*.>" } +func (h *SvcSubjectParamsHandler) MtStreamedReplyWithSubjectParamsHandler(ctx context.Context, tail []string, msg *nats.Msg) { + mtParams, encoding, err := nrpc.ParseSubjectTail(2, tail) + if err != nil { + log.Printf("SvcSubjectParams: MtStreamedReplyWithSubjectParams subject parsing failed:") + } + + ctx, cancel := context.WithCancel(ctx) + + keepStreamAlive := nrpc.NewKeepStreamAlive(h.nc, msg.Reply, encoding, cancel) + + var msgCount uint32 + + _, nrpcErr := nrpc.CaptureErrors(func() (proto.Message, error) { + err := h.server.MtStreamedReplyWithSubjectParams(ctx, mtParams[0], mtParams[1], func(rep SimpleStringReply){ + if err = nrpc.Publish(&rep, nil, h.nc, msg.Reply, encoding); err != nil { + log.Printf("nrpc: error publishing response") + cancel() + return + } + msgCount++ + }) + return nil, err + }) + keepStreamAlive.Stop() + + if nrpcErr != nil { + nrpc.Publish(nil, nrpcErr, h.nc, msg.Reply, encoding) + } else { + nrpc.Publish( + nil, &nrpc.Error{Type: nrpc.Error_EOS, MsgCount: msgCount}, + h.nc, msg.Reply, encoding) + } +} + func (h *SvcSubjectParamsHandler) MtNoRequestWParamsPublish(pkginstance string, svcclientid string, mtmp1 string, msg SimpleStringReply) error { rawMsg, err := nrpc.Marshal("protobuf", &msg) if err != nil { @@ -465,6 +500,9 @@ func (h *SvcSubjectParamsHandler) Handler(msg *nats.Msg) { log.Printf("MtWithSubjectParamsHandler: MtWithSubjectParams handler failed: %s", replyError.Error()) } } + case "mtstreamedreplywithsubjectparams": + h.MtStreamedReplyWithSubjectParamsHandler(ctx, tail, msg) + return case "mtnoreply": noreply = true _, encoding, err = nrpc.ParseSubjectTail(0, tail) @@ -551,6 +589,31 @@ func (c *SvcSubjectParamsClient) MtWithSubjectParams(mp1 string, mp2 string, ) ( return } +func (c *SvcSubjectParamsClient) MtStreamedReplyWithSubjectParams( + ctx context.Context,mp1 string,mp2 string, + cb func (context.Context, SimpleStringReply), +) error { + subject := c.PkgSubject + "." + c.PkgParaminstance + "." + c.Subject + "." + c.SvcParamclientid + "." + "mtstreamedreplywithsubjectparams" + "." + mp1 + "." + mp2 + + sub, err := nrpc.StreamCall(ctx, c.nc, subject, &nrpc.Void{}, c.Encoding, c.Timeout) + if err != nil { + return err + } + + var res SimpleStringReply + for { + err = sub.Next(&res) + if err != nil { + break + } + cb(ctx, res) + } + if err == nrpc.ErrEOS { + err = nil + } + return err +} + func (c *SvcSubjectParamsClient) MtNoReply() (err error) { subject := c.PkgSubject + "." + c.PkgParaminstance + "." + c.Subject + "." + c.SvcParamclientid + "." + "mtnoreply" diff --git a/examples/alloptions/alloptions.pb.go b/examples/alloptions/alloptions.pb.go index 2597e3c..89716f8 100644 --- a/examples/alloptions/alloptions.pb.go +++ b/examples/alloptions/alloptions.pb.go @@ -30,7 +30,7 @@ func (m *StringArg) Reset() { *m = StringArg{} } func (m *StringArg) String() string { return proto.CompactTextString(m) } func (*StringArg) ProtoMessage() {} func (*StringArg) Descriptor() ([]byte, []int) { - return fileDescriptor_alloptions_072d689a4afa170c, []int{0} + return fileDescriptor_alloptions_701d989e418950a7, []int{0} } func (m *StringArg) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_StringArg.Unmarshal(m, b) @@ -68,7 +68,7 @@ func (m *SimpleStringReply) Reset() { *m = SimpleStringReply{} } func (m *SimpleStringReply) String() string { return proto.CompactTextString(m) } func (*SimpleStringReply) ProtoMessage() {} func (*SimpleStringReply) Descriptor() ([]byte, []int) { - return fileDescriptor_alloptions_072d689a4afa170c, []int{1} + return fileDescriptor_alloptions_701d989e418950a7, []int{1} } func (m *SimpleStringReply) XXX_Unmarshal(b []byte) error { return xxx_messageInfo_SimpleStringReply.Unmarshal(m, b) @@ -100,34 +100,35 @@ func init() { proto.RegisterType((*SimpleStringReply)(nil), "main.SimpleStringReply") } -func init() { proto.RegisterFile("alloptions.proto", fileDescriptor_alloptions_072d689a4afa170c) } +func init() { proto.RegisterFile("alloptions.proto", fileDescriptor_alloptions_701d989e418950a7) } -var fileDescriptor_alloptions_072d689a4afa170c = []byte{ - // 416 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x93, 0xc1, 0x6e, 0xd3, 0x30, - 0x1c, 0xc6, 0x17, 0x08, 0xb0, 0x1a, 0xb6, 0x14, 0x0f, 0x41, 0x1a, 0x21, 0x81, 0xc2, 0x65, 0x48, - 0x90, 0x69, 0xe1, 0xc4, 0x71, 0xec, 0x80, 0x84, 0x94, 0x09, 0x35, 0x12, 0x3b, 0x56, 0xae, 0x6b, - 0x65, 0x46, 0xb1, 0x9d, 0xd9, 0xff, 0x4c, 0xe2, 0xda, 0x63, 0x4f, 0x3d, 0xf2, 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x97, 0xcc, 0x00, 0xf6, 0xec, 0xe6, 0x72, 0xb0, 0x3d, 0x7b, 0x0f, 0x9f, 0x21, - 0x2f, 0x81, 0x14, 0x34, 0x23, 0x82, 0x8d, 0xfe, 0xb7, 0xba, 0x3b, 0x9d, 0xf5, 0x1c, 0x7c, 0x8e, - 0x9e, 0x2e, 0xba, 0x5e, 0xaf, 0x3b, 0xad, 0xb4, 0xfc, 0x87, 0x49, 0x80, 0x7f, 0xcc, 0xfd, 0x43, - 0xda, 0x30, 0x1d, 0x18, 0x0b, 0x35, 0xfe, 0xe5, 0x34, 0xa4, 0x5b, 0xc6, 0x9f, 0x89, 0x26, 0xc2, - 0xe0, 0x4f, 0xe8, 0x28, 0x81, 0x4b, 0x0e, 0x57, 0xeb, 0xe3, 0x9d, 0xa2, 0x0e, 0x27, 0xb3, 0xde, - 0x5d, 0x51, 0x9c, 0xda, 0x5b, 0x8c, 0x8f, 0x51, 0xc7, 0x72, 0xdb, 0x2c, 0x7b, 0xf0, 0x97, 0xa0, - 0xc5, 0xf4, 0x11, 0xe1, 0x15, 0xc2, 0x97, 0x6d, 0xe8, 0xee, 0xa0, 0x1f, 0xb4, 0xc9, 0xc1, 0xa3, - 0x9f, 0x73, 0x7f, 0x9f, 0xe6, 0x9c, 0x49, 0xe0, 0xa3, 0x38, 0x41, 0xdd, 0xe5, 0xe1, 0x94, 0xe9, - 0x1b, 0x4e, 0xd9, 0x2d, 0x3e, 0xe6, 0x87, 0xe7, 0xe3, 0xca, 0x77, 0xb5, 0x52, 0x30, 0xa9, 0xfc, - 0x7d, 0x2e, 0x0d, 0x10, 0x49, 0xd9, 0xb4, 0xf2, 0x9d, 0xef, 0x95, 0xef, 0x0c, 0xef, 0x37, 0xff, - 0xc2, 0xbb, 0x3f, 0x01, 0x00, 0x00, 0xff, 0xff, 0x78, 0x58, 0xb6, 0x0a, 0x4a, 0x03, 0x00, 0x00, +var fileDescriptor_alloptions_701d989e418950a7 = []byte{ + // 429 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x53, 0x31, 0x6f, 0xd4, 0x30, + 0x14, 0x6e, 0xe0, 0x80, 0xde, 0x83, 0x36, 0x87, 0x8b, 0x20, 0x17, 0x21, 0x51, 0x85, 0xa5, 0x48, + 0x90, 0xaa, 0x61, 0x62, 0x2c, 0x1d, 0x90, 0x90, 0x52, 0xa1, 0x8b, 0x44, 0x07, 0x86, 0x93, 0xcf, + 0x67, 0xa5, 0x46, 0xb1, 0x9d, 0xda, 0x2f, 0x95, 0x58, 0x3b, 0x76, 0xea, 0xc8, 0xef, 0x38, 0x29, + 0x1b, 0x13, 0x23, 0x7f, 0x84, 0x1f, 0xd0, 0x3f, 0x80, 0x12, 0xa7, 0x47, 0xef, 0xa4, 0x8a, 0x03, + 0x96, 0xc4, 0x79, 0xfe, 0xfc, 0x7d, 0xef, 0x7d, 0x5f, 0x0c, 0x03, 0x5a, 0x14, 0xba, 0x44, 0xa1, + 0x95, 0x8d, 0x4b, 0xa3, 0x51, 0x93, 0x9e, 0xa4, 0x42, 0x85, 0xcf, 0x73, 0x81, 0xc7, 0xd5, 0x24, + 0x66, 0x5a, 0xee, 0x2a, 0x8a, 0xf6, 0x95, 0x29, 0xd9, 0xae, 0xba, 0x7a, 0x38, 0x68, 0xf4, 0x0c, + 0xfa, 0x19, 0x1a, 0xa1, 0xf2, 0x7d, 0x93, 0x13, 0x02, 0x3d, 0x6a, 0xf2, 0xbd, 0xc0, 0xdb, 0xf6, + 0x76, 0xfa, 0xa3, 0x76, 0x1d, 0xbd, 0x80, 0x87, 0x99, 0x90, 0x65, 0xc1, 0x1d, 0x6c, 0xc4, 0xcb, + 0xe2, 0x0b, 0x79, 0x04, 0x77, 0x4c, 0xb3, 0xe8, 0x90, 0xee, 0x23, 0xf9, 0x79, 0x0b, 0x06, 0xd9, + 0x29, 0x3b, 0xa8, 0x2c, 0x6a, 0x99, 0x55, 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, 0xf8, 0x71, 0x19, 0xac, + 0xb3, 0x42, 0x70, 0x85, 0x62, 0x9a, 0xa4, 0x30, 0x98, 0x1f, 0xce, 0xb8, 0x39, 0x15, 0x8c, 0xff, + 0xc7, 0x9f, 0xf2, 0xf6, 0xe9, 0x59, 0x1d, 0xf4, 0x8c, 0xd6, 0x78, 0x5e, 0x07, 0xeb, 0x42, 0x59, + 0xa4, 0x8a, 0xf1, 0x8b, 0x3a, 0xf0, 0xbe, 0xd6, 0x81, 0x37, 0xb9, 0xdb, 0x5e, 0xb4, 0xd7, 0xbf, + 0x02, 0x00, 0x00, 0xff, 0xff, 0xd6, 0x51, 0xa4, 0x75, 0xa7, 0x03, 0x00, 0x00, } diff --git a/examples/alloptions/alloptions.proto b/examples/alloptions/alloptions.proto index ff36699..f7fc4a0 100644 --- a/examples/alloptions/alloptions.proto +++ b/examples/alloptions/alloptions.proto @@ -34,6 +34,11 @@ service SvcSubjectParams { option (nrpc.methodSubjectParams) = "mp1"; option (nrpc.methodSubjectParams) = "mp2"; } + rpc MtStreamedReplyWithSubjectParams(nrpc.Void) returns (SimpleStringReply) { + option (nrpc.streamedReply) = true; + option (nrpc.methodSubjectParams) = "mp1"; + option (nrpc.methodSubjectParams) = "mp2"; + } rpc MtNoReply(nrpc.Void) returns (nrpc.NoReply) {} rpc MtNoRequestWParams(nrpc.NoRequest) returns (SimpleStringReply) { option (nrpc.methodSubjectParams) = "mp1"; diff --git a/examples/alloptions/alloptions_test.go b/examples/alloptions/alloptions_test.go index 1cf6d32..9766e75 100644 --- a/examples/alloptions/alloptions_test.go +++ b/examples/alloptions/alloptions_test.go @@ -90,6 +90,14 @@ func (s BasicServerImpl) MtWithSubjectParams( return } +func (s BasicServerImpl) MtStreamedReplyWithSubjectParams( + ctx context.Context, mp1 string, mp2 string, send func(rep SimpleStringReply), +) error { + send(SimpleStringReply{Reply: mp1}) + send(SimpleStringReply{Reply: mp2}) + return nil +} + func TestAll(t *testing.T) { c, err := nats.Connect(natsURL) if err != nil { @@ -221,6 +229,25 @@ func TestAll(t *testing.T) { t.Errorf("Expected a nrpc.Error, got %#v", err) } + t.Run("StreamedReply with SubjectParams", func(t *testing.T) { + var resList []string + err := c2.MtStreamedReplyWithSubjectParams( + context.Background(), + "arg1", "arg2", + func(ctx context.Context, rep SimpleStringReply) { + resList = append(resList, rep.GetReply()) + }) + if err != nil { + t.Fatal(err) + } + if resList[0] != "arg1" { + t.Errorf("Expected 'arg1', got '%s'", resList[0]) + } + if resList[1] != "arg2" { + t.Errorf("Expected 'arg2', got '%s'", resList[1]) + } + }) + t.Run("NoRequest method with params", func(t *testing.T) { sub, err := c2.MtNoRequestWParamsSubscribeSync( "mtvalue", diff --git a/protoc-gen-nrpc/tmpl.go b/protoc-gen-nrpc/tmpl.go index 263add9..0b07105 100644 --- a/protoc-gen-nrpc/tmpl.go +++ b/protoc-gen-nrpc/tmpl.go @@ -158,7 +158,10 @@ func (h *{{$serviceName}}Handler) {{.GetName}}Publish( {{- if HasStreamedReply .}} func (h *{{$serviceName}}Handler) {{.GetName}}Handler(ctx context.Context, tail []string, msg *nats.Msg) { - _, encoding, err := nrpc.ParseSubjectTail({{len (GetMethodSubjectParams .)}}, tail) + {{if ne 0 (len (GetMethodSubjectParams .)) -}} + mtParams + {{- else -}}_{{- end -}} + , encoding, err := nrpc.ParseSubjectTail({{len (GetMethodSubjectParams .)}}, tail) if err != nil { log.Printf("{{$serviceName}}: {{.GetName}} subject parsing failed:") } @@ -179,8 +182,8 @@ func (h *{{$serviceName}}Handler) {{.GetName}}Handler(ctx context.Context, tail _, nrpcErr := nrpc.CaptureErrors(func() (proto.Message, error) { err := h.server.{{.GetName}}(ctx - {{- range GetMethodSubjectParams . -}} - , {{.}} + {{- range $i, $p := GetMethodSubjectParams . -}} + , mtParams[{{ $i }}] {{- end -}} {{- if ne .GetInputType ".nrpc.Void" -}} , req