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. | |
|
dnj (Google)
2016/01/22 04:06:23
Just uploaded a CL that changed how Retry works. N
| |
| 20 | |
| 21 // UserAgent is the value of User-Agent HTTP header. | |
| 22 // If empty, DefaultUserAgent is used. | |
| 23 UserAgent string | |
| 24 Insecure bool // if true, use HTTP instead of HTTPS. | |
| 25 | |
| 26 // the rest can be set only using CallOption. | |
| 27 | |
| 28 resHeaderMetadata *metadata.MD // destination for response HTTP headers . | |
| 29 resTrailerMetadata *metadata.MD // destination for response HTTP trailer s. | |
| 30 } | |
| 31 | |
| 32 // DefaultOptions are used if no options are specified in Client. | |
| 33 func DefaultOptions() *Options { | |
| 34 return &Options{ | |
| 35 Retry: func() retry.Iterator { | |
| 36 return &retry.ExponentialBackoff{ | |
| 37 Limited: retry.Limited{ | |
| 38 Delay: time.Second, | |
| 39 Retries: 5, | |
| 40 }, | |
| 41 } | |
| 42 }, | |
| 43 } | |
| 44 } | |
| 45 | |
| 46 func (o *Options) apply(callOptions []grpc.CallOption) { | |
| 47 for _, co := range callOptions { | |
| 48 prpcCo, ok := co.(CallOption) | |
| 49 if !ok { | |
| 50 panic(fmt.Errorf("non-pRPC call option %T is used with p RPC client", co)) | |
| 51 } | |
| 52 prpcCo.apply(o) | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 // CallOption mutates Options. | |
| 57 type CallOption struct { | |
| 58 grpc.CallOption | |
| 59 // apply mutates options. | |
| 60 apply func(*Options) | |
| 61 } | |
| 62 | |
| 63 // Header returns a CallOption that retrieves the header metadata. | |
| 64 // Can be used instead of with grpc.Header. | |
| 65 func Header(md *metadata.MD) *CallOption { | |
| 66 return &CallOption{ | |
| 67 grpc.Header(md), | |
| 68 func(o *Options) { | |
| 69 o.resHeaderMetadata = md | |
| 70 }, | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 // Trailer returns a CallOption that retrieves the trailer metadata. | |
| 75 // Can be used instead of grpc.Trailer. | |
| 76 func Trailer(md *metadata.MD) *CallOption { | |
| 77 return &CallOption{ | |
| 78 grpc.Trailer(md), | |
| 79 func(o *Options) { | |
| 80 o.resTrailerMetadata = md | |
| 81 }, | |
| 82 } | |
| 83 } | |
| OLD | NEW |