Skip to content

Commit 6492fec

Browse files
committed
call options provide methods so that interceptors in other packages can recover the configuration
1 parent 0843fd0 commit 6492fec

File tree

1 file changed

+166
-55
lines changed

1 file changed

+166
-55
lines changed

rpc_util.go

Lines changed: 166 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -160,46 +160,75 @@ type EmptyCallOption struct{}
160160
func (EmptyCallOption) before(*callInfo) error { return nil }
161161
func (EmptyCallOption) after(*callInfo) {}
162162

163-
type beforeCall func(c *callInfo) error
164-
165-
func (o beforeCall) before(c *callInfo) error { return o(c) }
166-
func (o beforeCall) after(c *callInfo) {}
167-
168-
type afterCall func(c *callInfo)
169-
170-
func (o afterCall) before(c *callInfo) error { return nil }
171-
func (o afterCall) after(c *callInfo) { o(c) }
172-
173163
// Header returns a CallOptions that retrieves the header metadata
174164
// for a unary RPC.
165+
//
166+
// The returned CallOption provides a HeaderAddr() method that can
167+
// be used by interceptors to recover the given metadata address.
175168
func Header(md *metadata.MD) CallOption {
176-
return afterCall(func(c *callInfo) {
177-
if c.stream != nil {
178-
*md, _ = c.stream.Header()
179-
}
180-
})
169+
return headerCallOption{md: md}
170+
}
171+
172+
type headerCallOption struct {
173+
md *metadata.MD
174+
}
175+
176+
func (o headerCallOption) before(c *callInfo) error { return nil }
177+
func (o headerCallOption) after(c *callInfo) {
178+
if c.stream != nil {
179+
*o.md, _ = c.stream.Header()
180+
}
181+
}
182+
func (o headerCallOption) HeaderAddr() *metadata.MD {
183+
return o.md
181184
}
182185

183186
// Trailer returns a CallOptions that retrieves the trailer metadata
184187
// for a unary RPC.
188+
//
189+
// The returned CallOption provides a TrailerAddr() method that can
190+
// be used by interceptors to recover the given metadata address.
185191
func Trailer(md *metadata.MD) CallOption {
186-
return afterCall(func(c *callInfo) {
187-
if c.stream != nil {
188-
*md = c.stream.Trailer()
189-
}
190-
})
192+
return trailerCallOption{md: md}
193+
}
194+
195+
type trailerCallOption struct {
196+
md *metadata.MD
197+
}
198+
199+
func (o trailerCallOption) before(c *callInfo) error { return nil }
200+
func (o trailerCallOption) after(c *callInfo) {
201+
if c.stream != nil {
202+
*o.md = c.stream.Trailer()
203+
}
204+
}
205+
func (o trailerCallOption) TrailerAddr() *metadata.MD {
206+
return o.md
191207
}
192208

