Skip to content

Commit fa4e176

Browse files
author
Steve Ayers
committed
Feedback
Signed-off-by: Steve Ayers <[email protected]>
1 parent 9143ba0 commit fa4e176

File tree

8 files changed

+45
-46
lines changed

8 files changed

+45
-46
lines changed

client.go

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func NewClient[Req, Res any](httpClient HTTPClient, url string, options ...Clien
7979
conn := client.protocolClient.NewConn(ctx, unarySpec, request.Header())
8080
conn.onRequestSend(func(r *http.Request) {
8181
request.setRequestMethod(r.Method)
82-
callInfo, ok := getClientCallInfoFromContext(ctx)
82+
callInfo, ok := clientCallInfoFromContext(ctx)
8383
if ok {
8484
callInfo.method = r.Method
8585
}
@@ -116,7 +116,7 @@ func NewClient[Req, Res any](httpClient HTTPClient, url string, options ...Clien
116116
protocolClient.WriteRequestHeader(StreamTypeUnary, request.Header())
117117

118118
// Also set them in the context if there's a call info present
119-
callInfo, callInfoOk := getClientCallInfoFromContext(ctx)
119+
callInfo, callInfoOk := clientCallInfoFromContext(ctx)
120120
if callInfoOk {
121121
callInfo.peer = request.Peer()
122122
callInfo.spec = request.Spec()
@@ -158,19 +158,6 @@ func (c *Client[Req, Res]) CallUnary(ctx context.Context, request *Request[Req])
158158
return c.callUnary(ctx, request)
159159
}
160160

161-
// CallUnarySimple calls a request-response procedure using the function signature
162-
// associated with the "simple" generation option.
163-
//
164-
// This option eliminates the [Request] and [Response] wrappers, and instead uses the
165-
// context.Context to propagate information such as headers.
166-
func (c *Client[Req, Res]) CallUnarySimple(ctx context.Context, request *Req) (*Res, error) {
167-
response, err := c.CallUnary(ctx, NewRequest(request))
168-
if response != nil {
169-
return response.Msg, err
170-
}
171-
return nil, err
172-
}
173-
174161
// CallClientStream calls a client streaming procedure.
175162
func (c *Client[Req, Res]) CallClientStream(ctx context.Context) *ClientStreamForClient[Req, Res] {
176163
if c.err != nil {
@@ -187,7 +174,7 @@ func (c *Client[Req, Res]) CallServerStream(ctx context.Context, request *Reques
187174
if c.err != nil {
188175
return nil, c.err
189176
}
190-
callInfo, callInfoOk := getClientCallInfoFromContext(ctx)
177+
callInfo, callInfoOk := clientCallInfoFromContext(ctx)
191178
// Set values in the context if there's a call info present
192179
if callInfoOk {
193180
// Copy the call info into a sentinel value. This is so we can compare
@@ -231,15 +218,6 @@ func (c *Client[Req, Res]) CallServerStream(ctx context.Context, request *Reques
231218
}, nil
232219
}
233220

234-
// CallServerStreamSimple calls a server streaming procedure using the function signature
235-
// associated with the "simple" generation option.
236-
//
237-
// This option eliminates the [Request] wrapper, and instead uses the context.Context to
238-
// propagate information such as headers.
239-
func (c *Client[Req, Res]) CallServerStreamSimple(ctx context.Context, requestMsg *Req) (*ServerStreamForClient[Res], error) {
240-
return c.CallServerStream(ctx, NewRequest(requestMsg))
241-
}
242-
243221
// CallBidiStream calls a bidirectional streaming procedure.
244222
func (c *Client[Req, Res]) CallBidiStream(ctx context.Context) *BidiStreamForClient[Req, Res] {
245223
if c.err != nil {

cmd/protoc-gen-connect-go/internal/testdata/simple/gen/genconnect/simple.connect.go

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/protoc-gen-connect-go/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,15 +384,19 @@ func generateClientMethod(g *protogen.GeneratedFile, method *protogen.Method, na
384384
g.P("return c.", unexport(method.GoName), ".CallClientStream(ctx)")
385385
case !isStreamingClient && isStreamingServer:
386386
if simple {
387-
g.P("return c.", unexport(method.GoName), ".CallServerStreamSimple(ctx, req)")
387+
g.P("return c.", unexport(method.GoName), ".CallServerStream(ctx, ", connectPackage.Ident("NewRequest"), "(req))")
388388
} else {
389389
g.P("return c.", unexport(method.GoName), ".CallServerStream(ctx, req)")
390390
}
391391
case isStreamingClient && isStreamingServer:
392392
g.P("return c.", unexport(method.GoName), ".CallBidiStream(ctx)")
393393
default:
394394
if simple {
395-
g.P("return c.", unexport(method.GoName), ".CallUnarySimple(ctx, req)")
395+
g.P("response, err := c.", unexport(method.GoName), ".CallUnary(ctx, ", connectPackage.Ident("NewRequest"), "(req))")
396+
g.P("if response != nil {")
397+
g.P("return response.Msg, err")
398+
g.P("}")
399+
g.P("return nil, err")
396400
} else {
397401
g.P("return c.", unexport(method.GoName), ".CallUnary(ctx, req)")
398402
}

connect.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,8 @@ type hasHTTPMethod interface {
368368
getHTTPMethod() string
369369
}
370370

371-
// errStreamingClientConn is a sentinel error implementation of StreamingClientConn
371+
// errStreamingClientConn is a sentinel error implementation of StreamingClientConn.
372372
type errStreamingClientConn struct {
373-
StreamingClientConn
374373
err error
375374
}
376375

context.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,13 @@ func (w *responseWrapper[Res]) ResponseTrailer() http.Header {
232232
return w.response.Trailer()
233233
}
234234

235-
// Gets a client (i.e. outgoing) call info from context.
236-
func getClientCallInfoFromContext(ctx context.Context) (*clientCallInfo, bool) {
235+
// clientCallInfoFromContext gets the call info from a client/outgoing context.
236+
func clientCallInfoFromContext(ctx context.Context) (*clientCallInfo, bool) {
237237
info, ok := ctx.Value(clientCallInfoContextKey{}).(*clientCallInfo)
238238
return info, ok
239239
}
240240

241-
// newHandlerContext creates a new handler (i.e. incoming) context.
241+
// newHandlerContext creates a new handler/incoming context.
242242
func newHandlerContext(ctx context.Context, info CallInfo) context.Context {
243243
return context.WithValue(ctx, handlerCallInfoContextKey{}, info)
244244
}

interceptor.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,23 +114,25 @@ func (c *chain) WrapStreamingHandler(next StreamingHandlerFunc) StreamingHandler
114114

115115
func unaryThunk(next UnaryFunc) UnaryFunc {
116116
return func(ctx context.Context, req AnyRequest) (AnyResponse, error) {
117-
if !checkSentinel(ctx) {
118-
return nil, errNewClientContextProhibited
117+
if err := checkSentinel(ctx); err != nil {
118+
return nil, err
119119
}
120120
return next(ctx, req)
121121
}
122122
}
123123

124124
func streamingClientThunk(next StreamingClientFunc) StreamingClientFunc {
125125
return func(ctx context.Context, spec Spec) StreamingClientConn {
126-
if !checkSentinel(ctx) {
127-
return &errStreamingClientConn{err: errNewClientContextProhibited}
126+
if err := checkSentinel(ctx); err != nil {
127+
return &errStreamingClientConn{err: err}
128128
}
129129
return next(ctx, spec)
130130
}
131131
}
132132

133-
func checkSentinel(ctx context.Context) bool {
134-
// Only verify if there's a sentinel call info to compare it to
135-
return ctx.Value(clientCallInfoContextKey{}) == ctx.Value(sentinelContextKey{})
133+
func checkSentinel(ctx context.Context) error {
134+
if ctx.Value(clientCallInfoContextKey{}) != ctx.Value(sentinelContextKey{}) {
135+
return errNewClientContextProhibited
136+
}
137+
return nil
136138
}

internal/gen/simple/connect/collide/v1/collidev1connect/collide.connect.go

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/gen/simple/connect/ping/v1/pingv1connect/ping.connect.go

Lines changed: 11 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)