Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package prpc | |
| 6 | |
| 7 import ( | |
| 8 "fmt" | |
| 9 "time" | |
| 10 | |
| 11 "google.golang.org/grpc" | |
| 12 "google.golang.org/grpc/metadata" | |
| 13 | |
| 14 "github.com/luci/luci-go/common/retry" | |
| 15 ) | |
| 16 | |
| 17 // Options controls how RPC requests are sent. | |
| 18 type Options struct { | |
| 19 Retry func() retry.Iterator // RPC retrial. | |
| 20 UserAgent string // value of User-Agent HTTP header. | |
| 21 Insecure bool // if true, use HTTP instead of HTTPS. | |
| 22 | |
| 23 // the rest can be set only using CallOption. | |
| 24 | |
| 25 resHeaderMetadata *metadata.MD // destination for response HTTP headers . | |
| 26 resTrailerMetadata *metadata.MD // destination for response HTTP trailer s. | |
| 27 } | |
| 28 | |
| 29 // DefaultOptions are used if no options are specified in Client. | |
| 30 func DefaultOptions() *Options { | |
| 31 return &Options{ | |
| 32 Retry: func() retry.Iterator { | |
| 33 return &retry.ExponentialBackoff{ | |
| 34 Limited: retry.Limited{ | |
| 35 Delay: time.Second, | |
| 36 Retries: 5, | |
| 37 }, | |
| 38 } | |
| 39 }, | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 func (o *Options) apply(callOptions []grpc.CallOption) { | |
| 44 for _, co := range callOptions { | |
| 45 prpcCo, ok := co.(CallOption) | |
| 46 if !ok { | |
| 47 panic(fmt.Errorf("non-pRPC call option %T is used with p RPC client", co)) | |
| 48 } | |
| 49 prpcCo.Apply(o) | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 // CallOption mutates Options. | |
| 54 type CallOption struct { | |
| 55 grpc.CallOption | |
| 56 // Apply mutates options. | |
| 57 Apply func(*Options) | |
|
dnj
2016/01/21 07:44:28
Should this be internal "apply"?
nodir
2016/01/22 00:47:24
I don't mind
| |
| 58 } | |
| 59 | |
| 60 // Header returns a CallOption that retrieves the header metadata. | |
| 61 // Can be used instead of with grpc.Header. | |
| 62 func Header(md *metadata.MD) *CallOption { | |
| 63 return &CallOption{ | |
| 64 grpc.Header(md), | |
| 65 func(o *Options) { | |
| 66 o.resHeaderMetadata = md | |
| 67 }, | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 // Trailer returns a CallOption that retrieves the trailer metadata. | |
| 72 // Can be used instead of grpc.Trailer. | |
| 73 func Trailer(md *metadata.MD) *CallOption { | |
| 74 return &CallOption{ | |
| 75 grpc.Trailer(md), | |
| 76 func(o *Options) { | |
| 77 o.resTrailerMetadata = md | |
| 78 }, | |
| 79 } | |
| 80 } | |
| OLD | NEW |