193209
// Peer returns a CallOption that retrieves peer information for a
194210
// unary RPC.
211+
//
212+
// The returned CallOption provides a PeerAddr() method that can
213+
// be used by interceptors to recover the given peer address.
195214
func Peer(p *peer.Peer) CallOption {
196-
return afterCall(func(c *callInfo) {
197-
if c.stream != nil {
198-
if x, ok := peer.FromContext(c.stream.Context()); ok {
199-
*p = *x
200-
}
215+
return peerCallOption{p: p}
216+
}
217+
218+
type peerCallOption struct {
219+
p *peer.Peer
220+
}
221+
222+
func (o peerCallOption) before(c *callInfo) error { return nil }
223+
func (o peerCallOption) after(c *callInfo) {
224+
if c.stream != nil {
225+
if x, ok := peer.FromContext(c.stream.Context()); ok {
226+
*o.p = *x
201227
}
202-
})
228+
}
229+
}
230+
func (o peerCallOption) PeerAddr() *peer.Peer {
231+
return o.p
203232
}
204233

205234
// FailFast configures the action to take when an RPC is attempted on broken
@@ -212,48 +241,107 @@ func Peer(p *peer.Peer) CallOption {
212241
// https://github.com/grpc/grpc/blob/master/doc/wait-for-ready.md.
213242
//
214243
// By default, RPCs are "Fail Fast".
244+
//
245+
// The returned CallOption provides a FailFast() method that can
246+
// be used by interceptors to recover the setting.
215247
func FailFast(failFast bool) CallOption {
216-
return beforeCall(func(c *callInfo) error {
217-
c.failFast = failFast
218-
return nil
219-
})
248+
return failFastCallOption(failFast)
249+
}
250+
251+
type failFastCallOption bool
252+
253+
func (o failFastCallOption) before(c *callInfo) error {
254+
c.failFast = bool(o)
255+
return nil
256+
}
257+
func (o failFastCallOption) after(c *callInfo) { return }
258+
func (o failFastCallOption) FailFast() bool {
259+
return bool(o)
220260
}
221261

222262
// MaxCallRecvMsgSize returns a CallOption which sets the maximum message size the client can receive.
263+
//
264+
// The returned CallOption provides a MaxRecvMsgSize() method that can be used by interceptors to
265+
// recover the setting.
223266
func MaxCallRecvMsgSize(s int) CallOption {
224-
return beforeCall(func(o *callInfo) error {
225-
o.maxReceiveMessageSize = &s
226-
return nil
227-
})
267+
return maxRecvMsgSizeCallOption(s)
268+
}
269+
270+
type maxRecvMsgSizeCallOption int
271+
272+
func (o maxRecvMsgSizeCallOption) before(c *callInfo) error {
273+
s := int(o)
274+
c.maxReceiveMessageSize = &s
275+
return nil
276+
}
277+
func (o maxRecvMsgSizeCallOption) after(c *callInfo) { return }
278+
func (o maxRecvMsgSizeCallOption) MaxRecvMsgSize() int {
279+
return int(o)
228280
}
229281

230282
// MaxCallSendMsgSize returns a CallOption which sets the maximum message size the client can send.
283+
//
284+
// The returned CallOption provides a MaxSendMsgSize() method that can be used by interceptors to
285+
// recover the setting.
231286
func MaxCallSendMsgSize(s int) CallOption {
232-
return beforeCall(func(o *callInfo) error {
233-
o.maxSendMessageSize = &s
234-
return nil
235-
})
287+
return maxSendMsgSizeCallOption(s)
288+
}
289+
290+
type maxSendMsgSizeCallOption int
291+
292+
func (o maxSendMsgSizeCallOption) before(c *callInfo) error {
293+
s := int(o)
294+
c.maxSendMessageSize = &s
295+
return nil
296+
}
297+
func (o maxSendMsgSizeCallOption) after(c *callInfo) { return }
298+
func (o maxSendMsgSizeCallOption) MaxSendMsgSize() int {
299+
return int(o)
236300
}
237301

238302
// PerRPCCredentials returns a CallOption that sets credentials.PerRPCCredentials
239303
// for a call.
304+
//
305+
// The returned CallOption provides a PerRPCCredentials() method that can be used
306+
// by interceptors to recover the credentials.
240307
func PerRPCCredentials(creds credentials.PerRPCCredentials) CallOption {
241-
return beforeCall(func(c *callInfo) error {
242-
c.creds = creds
243-
return nil
244-
})
308+
return perRPCCredsCallOption{creds: creds}
309+
}
310+
311+
type perRPCCredsCallOption struct {
312+
creds credentials.PerRPCCredentials
313+
}
314+
315+
func (o perRPCCredsCallOption) before(c *callInfo) error {
316+
c.creds = o.creds
317+
return nil
318+
}
319+
func (o perRPCCredsCallOption) after(c *callInfo) { return }
320+
func (o perRPCCredsCallOption) PerRPCCredentials() credentials.PerRPCCredentials {
321+
return o.creds
245322
}
246323

247324
// UseCompressor returns a CallOption which sets the compressor used when
248325
// sending the request. If WithCompressor is also set, UseCompressor has
249326
// higher priority.
250327
//
251328
// This API is EXPERIMENTAL.
329+
//
330+
// The returned CallOption provides a Compressor() method that can be used
331+
// by interceptors to recover the given name.
252332
func UseCompressor(name string) CallOption {
253-
return beforeCall(func(c *callInfo) error {
254-
c.compressorType = name
255-
return nil
256-
})
333+
return compressorCallOption(name)
334+
}
335+
336+
type compressorCallOption string
337+
338+
func (o compressorCallOption) before(c *callInfo) error {
339+
c.compressorType = string(o)
340+
return nil
341+
}
342+
func (o compressorCallOption) after(c *callInfo) { return }
343+
func (o compressorCallOption) Compressor() string {
344+
return string(o)
257345
}
258346

259347
// CallContentSubtype returns a CallOption that will set the content-subtype
@@ -272,12 +360,22 @@ func UseCompressor(name string) CallOption {
272360
// If CallCustomCodec is also used, that Codec will be used for all request and
273361
// response messages, with the content-subtype set to the given contentSubtype
274362
// here for requests.
363+
//
364+
// The returned CallOption provides a ContentSubtype() method that can be used
365+
// by interceptors to recover the given setting.
275366
func CallContentSubtype(contentSubtype string) CallOption {
276-
contentSubtype = strings.ToLower(contentSubtype)
277-
return beforeCall(func(c *callInfo) error {
278-
c.contentSubtype = contentSubtype
279-
return nil
280-
})
367+
return contentSubtypeCallOption(strings.ToLower(contentSubtype))
368+
}
369+
370+
type contentSubtypeCallOption string
371+
372+
func (o contentSubtypeCallOption) before(c *callInfo) error {
373+
c.contentSubtype = string(o)
374+
return nil
375+
}
376+
func (o contentSubtypeCallOption) after(c *callInfo) { return }
377+
func (o contentSubtypeCallOption) ContentSubtype() string {
378+
return string(o)
281379
}
282380

283381
// CallCustomCodec returns a CallOption that will set the given Codec to be
@@ -292,11 +390,24 @@ func CallContentSubtype(contentSubtype string) CallOption {
292390
//
293391
// This function is provided for advanced users; prefer to use only
294392
// CallContentSubtype to select a registered codec instead.
393+
//
394+
// The returned CallOption provides a Codec() method that can be used by
395+
// interceptors to recover the configured codec.
295396
func CallCustomCodec(codec Codec) CallOption {
296-
return beforeCall(func(c *callInfo) error {
297-
c.codec = codec
298-
return nil
299-
})
397+
return customCodecCallOption{codec: codec}
398+
}
399+
400+
type customCodecCallOption struct {
401+
codec Codec
402+
}
403+
404+
func (o customCodecCallOption) before(c *callInfo) error {
405+
c.codec = o.codec
406+
return nil
407+
}
408+
func (o customCodecCallOption) after(c *callInfo) { return }
409+
func (o customCodecCallOption) Codec() Codec {
410+
return o.codec
300411
}
301412

302413
// The format of the payload: compressed or not?

0 commit comments

Comments
 (0)