@@ -160,46 +160,75 @@ type EmptyCallOption struct{}
160
160
func (EmptyCallOption ) before (* callInfo ) error { return nil }
161
161
func (EmptyCallOption ) after (* callInfo ) {}
162
162
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
-
173
163
// Header returns a CallOptions that retrieves the header metadata
174
164
// 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.
175
168
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
181
184
}
182
185
183
186
// Trailer returns a CallOptions that retrieves the trailer metadata
184
187
// 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.
185
191
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
191
207
}
192
208
193
209
// Peer returns a CallOption that retrieves peer information for a
194
210
// unary RPC.
211
+ //
212
+ // The returned CallOption provides a PeerAddr() method that can
213
+ // be used by interceptors to recover the given peer address.
195
214
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
201
227
}
202
- })
228
+ }
229
+ }
230
+ func (o peerCallOption ) PeerAddr () * peer.Peer {
231
+ return o .p
203
232
}
204
233
205
234
// FailFast configures the action to take when an RPC is attempted on broken
@@ -212,48 +241,107 @@ func Peer(p *peer.Peer) CallOption {
212
241
// https://github.com/grpc/grpc/blob/master/doc/wait-for-ready.md.
213
242
//
214
243
// 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.
215
247
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 )
220
260
}
221
261
222
262
// 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.
223
266
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 )
228
280
}
229
281
230
282
// 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.
231
286
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 )
236
300
}
237
301
238
302
// PerRPCCredentials returns a CallOption that sets credentials.PerRPCCredentials
239
303
// for a call.
304
+ //
305
+ // The returned CallOption provides a PerRPCCredentials() method that can be used
306
+ // by interceptors to recover the credentials.
240
307
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
245
322
}
246
323
247
324
// UseCompressor returns a CallOption which sets the compressor used when
248
325
// sending the request. If WithCompressor is also set, UseCompressor has
249
326
// higher priority.
250
327
//
251
328
// 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.
252
332
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 )
257
345
}
258
346
259
347
// CallContentSubtype returns a CallOption that will set the content-subtype
@@ -272,12 +360,22 @@ func UseCompressor(name string) CallOption {
272
360
// If CallCustomCodec is also used, that Codec will be used for all request and
273
361
// response messages, with the content-subtype set to the given contentSubtype
274
362
// here for requests.
363
+ //
364
+ // The returned CallOption provides a ContentSubtype() method that can be used
365
+ // by interceptors to recover the given setting.
275
366
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 )
281
379
}
282
380
283
381
// CallCustomCodec returns a CallOption that will set the given Codec to be
@@ -292,11 +390,24 @@ func CallContentSubtype(contentSubtype string) CallOption {
292
390
//
293
391
// This function is provided for advanced users; prefer to use only
294
392
// 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.
295
396
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
300
411
}
301
412
302
413
// The format of the payload: compressed or not?
0 commit comments