-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add bidirectional streaming support by running Send() and Recv() concurrently #170
Changes from 3 commits
a5064d5
1725708
d6f3500
c6357e8
6e38833
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -117,7 +117,9 @@ var _ = utilities.NewDoubleArray | |
`)) | ||
|
||
handlerTemplate = template.Must(template.New("handler").Parse(` | ||
{{if .Method.GetClientStreaming}} | ||
{{if and .Method.GetClientStreaming .Method.GetServerStreaming}} | ||
{{template "bidi-streaming-request-func" .}} | ||
{{else if .Method.GetClientStreaming}} | ||
{{template "client-streaming-request-func" .}} | ||
{{else}} | ||
{{template "client-rpc-request-func" .}} | ||
|
@@ -234,6 +236,49 @@ var ( | |
{{end}} | ||
}`)) | ||
|
||
_ = template.Must(handlerTemplate.New("bidi-streaming-request-func").Parse(` | ||
{{template "request-func-signature" .}} { | ||
var metadata runtime.ServerMetadata | ||
stream, err := client.{{.Method.GetName}}(ctx) | ||
if err != nil { | ||
grpclog.Printf("Failed to start streaming: %v", err) | ||
return nil, metadata, err | ||
} | ||
dec := marshaler.NewDecoder(req.Body) | ||
handleSend := func() error { | ||
var protoReq {{.Method.RequestType.GoType .Method.Service.File.GoPkg.Path}} | ||
err = dec.Decode(&protoReq) | ||
if err != nil { | ||
grpclog.Printf("Failed to decode request: %v", err) | ||
return err | ||
} | ||
if err = stream.Send(&protoReq); err != nil { | ||
grpclog.Printf("Failed to send request: %v", err) | ||
return err | ||
} | ||
return nil | ||
} | ||
firstResult := handleSend() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why don't you return here on error? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did that to allow the goroutine to start so CloseSend would be called. would you prefer that be done explicitly and return before launching the goroutine? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. Could you do so? I am worrying about that the error from |
||
go func() { | ||
for { | ||
if err := handleSend(); err != nil { | ||
break | ||
} | ||
} | ||
if err := stream.CloseSend(); err != nil { | ||
grpclog.Printf("Failed to terminate client stream: %v", err) | ||
} | ||
}() | ||
header, err := stream.Header() | ||
if err != nil { | ||
grpclog.Printf("Failed to get header from client: %v", err) | ||
return nil, metadata, err | ||
} | ||
metadata.HeaderMD = header | ||
return stream, metadata, firstResult | ||
} | ||
`)) | ||
|
||
trailerTemplate = template.Must(template.New("trailer").Parse(` | ||
{{range $svc := .}} | ||
// Register{{$svc.GetName}}HandlerFromEndpoint is same as Register{{$svc.GetName}}Handler but | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if err := dec.Decode(&protoReq); err != nil
would be more consistent?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i was following existing style from the 'client-streaming-request-func' template.