Jiri Simsa | d7616c9 | 2015-03-24 23:44:30 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 5 | package rpc |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 6 | |
| 7 | import ( |
Asim Shankar | b54d764 | 2014-06-05 13:08:04 -0700 | [diff] [blame] | 8 | "fmt" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 9 | "io" |
David Why Use Two When One Will Do Presotto | 06c60f0 | 2014-09-11 14:52:19 -0700 | [diff] [blame] | 10 | "math/rand" |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 11 | "net" |
Suharsh Sivakumar | 67ef84a | 2015-02-13 13:04:44 -0800 | [diff] [blame] | 12 | "reflect" |
David Why Use Two When One Will Do Presotto | 06c60f0 | 2014-09-11 14:52:19 -0700 | [diff] [blame] | 13 | "strings" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 14 | "sync" |
| 15 | "time" |
| 16 | |
Cosmos Nicolaou | b291d6c | 2015-03-27 09:01:46 -0700 | [diff] [blame] | 17 | "v.io/x/lib/vlog" |
| 18 | |
Suharsh Sivakumar | 2ad4e10 | 2015-03-17 21:23:37 -0700 | [diff] [blame] | 19 | "v.io/v23" |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 20 | "v.io/v23/context" |
| 21 | "v.io/v23/i18n" |
Todd Wang | 5082a55 | 2015-04-02 10:56:11 -0700 | [diff] [blame] | 22 | "v.io/v23/namespace" |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 23 | "v.io/v23/naming" |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 24 | "v.io/v23/rpc" |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 25 | "v.io/v23/security" |
| 26 | "v.io/v23/vdl" |
Todd Wang | f6a0688 | 2015-02-27 17:38:01 -0800 | [diff] [blame] | 27 | vtime "v.io/v23/vdlroot/time" |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 28 | "v.io/v23/verror" |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 29 | "v.io/v23/vom" |
| 30 | "v.io/v23/vtrace" |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 31 | |
Matt Rosencrantz | dbc1be2 | 2015-02-28 15:15:49 -0800 | [diff] [blame] | 32 | inaming "v.io/x/ref/profiles/internal/naming" |
Cosmos Nicolaou | b291d6c | 2015-03-27 09:01:46 -0700 | [diff] [blame] | 33 | "v.io/x/ref/profiles/internal/rpc/stream" |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 34 | "v.io/x/ref/profiles/internal/rpc/stream/vc" |
| 35 | "v.io/x/ref/profiles/internal/rpc/version" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 36 | ) |
| 37 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 38 | const pkgPath = "v.io/x/ref/profiles/internal/rpc" |
Cosmos Nicolaou | 112bf1c | 2014-11-21 15:43:11 -0800 | [diff] [blame] | 39 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 40 | var ( |
Cosmos Nicolaou | 112bf1c | 2014-11-21 15:43:11 -0800 | [diff] [blame] | 41 | // Local errs that are used to provide details to the public ones. |
| 42 | errClientCloseAlreadyCalled = verror.Register(pkgPath+".closeAlreadyCalled", verror.NoRetry, |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 43 | "rpc.Client.Close has already been called") |
Cosmos Nicolaou | 112bf1c | 2014-11-21 15:43:11 -0800 | [diff] [blame] | 44 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 45 | errClientFinishAlreadyCalled = verror.Register(pkgPath+".finishAlreadyCalled", verror.NoRetry, "rpc.ClientCall.Finish has already been called") |
Cosmos Nicolaou | 112bf1c | 2014-11-21 15:43:11 -0800 | [diff] [blame] | 46 | |
| 47 | errNonRootedName = verror.Register(pkgPath+".nonRootedName", verror.NoRetry, "{3} does not appear to contain an address") |
| 48 | |
| 49 | errInvalidEndpoint = verror.Register(pkgPath+".invalidEndpoint", verror.RetryRefetch, "{3} is an invalid endpoint") |
| 50 | |
| 51 | errIncompatibleEndpoint = verror.Register(pkgPath+".invalidEndpoint", verror.RetryRefetch, "{3} is an incompatible endpoint") |
| 52 | |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 53 | errNotTrusted = verror.Register(pkgPath+".notTrusted", verror.NoRetry, "name {3} not trusted using blessings {4}{:5}") |
Cosmos Nicolaou | 112bf1c | 2014-11-21 15:43:11 -0800 | [diff] [blame] | 54 | |
Asim Shankar | 6d5e9e7 | 2015-03-30 18:51:33 -0700 | [diff] [blame] | 55 | errAuthError = verror.Register(pkgPath+".authError", verror.RetryRefetch, "{3}") |
Cosmos Nicolaou | 112bf1c | 2014-11-21 15:43:11 -0800 | [diff] [blame] | 56 | |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 57 | errSystemRetry = verror.Register(pkgPath+".sysErrorRetryConnection", verror.RetryConnection, "{:3:}") |
Cosmos Nicolaou | 112bf1c | 2014-11-21 15:43:11 -0800 | [diff] [blame] | 58 | |
Todd Wang | 34ed4c6 | 2014-11-26 15:15:52 -0800 | [diff] [blame] | 59 | errVomEncoder = verror.Register(pkgPath+".vomEncoder", verror.NoRetry, "failed to create vom encoder {:3}") |
| 60 | errVomDecoder = verror.Register(pkgPath+".vomDecoder", verror.NoRetry, "failed to create vom decoder {:3}") |
| 61 | |
Cosmos Nicolaou | 112bf1c | 2014-11-21 15:43:11 -0800 | [diff] [blame] | 62 | errRequestEncoding = verror.Register(pkgPath+".requestEncoding", verror.NoRetry, "failed to encode request {3}{:4}") |
| 63 | |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 64 | errDischargeEncoding = verror.Register(pkgPath+".dischargeEncoding", verror.NoRetry, "failed to encode discharges {:3}") |
| 65 | |
| 66 | errBlessingEncoding = verror.Register(pkgPath+".blessingEncoding", verror.NoRetry, "failed to encode blessing {3}{:4}") |
Cosmos Nicolaou | 112bf1c | 2014-11-21 15:43:11 -0800 | [diff] [blame] | 67 | |
| 68 | errArgEncoding = verror.Register(pkgPath+".argEncoding", verror.NoRetry, "failed to encode arg #{3}{:4:}") |
| 69 | |
Benjamin Prosnitz | 0db77a2 | 2015-01-20 14:25:15 -0800 | [diff] [blame] | 70 | errMismatchedResults = verror.Register(pkgPath+".mismatchedResults", verror.NoRetry, "got {3} results, but want {4}") |
Cosmos Nicolaou | 112bf1c | 2014-11-21 15:43:11 -0800 | [diff] [blame] | 71 | |
| 72 | errResultDecoding = verror.Register(pkgPath+".resultDecoding", verror.NoRetry, "failed to decode result #{3}{:4}") |
| 73 | |
| 74 | errResponseDecoding = verror.Register(pkgPath+".responseDecoding", verror.NoRetry, "failed to decode response{:3}") |
| 75 | |
| 76 | errRemainingStreamResults = verror.Register(pkgPath+".remaingStreamResults", verror.NoRetry, "stream closed with remaining stream results") |
| 77 | |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 78 | errNoBlessingsForPeer = verror.Register(pkgPath+".noBlessingsForPeer", verror.NoRetry, "no blessings tagged for peer {3}{:4}") |
Suharsh Sivakumar | 67ef84a | 2015-02-13 13:04:44 -0800 | [diff] [blame] | 79 | |
Cosmos Nicolaou | 112bf1c | 2014-11-21 15:43:11 -0800 | [diff] [blame] | 80 | errBlessingGrant = verror.Register(pkgPath+".blessingGrantFailed", verror.NoRetry, "failed to grant blessing to server with blessings {3}{:4}") |
| 81 | |
| 82 | errBlessingAdd = verror.Register(pkgPath+".blessingAddFailed", verror.NoRetry, "failed to add blessing granted to server {3}{:4}") |
Suharsh Sivakumar | 0ed10c2 | 2015-04-06 12:55:55 -0700 | [diff] [blame] | 83 | |
| 84 | errNoPrincipal = verror.Register(pkgPath+".noPrincipal", verror.NoRetry, "principal required for secure connections") |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 85 | ) |
| 86 | |
| 87 | type client struct { |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 88 | streamMgr stream.Manager |
Todd Wang | 5082a55 | 2015-04-02 10:56:11 -0700 | [diff] [blame] | 89 | ns namespace.T |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 90 | vcOpts []stream.VCOpt // vc opts passed to dial |
| 91 | preferredProtocols []string |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 92 | |
Jungho Ahn | 25545d3 | 2015-01-26 15:14:14 -0800 | [diff] [blame] | 93 | // We cache the IP networks on the device since it is not that cheap to read |
| 94 | // network interfaces through os syscall. |
| 95 | // TODO(jhahn): Add monitoring the network interface changes. |
| 96 | ipNets []*net.IPNet |
| 97 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 98 | // We support concurrent calls to StartCall and Close, so we must protect the |
| 99 | // vcMap. Everything else is initialized upon client construction, and safe |
| 100 | // to use concurrently. |
| 101 | vcMapMu sync.Mutex |
Suharsh Sivakumar | ae774a5 | 2015-01-09 14:26:32 -0800 | [diff] [blame] | 102 | vcMap map[vcMapKey]*vcInfo |
Andres Erbsen | b7f95f3 | 2014-07-07 12:07:56 -0700 | [diff] [blame] | 103 | |
Ankur | e49a86a | 2014-11-11 18:52:43 -0800 | [diff] [blame] | 104 | dc vc.DischargeClient |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 107 | var _ rpc.Client = (*client)(nil) |
Benjamin Prosnitz | fdfbf7b | 2014-10-08 09:47:21 -0700 | [diff] [blame] | 108 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 109 | type vcInfo struct { |
| 110 | vc stream.VC |
| 111 | remoteEP naming.Endpoint |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Suharsh Sivakumar | ae774a5 | 2015-01-09 14:26:32 -0800 | [diff] [blame] | 114 | type vcMapKey struct { |
Suharsh Sivakumar | 2ad4e10 | 2015-03-17 21:23:37 -0700 | [diff] [blame] | 115 | endpoint string |
Suharsh Sivakumar | 2c5d810 | 2015-03-23 08:49:12 -0700 | [diff] [blame] | 116 | clientPublicKey string // clientPublicKey = "" means we are running unencrypted (i.e. SecurityNone) |
Suharsh Sivakumar | ae774a5 | 2015-01-09 14:26:32 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Todd Wang | 5082a55 | 2015-04-02 10:56:11 -0700 | [diff] [blame] | 119 | func InternalNewClient(streamMgr stream.Manager, ns namespace.T, opts ...rpc.ClientOpt) (rpc.Client, error) { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 120 | c := &client{ |
Ankur | e49a86a | 2014-11-11 18:52:43 -0800 | [diff] [blame] | 121 | streamMgr: streamMgr, |
| 122 | ns: ns, |
Jungho Ahn | 25545d3 | 2015-01-26 15:14:14 -0800 | [diff] [blame] | 123 | ipNets: ipNetworks(), |
Suharsh Sivakumar | ae774a5 | 2015-01-09 14:26:32 -0800 | [diff] [blame] | 124 | vcMap: make(map[vcMapKey]*vcInfo), |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 125 | } |
Suharsh Sivakumar | 0891858 | 2015-03-03 15:16:36 -0800 | [diff] [blame] | 126 | c.dc = InternalNewDischargeClient(nil, c, 0) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 127 | for _, opt := range opts { |
| 128 | // Collect all client opts that are also vc opts. |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 129 | switch v := opt.(type) { |
| 130 | case stream.VCOpt: |
| 131 | c.vcOpts = append(c.vcOpts, v) |
Suharsh Sivakumar | d7a6519 | 2015-01-27 22:57:15 -0800 | [diff] [blame] | 132 | case PreferredProtocols: |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 133 | c.preferredProtocols = v |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 134 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 135 | } |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 136 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 137 | return c, nil |
| 138 | } |
| 139 | |
Suharsh Sivakumar | 2c5d810 | 2015-03-23 08:49:12 -0700 | [diff] [blame] | 140 | func (c *client) createFlow(ctx *context.T, principal security.Principal, ep naming.Endpoint, vcOpts []stream.VCOpt) (stream.Flow, error) { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 141 | c.vcMapMu.Lock() |
| 142 | defer c.vcMapMu.Unlock() |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 143 | if c.vcMap == nil { |
Todd Wang | ff73e1f | 2015-02-10 21:45:52 -0800 | [diff] [blame] | 144 | return nil, verror.New(errClientCloseAlreadyCalled, ctx) |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 145 | } |
Suharsh Sivakumar | 2ad4e10 | 2015-03-17 21:23:37 -0700 | [diff] [blame] | 146 | vcKey := vcMapKey{endpoint: ep.String()} |
Suharsh Sivakumar | 2ad4e10 | 2015-03-17 21:23:37 -0700 | [diff] [blame] | 147 | if principal != nil { |
| 148 | vcKey.clientPublicKey = principal.PublicKey().String() |
| 149 | } |
Suharsh Sivakumar | ae774a5 | 2015-01-09 14:26:32 -0800 | [diff] [blame] | 150 | if vcinfo := c.vcMap[vcKey]; vcinfo != nil { |
Bogdan Caprita | 783f779 | 2014-05-15 09:29:17 -0700 | [diff] [blame] | 151 | if flow, err := vcinfo.vc.Connect(); err == nil { |
| 152 | return flow, nil |
| 153 | } |
| 154 | // If the vc fails to establish a new flow, we assume it's |
| 155 | // broken, remove it from the map, and proceed to establishing |
| 156 | // a new vc. |
| 157 | // TODO(caprita): Should we distinguish errors due to vc being |
| 158 | // closed from other errors? If not, should we call vc.Close() |
| 159 | // before removing the vc from the map? |
Suharsh Sivakumar | ae774a5 | 2015-01-09 14:26:32 -0800 | [diff] [blame] | 160 | delete(c.vcMap, vcKey) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 161 | } |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 162 | sm := c.streamMgr |
Robin Thellend | ee43964 | 2014-10-20 14:39:17 -0700 | [diff] [blame] | 163 | c.vcMapMu.Unlock() |
Suharsh Sivakumar | 2ad4e10 | 2015-03-17 21:23:37 -0700 | [diff] [blame] | 164 | |
| 165 | vc, err := sm.Dial(ep, principal, vcOpts...) |
Robin Thellend | ee43964 | 2014-10-20 14:39:17 -0700 | [diff] [blame] | 166 | c.vcMapMu.Lock() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 167 | if err != nil { |
Cosmos Nicolaou | 112bf1c | 2014-11-21 15:43:11 -0800 | [diff] [blame] | 168 | if strings.Contains(err.Error(), "authentication failed") { |
Asim Shankar | 6d5e9e7 | 2015-03-30 18:51:33 -0700 | [diff] [blame] | 169 | return nil, verror.New(errAuthError, ctx, err) |
Cosmos Nicolaou | 112bf1c | 2014-11-21 15:43:11 -0800 | [diff] [blame] | 170 | } else { |
Todd Wang | ff73e1f | 2015-02-10 21:45:52 -0800 | [diff] [blame] | 171 | return nil, verror.New(errSystemRetry, ctx, err) |
Cosmos Nicolaou | 112bf1c | 2014-11-21 15:43:11 -0800 | [diff] [blame] | 172 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 173 | } |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 174 | if c.vcMap == nil { |
| 175 | sm.ShutdownEndpoint(ep) |
Todd Wang | ff73e1f | 2015-02-10 21:45:52 -0800 | [diff] [blame] | 176 | return nil, verror.New(errClientCloseAlreadyCalled, ctx) |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 177 | } |
Suharsh Sivakumar | ae774a5 | 2015-01-09 14:26:32 -0800 | [diff] [blame] | 178 | if othervc, exists := c.vcMap[vcKey]; exists { |
Robin Thellend | ee43964 | 2014-10-20 14:39:17 -0700 | [diff] [blame] | 179 | vc = othervc.vc |
| 180 | // TODO(ashankar,toddw): Figure out how to close up the VC that |
| 181 | // is discarded. vc.Close? |
| 182 | } else { |
Suharsh Sivakumar | ae774a5 | 2015-01-09 14:26:32 -0800 | [diff] [blame] | 183 | c.vcMap[vcKey] = &vcInfo{vc: vc, remoteEP: ep} |
Robin Thellend | ee43964 | 2014-10-20 14:39:17 -0700 | [diff] [blame] | 184 | } |
Asim Shankar | 6d5e9e7 | 2015-03-30 18:51:33 -0700 | [diff] [blame] | 185 | return vc.Connect() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Cosmos Nicolaou | b291d6c | 2015-03-27 09:01:46 -0700 | [diff] [blame] | 188 | // A randomized exponential backoff. The randomness deters error convoys |
Matt Rosencrantz | 254d570 | 2015-04-01 09:47:38 -0700 | [diff] [blame] | 189 | // from forming. The first time you retry n should be 0, then 1 etc. |
| 190 | func backoff(n uint, deadline time.Time) bool { |
| 191 | // This is ((100 to 200) * 2^n) ms. |
| 192 | b := time.Duration((100+rand.Intn(100))<<n) * time.Millisecond |
David Why Use Two When One Will Do Presotto | b02cf90 | 2014-09-12 17:22:56 -0700 | [diff] [blame] | 193 | if b > maxBackoff { |
| 194 | b = maxBackoff |
| 195 | } |
| 196 | r := deadline.Sub(time.Now()) |
| 197 | if b > r { |
| 198 | // We need to leave a little time for the call to start or |
| 199 | // we'll just timeout in startCall before we actually do |
| 200 | // anything. If we just have a millisecond left, give up. |
| 201 | if r <= time.Millisecond { |
| 202 | return false |
| 203 | } |
| 204 | b = r - time.Millisecond |
| 205 | } |
| 206 | time.Sleep(b) |
| 207 | return true |
| 208 | } |
| 209 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 210 | func (c *client) StartCall(ctx *context.T, name, method string, args []interface{}, opts ...rpc.CallOpt) (rpc.ClientCall, error) { |
Suharsh Sivakumar | ae774a5 | 2015-01-09 14:26:32 -0800 | [diff] [blame] | 211 | defer vlog.LogCall()() |
| 212 | return c.startCall(ctx, name, method, args, opts) |
| 213 | } |
| 214 | |
Suharsh Sivakumar | 076e953 | 2015-04-09 17:36:25 -0700 | [diff] [blame^] | 215 | func (c *client) Call(ctx *context.T, name, method string, inArgs, outArgs []interface{}, opts ...rpc.CallOpt) error { |
| 216 | defer vlog.LogCall()() |
| 217 | |
| 218 | deadline := getDeadline(ctx, opts) |
| 219 | |
| 220 | var lastErr error |
| 221 | for retries := uint(0); ; retries++ { |
| 222 | call, err := c.startCall(ctx, name, method, inArgs, opts) |
| 223 | if err != nil { |
| 224 | return err |
| 225 | } |
| 226 | err = call.Finish(outArgs...) |
| 227 | if err == nil { |
| 228 | return nil |
| 229 | } |
| 230 | lastErr = err |
| 231 | // We only retry if RetryBackoff is returned by the application because other |
| 232 | // RetryConnection and RetryRefetch required actions by the client before |
| 233 | // retrying. |
| 234 | if !shouldRetryBackoff(verror.Action(lastErr), deadline, opts) { |
| 235 | vlog.Infof("Cannot retry after error: %s", lastErr) |
| 236 | break |
| 237 | } |
| 238 | if !backoff(retries, deadline) { |
| 239 | break |
| 240 | } |
| 241 | vlog.Infof("Retrying due to error: %s", lastErr) |
| 242 | } |
| 243 | return lastErr |
| 244 | } |
| 245 | |
| 246 | func getDeadline(ctx *context.T, opts []rpc.CallOpt) time.Time { |
| 247 | // Context specified deadline. |
| 248 | deadline, hasDeadline := ctx.Deadline() |
| 249 | if !hasDeadline { |
| 250 | // Default deadline. |
| 251 | deadline = time.Now().Add(defaultCallTimeout) |
| 252 | } |
| 253 | if r, ok := getRetryTimeoutOpt(opts); ok { |
| 254 | // Caller specified deadline. |
| 255 | deadline = time.Now().Add(r) |
| 256 | } |
| 257 | return deadline |
| 258 | } |
| 259 | |
| 260 | func shouldRetryBackoff(action verror.ActionCode, deadline time.Time, opts []rpc.CallOpt) bool { |
| 261 | switch { |
| 262 | case noRetry(opts): |
| 263 | return false |
| 264 | case action != verror.RetryBackoff: |
| 265 | return false |
| 266 | case time.Now().After(deadline): |
| 267 | return false |
| 268 | } |
| 269 | return true |
| 270 | } |
| 271 | |
| 272 | func shouldRetry(action verror.ActionCode, deadline time.Time, opts []rpc.CallOpt) bool { |
| 273 | switch { |
| 274 | case noRetry(opts): |
| 275 | return false |
| 276 | case action != verror.RetryConnection && action != verror.RetryRefetch: |
| 277 | return false |
| 278 | case time.Now().After(deadline): |
| 279 | return false |
| 280 | case action == verror.RetryRefetch && getNoNamespaceOpt(opts): |
| 281 | // If we're skipping resolution and there are no servers for |
| 282 | // this call retrying is not going to help, we can't come up |
| 283 | // with new servers if there is no resolution. |
| 284 | return false |
| 285 | } |
| 286 | return true |
| 287 | } |
| 288 | |
Todd Wang | b31da59 | 2015-02-20 12:50:39 -0800 | [diff] [blame] | 289 | func mkDischargeImpetus(serverBlessings []string, method string, args []interface{}) (security.DischargeImpetus, error) { |
Ankur | e49a86a | 2014-11-11 18:52:43 -0800 | [diff] [blame] | 290 | var impetus security.DischargeImpetus |
| 291 | if len(serverBlessings) > 0 { |
| 292 | impetus.Server = make([]security.BlessingPattern, len(serverBlessings)) |
| 293 | for i, b := range serverBlessings { |
| 294 | impetus.Server[i] = security.BlessingPattern(b) |
| 295 | } |
| 296 | } |
| 297 | impetus.Method = method |
| 298 | if len(args) > 0 { |
Todd Wang | b31da59 | 2015-02-20 12:50:39 -0800 | [diff] [blame] | 299 | impetus.Arguments = make([]*vdl.Value, len(args)) |
Ankur | e49a86a | 2014-11-11 18:52:43 -0800 | [diff] [blame] | 300 | for i, a := range args { |
Todd Wang | b31da59 | 2015-02-20 12:50:39 -0800 | [diff] [blame] | 301 | vArg, err := vdl.ValueFromReflect(reflect.ValueOf(a)) |
| 302 | if err != nil { |
| 303 | return security.DischargeImpetus{}, err |
| 304 | } |
| 305 | impetus.Arguments[i] = vArg |
Ankur | e49a86a | 2014-11-11 18:52:43 -0800 | [diff] [blame] | 306 | } |
| 307 | } |
Todd Wang | b31da59 | 2015-02-20 12:50:39 -0800 | [diff] [blame] | 308 | return impetus, nil |
Ankur | e49a86a | 2014-11-11 18:52:43 -0800 | [diff] [blame] | 309 | } |
| 310 | |
Mike Burrows | 2ec2bb3 | 2015-02-26 15:14:43 -0800 | [diff] [blame] | 311 | // startCall ensures StartCall always returns verror.E. |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 312 | func (c *client) startCall(ctx *context.T, name, method string, args []interface{}, opts []rpc.CallOpt) (rpc.ClientCall, error) { |
Matt Rosencrantz | 4f8ac60 | 2014-12-29 14:42:48 -0800 | [diff] [blame] | 313 | if !ctx.Initialized() { |
Cosmos Nicolaou | b291d6c | 2015-03-27 09:01:46 -0700 | [diff] [blame] | 314 | return nil, verror.ExplicitNew(verror.ErrBadArg, i18n.LangID("en-us"), "<rpc.Client>", "StartCall", "context not initialized") |
Matt Rosencrantz | 321a51d | 2014-10-30 10:37:56 -0700 | [diff] [blame] | 315 | } |
Cosmos Nicolaou | b291d6c | 2015-03-27 09:01:46 -0700 | [diff] [blame] | 316 | ctx, span := vtrace.SetNewSpan(ctx, fmt.Sprintf("<rpc.Client>%q.%s", name, method)) |
| 317 | if err := canCreateServerAuthorizer(ctx, opts); err != nil { |
| 318 | return nil, verror.New(verror.ErrBadArg, ctx, err) |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 319 | } |
Matt Rosencrantz | 321a51d | 2014-10-30 10:37:56 -0700 | [diff] [blame] | 320 | |
Suharsh Sivakumar | 076e953 | 2015-04-09 17:36:25 -0700 | [diff] [blame^] | 321 | deadline := getDeadline(ctx, opts) |
Matt Rosencrantz | cc922c1 | 2014-11-28 20:28:59 -0800 | [diff] [blame] | 322 | |
Cosmos Nicolaou | d1ca686 | 2015-01-30 11:43:39 -0800 | [diff] [blame] | 323 | var lastErr error |
Matt Rosencrantz | 254d570 | 2015-04-01 09:47:38 -0700 | [diff] [blame] | 324 | for retries := uint(0); ; retries++ { |
Suharsh Sivakumar | ae774a5 | 2015-01-09 14:26:32 -0800 | [diff] [blame] | 325 | call, action, err := c.tryCall(ctx, name, method, args, opts) |
David Why Use Two When One Will Do Presotto | 06c60f0 | 2014-09-11 14:52:19 -0700 | [diff] [blame] | 326 | if err == nil { |
| 327 | return call, nil |
| 328 | } |
| 329 | lastErr = err |
Suharsh Sivakumar | 076e953 | 2015-04-09 17:36:25 -0700 | [diff] [blame^] | 330 | if !shouldRetry(action, deadline, opts) { |
Matt Rosencrantz | abacd43 | 2014-11-24 10:44:31 -0800 | [diff] [blame] | 331 | span.Annotatef("Cannot retry after error: %s", err) |
David Why Use Two When One Will Do Presotto | 06c60f0 | 2014-09-11 14:52:19 -0700 | [diff] [blame] | 332 | break |
| 333 | } |
Matt Rosencrantz | 254d570 | 2015-04-01 09:47:38 -0700 | [diff] [blame] | 334 | if !backoff(retries, deadline) { |
| 335 | break |
| 336 | } |
Suharsh Sivakumar | 076e953 | 2015-04-09 17:36:25 -0700 | [diff] [blame^] | 337 | span.Annotatef("Retrying due to error: %s", err) |
David Why Use Two When One Will Do Presotto | 06c60f0 | 2014-09-11 14:52:19 -0700 | [diff] [blame] | 338 | } |
| 339 | return nil, lastErr |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 342 | type serverStatus struct { |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 343 | index int |
| 344 | suffix string |
| 345 | flow stream.Flow |
| 346 | blessings []string // authorized server blessings |
| 347 | rejectedBlessings []security.RejectedBlessing // rejected server blessings |
| 348 | err error |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Asim Shankar | aae3180 | 2015-01-22 11:59:42 -0800 | [diff] [blame] | 351 | // tryCreateFlow attempts to establish a Flow to "server" (which must be a |
| 352 | // rooted name), over which a method invocation request could be sent. |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 353 | // |
| 354 | // The server at the remote end of the flow is authorized using the provided |
| 355 | // authorizer, both during creation of the VC underlying the flow and the |
| 356 | // flow itself. |
Cosmos Nicolaou | 00a0f80 | 2014-11-16 22:44:55 -0800 | [diff] [blame] | 357 | // TODO(cnicolaou): implement real, configurable load balancing. |
Suharsh Sivakumar | 2c5d810 | 2015-03-23 08:49:12 -0700 | [diff] [blame] | 358 | func (c *client) tryCreateFlow(ctx *context.T, principal security.Principal, index int, name, server, method string, auth security.Authorizer, ch chan<- *serverStatus, vcOpts []stream.VCOpt) { |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 359 | status := &serverStatus{index: index} |
Asim Shankar | f4864f4 | 2014-11-25 18:53:05 -0800 | [diff] [blame] | 360 | var span vtrace.Span |
Asim Shankar | aae3180 | 2015-01-22 11:59:42 -0800 | [diff] [blame] | 361 | ctx, span = vtrace.SetNewSpan(ctx, "<client>tryCreateFlow") |
Asim Shankar | f4864f4 | 2014-11-25 18:53:05 -0800 | [diff] [blame] | 362 | span.Annotatef("address:%v", server) |
Asim Shankar | aae3180 | 2015-01-22 11:59:42 -0800 | [diff] [blame] | 363 | defer func() { |
| 364 | ch <- status |
| 365 | span.Finish() |
| 366 | }() |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 367 | |
Asim Shankar | aae3180 | 2015-01-22 11:59:42 -0800 | [diff] [blame] | 368 | address, suffix := naming.SplitAddressName(server) |
| 369 | if len(address) == 0 { |
Todd Wang | ff73e1f | 2015-02-10 21:45:52 -0800 | [diff] [blame] | 370 | status.err = verror.New(errNonRootedName, ctx, server) |
Asim Shankar | aae3180 | 2015-01-22 11:59:42 -0800 | [diff] [blame] | 371 | return |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 372 | } |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 373 | status.suffix = suffix |
| 374 | |
Asim Shankar | aae3180 | 2015-01-22 11:59:42 -0800 | [diff] [blame] | 375 | ep, err := inaming.NewEndpoint(address) |
| 376 | if err != nil { |
Todd Wang | ff73e1f | 2015-02-10 21:45:52 -0800 | [diff] [blame] | 377 | status.err = verror.New(errInvalidEndpoint, ctx, address) |
Asim Shankar | aae3180 | 2015-01-22 11:59:42 -0800 | [diff] [blame] | 378 | return |
| 379 | } |
| 380 | if err = version.CheckCompatibility(ep); err != nil { |
Todd Wang | ff73e1f | 2015-02-10 21:45:52 -0800 | [diff] [blame] | 381 | status.err = verror.New(errIncompatibleEndpoint, ctx, ep) |
Asim Shankar | aae3180 | 2015-01-22 11:59:42 -0800 | [diff] [blame] | 382 | return |
| 383 | } |
Suharsh Sivakumar | 2c5d810 | 2015-03-23 08:49:12 -0700 | [diff] [blame] | 384 | if status.flow, status.err = c.createFlow(ctx, principal, ep, append(vcOpts, &vc.ServerAuthorizer{Suffix: status.suffix, Method: method, Policy: auth})); status.err != nil { |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 385 | vlog.VI(2).Infof("rpc: Failed to create Flow with %v: %v", server, status.err) |
Asim Shankar | aae3180 | 2015-01-22 11:59:42 -0800 | [diff] [blame] | 386 | return |
| 387 | } |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 388 | |
| 389 | // Authorize the remote end of the flow using the provided authorizer. |
| 390 | if status.flow.LocalPrincipal() == nil { |
| 391 | // LocalPrincipal is nil which means we are operating under |
Suharsh Sivakumar | 2c5d810 | 2015-03-23 08:49:12 -0700 | [diff] [blame] | 392 | // SecurityNone. |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 393 | return |
| 394 | } |
| 395 | |
Ankur | d864681 | 2015-03-12 10:48:41 -0700 | [diff] [blame] | 396 | seccall := security.NewCall(&security.CallParams{ |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 397 | LocalPrincipal: status.flow.LocalPrincipal(), |
| 398 | LocalBlessings: status.flow.LocalBlessings(), |
| 399 | RemoteBlessings: status.flow.RemoteBlessings(), |
| 400 | LocalEndpoint: status.flow.LocalEndpoint(), |
| 401 | RemoteEndpoint: status.flow.RemoteEndpoint(), |
| 402 | RemoteDischarges: status.flow.RemoteDischarges(), |
| 403 | Method: method, |
Matt Rosencrantz | 250558f | 2015-03-17 11:37:31 -0700 | [diff] [blame] | 404 | Suffix: status.suffix, |
| 405 | }) |
| 406 | ctx = security.SetCall(ctx, seccall) |
| 407 | if err := auth.Authorize(ctx); err != nil { |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 408 | status.err = verror.New(verror.ErrNotTrusted, ctx, name, status.flow.RemoteBlessings(), err) |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 409 | vlog.VI(2).Infof("rpc: Failed to authorize Flow created with server %v: %s", server, status.err) |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 410 | status.flow.Close() |
| 411 | status.flow = nil |
| 412 | return |
| 413 | } |
Ankur | 9e75e7f | 2015-03-18 18:48:41 -0700 | [diff] [blame] | 414 | status.blessings, status.rejectedBlessings = security.RemoteBlessingNames(ctx) |
Asim Shankar | aae3180 | 2015-01-22 11:59:42 -0800 | [diff] [blame] | 415 | return |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Asim Shankar | aae3180 | 2015-01-22 11:59:42 -0800 | [diff] [blame] | 418 | // tryCall makes a single attempt at a call. It may connect to multiple servers |
| 419 | // (all that serve "name"), but will invoke the method on at most one of them |
| 420 | // (the server running on the most preferred protcol and network amongst all |
| 421 | // the servers that were successfully connected to and authorized). |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 422 | func (c *client) tryCall(ctx *context.T, name, method string, args []interface{}, opts []rpc.CallOpt) (rpc.ClientCall, verror.ActionCode, error) { |
Asim Shankar | aae3180 | 2015-01-22 11:59:42 -0800 | [diff] [blame] | 423 | var resolved *naming.MountEntry |
Asim Shankar | aae3180 | 2015-01-22 11:59:42 -0800 | [diff] [blame] | 424 | var err error |
Asim Shankar | 263c73b | 2015-03-19 18:31:26 -0700 | [diff] [blame] | 425 | var blessingPattern security.BlessingPattern |
| 426 | blessingPattern, name = security.SplitPatternName(name) |
David Why Use Two When One Will Do Presotto | 38788d4 | 2015-03-31 17:13:54 -0700 | [diff] [blame] | 427 | if resolved, err = c.ns.Resolve(ctx, name, getNamespaceOpts(opts)...); err != nil { |
David Why Use Two When One Will Do Presotto | 8de8585 | 2015-01-21 11:05:09 -0800 | [diff] [blame] | 428 | vlog.Errorf("Resolve: %v", err) |
Cosmos Nicolaou | d1ca686 | 2015-01-30 11:43:39 -0800 | [diff] [blame] | 429 | // We always return NoServers as the error so that the caller knows |
| 430 | // that's ok to retry the operation since the name may be registered |
| 431 | // in the near future. |
Cosmos Nicolaou | b291d6c | 2015-03-27 09:01:46 -0700 | [diff] [blame] | 432 | switch { |
| 433 | case verror.ErrorID(err) == naming.ErrNoSuchName.ID: |
Jiri Simsa | 074bf36 | 2015-02-17 09:29:45 -0800 | [diff] [blame] | 434 | return nil, verror.RetryRefetch, verror.New(verror.ErrNoServers, ctx, name) |
Cosmos Nicolaou | b291d6c | 2015-03-27 09:01:46 -0700 | [diff] [blame] | 435 | case verror.ErrorID(err) == verror.ErrNoServers.ID: |
| 436 | // Avoid wrapping errors unnecessarily. |
| 437 | return nil, verror.NoRetry, err |
| 438 | default: |
| 439 | return nil, verror.NoRetry, verror.New(verror.ErrNoServers, ctx, name, err) |
Ryan Brown | 6153c6c | 2014-12-11 13:10:09 -0800 | [diff] [blame] | 440 | } |
David Why Use Two When One Will Do Presotto | 3da1c79 | 2014-10-03 11:15:53 -0700 | [diff] [blame] | 441 | } else { |
Ryan Brown | 6153c6c | 2014-12-11 13:10:09 -0800 | [diff] [blame] | 442 | if len(resolved.Servers) == 0 { |
Cosmos Nicolaou | b291d6c | 2015-03-27 09:01:46 -0700 | [diff] [blame] | 443 | // This should never happen. |
| 444 | return nil, verror.NoRetry, verror.New(verror.ErrInternal, ctx, name) |
Suharsh Sivakumar | 65e44c2 | 2014-12-10 17:15:19 -0800 | [diff] [blame] | 445 | } |
Ryan Brown | 6153c6c | 2014-12-11 13:10:09 -0800 | [diff] [blame] | 446 | // An empty set of protocols means all protocols... |
Jungho Ahn | 25545d3 | 2015-01-26 15:14:14 -0800 | [diff] [blame] | 447 | if resolved.Servers, err = filterAndOrderServers(resolved.Servers, c.preferredProtocols, c.ipNets); err != nil { |
Jiri Simsa | 074bf36 | 2015-02-17 09:29:45 -0800 | [diff] [blame] | 448 | return nil, verror.RetryRefetch, verror.New(verror.ErrNoServers, ctx, name, err) |
David Why Use Two When One Will Do Presotto | 3da1c79 | 2014-10-03 11:15:53 -0700 | [diff] [blame] | 449 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 450 | } |
Cosmos Nicolaou | 112bf1c | 2014-11-21 15:43:11 -0800 | [diff] [blame] | 451 | |
Suharsh Sivakumar | 2c5d810 | 2015-03-23 08:49:12 -0700 | [diff] [blame] | 452 | // We need to ensure calls to v23 factory methods do not occur during runtime |
| 453 | // initialization. Currently, the agent, which uses SecurityNone, is the only caller |
| 454 | // during runtime initialization. We would like to set the principal in the context |
| 455 | // to nil if we are running in SecurityNone, but this always results in a panic since |
| 456 | // the agent client would trigger the call v23.SetPrincipal during runtime |
| 457 | // initialization. So, we gate the call to v23.GetPrincipal instead since the agent |
| 458 | // client will have callEncrypted == false. |
| 459 | // Potential solutions to this are: |
| 460 | // (1) Create a separate client for the agent so that this code doesn't have to |
| 461 | // account for its use during runtime initialization. |
| 462 | // (2) Have a ctx.IsRuntimeInitialized() method that we can additionally predicate |
| 463 | // on here. |
| 464 | var principal security.Principal |
| 465 | if callEncrypted(opts) { |
Suharsh Sivakumar | 0ed10c2 | 2015-04-06 12:55:55 -0700 | [diff] [blame] | 466 | if principal = v23.GetPrincipal(ctx); principal == nil { |
| 467 | return nil, verror.NoRetry, verror.New(errNoPrincipal, ctx) |
| 468 | } |
Suharsh Sivakumar | 2c5d810 | 2015-03-23 08:49:12 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Asim Shankar | b547ea9 | 2015-02-17 18:49:45 -0800 | [diff] [blame] | 471 | // servers is now ordered by the priority heurestic implemented in |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 472 | // filterAndOrderServers. |
Asim Shankar | aae3180 | 2015-01-22 11:59:42 -0800 | [diff] [blame] | 473 | // |
| 474 | // Try to connect to all servers in parallel. Provide sufficient |
| 475 | // buffering for all of the connections to finish instantaneously. This |
| 476 | // is important because we want to process the responses in priority |
| 477 | // order; that order is indicated by the order of entries in servers. |
| 478 | // So, if two respones come in at the same 'instant', we prefer the |
| 479 | // first in the resolved.Servers) |
| 480 | attempts := len(resolved.Servers) |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 481 | |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 482 | responses := make([]*serverStatus, attempts) |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 483 | ch := make(chan *serverStatus, attempts) |
Asim Shankar | aae3180 | 2015-01-22 11:59:42 -0800 | [diff] [blame] | 484 | vcOpts := append(getVCOpts(opts), c.vcOpts...) |
Asim Shankar | 263c73b | 2015-03-19 18:31:26 -0700 | [diff] [blame] | 485 | authorizer := newServerAuthorizer(blessingPattern, opts...) |
Asim Shankar | aae3180 | 2015-01-22 11:59:42 -0800 | [diff] [blame] | 486 | for i, server := range resolved.Names() { |
Asim Shankar | 263c73b | 2015-03-19 18:31:26 -0700 | [diff] [blame] | 487 | // Create a copy of vcOpts for each call to tryCreateFlow |
| 488 | // to avoid concurrent tryCreateFlows from stepping on each |
| 489 | // other while manipulating their copy of the options. |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 490 | vcOptsCopy := make([]stream.VCOpt, len(vcOpts)) |
| 491 | copy(vcOptsCopy, vcOpts) |
Suharsh Sivakumar | 2c5d810 | 2015-03-23 08:49:12 -0700 | [diff] [blame] | 492 | go c.tryCreateFlow(ctx, principal, i, name, server, method, authorizer, ch, vcOptsCopy) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 493 | } |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 494 | |
Todd Wang | f6a0688 | 2015-02-27 17:38:01 -0800 | [diff] [blame] | 495 | var timeoutChan <-chan time.Time |
| 496 | if deadline, ok := ctx.Deadline(); ok { |
| 497 | timeoutChan = time.After(deadline.Sub(time.Now())) |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 498 | } |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 499 | |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 500 | for { |
Todd Wang | ef05c06 | 2014-11-15 09:51:43 -0800 | [diff] [blame] | 501 | // Block for at least one new response from the server, or the timeout. |
| 502 | select { |
| 503 | case r := <-ch: |
| 504 | responses[r.index] = r |
| 505 | // Read as many more responses as we can without blocking. |
| 506 | LoopNonBlocking: |
| 507 | for { |
| 508 | select { |
| 509 | default: |
| 510 | break LoopNonBlocking |
| 511 | case r := <-ch: |
| 512 | responses[r.index] = r |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 513 | } |
Todd Wang | ef05c06 | 2014-11-15 09:51:43 -0800 | [diff] [blame] | 514 | } |
| 515 | case <-timeoutChan: |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 516 | vlog.VI(2).Infof("rpc: timeout on connection to server %v ", name) |
Asim Shankar | aae3180 | 2015-01-22 11:59:42 -0800 | [diff] [blame] | 517 | _, _, err := c.failedTryCall(ctx, name, method, responses, ch) |
Todd Wang | 8fa3876 | 2015-03-25 14:04:59 -0700 | [diff] [blame] | 518 | if verror.ErrorID(err) != verror.ErrTimeout.ID { |
Jiri Simsa | 074bf36 | 2015-02-17 09:29:45 -0800 | [diff] [blame] | 519 | return nil, verror.NoRetry, verror.New(verror.ErrTimeout, ctx, err) |
Cosmos Nicolaou | 38209d4 | 2014-12-09 16:50:38 -0800 | [diff] [blame] | 520 | } |
| 521 | return nil, verror.NoRetry, err |
Todd Wang | ef05c06 | 2014-11-15 09:51:43 -0800 | [diff] [blame] | 522 | } |
| 523 | |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 524 | dc := c.dc |
| 525 | if shouldNotFetchDischarges(opts) { |
| 526 | dc = nil |
| 527 | } |
Todd Wang | ef05c06 | 2014-11-15 09:51:43 -0800 | [diff] [blame] | 528 | // Process new responses, in priority order. |
| 529 | numResponses := 0 |
| 530 | for _, r := range responses { |
| 531 | if r != nil { |
| 532 | numResponses++ |
| 533 | } |
| 534 | if r == nil || r.flow == nil { |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 535 | continue |
| 536 | } |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 537 | |
Matt Rosencrantz | 1094d06 | 2015-01-30 06:43:12 -0800 | [diff] [blame] | 538 | doneChan := ctx.Done() |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 539 | r.flow.SetDeadline(doneChan) |
Cosmos Nicolaou | b291d6c | 2015-03-27 09:01:46 -0700 | [diff] [blame] | 540 | // TODO(cnicolaou): continue verror testing from here. |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 541 | fc, err := newFlowClient(ctx, r.flow, r.blessings, dc) |
| 542 | if err != nil { |
Cosmos Nicolaou | b291d6c | 2015-03-27 09:01:46 -0700 | [diff] [blame] | 543 | return nil, verror.NoRetry, err |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 544 | } |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 545 | |
Ankur | dda1649 | 2015-04-07 12:35:42 -0700 | [diff] [blame] | 546 | if err := fc.prepareBlessingsAndDischarges(ctx, method, r.suffix, args, r.rejectedBlessings, opts); err != nil { |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 547 | r.err = verror.New(verror.ErrNotTrusted, ctx, name, r.flow.RemoteBlessings(), err) |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 548 | vlog.VI(2).Infof("rpc: err: %s", r.err) |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 549 | r.flow.Close() |
| 550 | r.flow = nil |
| 551 | continue |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 552 | } |
| 553 | |
Todd Wang | ef05c06 | 2014-11-15 09:51:43 -0800 | [diff] [blame] | 554 | // This is the 'point of no return'; once the RPC is started (fc.start |
| 555 | // below) we can't be sure if it makes it to the server or not so, this |
| 556 | // code will never call fc.start more than once to ensure that we provide |
| 557 | // 'at-most-once' rpc semantics at this level. Retrying the network |
| 558 | // connections (i.e. creating flows) is fine since we can cleanup that |
| 559 | // state if we abort a call (i.e. close the flow). |
| 560 | // |
| 561 | // We must ensure that all flows other than r.flow are closed. |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 562 | // |
| 563 | // TODO(cnicolaou): all errors below are marked as NoRetry |
| 564 | // because we want to provide at-most-once rpc semantics so |
| 565 | // we only ever attempt an RPC once. In the future, we'll cache |
Cosmos Nicolaou | b291d6c | 2015-03-27 09:01:46 -0700 | [diff] [blame] | 566 | // responses on the server and then we can retry in-flight |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 567 | // RPCs. |
Todd Wang | ef05c06 | 2014-11-15 09:51:43 -0800 | [diff] [blame] | 568 | go cleanupTryCall(r, responses, ch) |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 569 | |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 570 | if doneChan != nil { |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 571 | go func() { |
| 572 | select { |
Matt Rosencrantz | fa3082c | 2015-01-22 21:39:04 -0800 | [diff] [blame] | 573 | case <-doneChan: |
Matt Rosencrantz | 5f98d94 | 2015-01-08 13:48:30 -0800 | [diff] [blame] | 574 | vtrace.GetSpan(fc.ctx).Annotate("Cancelled") |
Matt Rosencrantz | 9346b41 | 2014-12-18 15:59:19 -0800 | [diff] [blame] | 575 | fc.flow.Cancel() |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 576 | case <-fc.flow.Closed(): |
| 577 | } |
| 578 | }() |
| 579 | } |
| 580 | |
Todd Wang | f6a0688 | 2015-02-27 17:38:01 -0800 | [diff] [blame] | 581 | deadline, _ := ctx.Deadline() |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 582 | if verr := fc.start(r.suffix, method, args, deadline); verr != nil { |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 583 | return nil, verror.NoRetry, verr |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 584 | } |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 585 | return fc, verror.NoRetry, nil |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 586 | } |
Todd Wang | ef05c06 | 2014-11-15 09:51:43 -0800 | [diff] [blame] | 587 | if numResponses == len(responses) { |
Asim Shankar | aae3180 | 2015-01-22 11:59:42 -0800 | [diff] [blame] | 588 | return c.failedTryCall(ctx, name, method, responses, ch) |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 589 | } |
| 590 | } |
Todd Wang | ef05c06 | 2014-11-15 09:51:43 -0800 | [diff] [blame] | 591 | } |
| 592 | |
Asim Shankar | aae3180 | 2015-01-22 11:59:42 -0800 | [diff] [blame] | 593 | // cleanupTryCall ensures we've waited for every response from the tryCreateFlow |
Todd Wang | ef05c06 | 2014-11-15 09:51:43 -0800 | [diff] [blame] | 594 | // goroutines, and have closed the flow from each one except skip. This is a |
| 595 | // blocking function; it should be called in its own goroutine. |
| 596 | func cleanupTryCall(skip *serverStatus, responses []*serverStatus, ch chan *serverStatus) { |
| 597 | numPending := 0 |
| 598 | for _, r := range responses { |
| 599 | switch { |
| 600 | case r == nil: |
| 601 | // The response hasn't arrived yet. |
| 602 | numPending++ |
| 603 | case r == skip || r.flow == nil: |
| 604 | // Either we should skip this flow, or we've closed the flow for this |
| 605 | // response already; nothing more to do. |
| 606 | default: |
| 607 | // We received the response, but haven't closed the flow yet. |
| 608 | r.flow.Close() |
| 609 | } |
| 610 | } |
| 611 | // Now we just need to wait for the pending responses and close their flows. |
| 612 | for i := 0; i < numPending; i++ { |
| 613 | if r := <-ch; r.flow != nil { |
| 614 | r.flow.Close() |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | // failedTryCall performs asynchronous cleanup for tryCall, and returns an |
| 620 | // appropriate error from the responses we've already received. All parallel |
| 621 | // calls in tryCall failed or we timed out if we get here. |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 622 | func (c *client) failedTryCall(ctx *context.T, name, method string, responses []*serverStatus, ch chan *serverStatus) (rpc.ClientCall, verror.ActionCode, error) { |
Todd Wang | ef05c06 | 2014-11-15 09:51:43 -0800 | [diff] [blame] | 623 | go cleanupTryCall(nil, responses, ch) |
Cosmos Nicolaou | 4e8da64 | 2014-11-13 08:32:05 -0800 | [diff] [blame] | 624 | c.ns.FlushCacheEntry(name) |
Cosmos Nicolaou | 112bf1c | 2014-11-21 15:43:11 -0800 | [diff] [blame] | 625 | noconn, untrusted := []string{}, []string{} |
Asim Shankar | aae3180 | 2015-01-22 11:59:42 -0800 | [diff] [blame] | 626 | for _, r := range responses { |
Cosmos Nicolaou | 112bf1c | 2014-11-21 15:43:11 -0800 | [diff] [blame] | 627 | if r != nil && r.err != nil { |
Cosmos Nicolaou | 112bf1c | 2014-11-21 15:43:11 -0800 | [diff] [blame] | 628 | switch { |
Todd Wang | 8fa3876 | 2015-03-25 14:04:59 -0700 | [diff] [blame] | 629 | case verror.ErrorID(r.err) == verror.ErrNotTrusted.ID || verror.ErrorID(r.err) == errAuthError.ID: |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 630 | untrusted = append(untrusted, "("+r.err.Error()+") ") |
Cosmos Nicolaou | 112bf1c | 2014-11-21 15:43:11 -0800 | [diff] [blame] | 631 | default: |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 632 | noconn = append(noconn, "("+r.err.Error()+") ") |
Cosmos Nicolaou | 112bf1c | 2014-11-21 15:43:11 -0800 | [diff] [blame] | 633 | } |
Todd Wang | ef05c06 | 2014-11-15 09:51:43 -0800 | [diff] [blame] | 634 | } |
| 635 | } |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 636 | // TODO(cnicolaou): we get system errors for things like dialing using |
| 637 | // the 'ws' protocol which can never succeed even if we retry the connection, |
Cosmos Nicolaou | b291d6c | 2015-03-27 09:01:46 -0700 | [diff] [blame] | 638 | // hence we return RetryRefetch below except for the case where the servers |
| 639 | // are not trusted, in case there's no point in retrying at all. |
| 640 | // TODO(cnicolaou): implementing at-most-once rpc semantics in the future |
| 641 | // will require thinking through all of the cases where the RPC can |
| 642 | // be retried by the client whilst it's actually being executed on the |
| 643 | // client. |
Cosmos Nicolaou | 112bf1c | 2014-11-21 15:43:11 -0800 | [diff] [blame] | 644 | switch { |
| 645 | case len(untrusted) > 0 && len(noconn) > 0: |
Jiri Simsa | 074bf36 | 2015-02-17 09:29:45 -0800 | [diff] [blame] | 646 | return nil, verror.RetryRefetch, verror.New(verror.ErrNoServersAndAuth, ctx, append(noconn, untrusted...)) |
Cosmos Nicolaou | 112bf1c | 2014-11-21 15:43:11 -0800 | [diff] [blame] | 647 | case len(noconn) > 0: |
Jiri Simsa | 074bf36 | 2015-02-17 09:29:45 -0800 | [diff] [blame] | 648 | return nil, verror.RetryRefetch, verror.New(verror.ErrNoServers, ctx, noconn) |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 649 | case len(untrusted) > 0: |
Jiri Simsa | 074bf36 | 2015-02-17 09:29:45 -0800 | [diff] [blame] | 650 | return nil, verror.NoRetry, verror.New(verror.ErrNotTrusted, ctx, untrusted) |
Cosmos Nicolaou | 112bf1c | 2014-11-21 15:43:11 -0800 | [diff] [blame] | 651 | default: |
Jiri Simsa | 074bf36 | 2015-02-17 09:29:45 -0800 | [diff] [blame] | 652 | return nil, verror.RetryRefetch, verror.New(verror.ErrTimeout, ctx) |
Cosmos Nicolaou | 112bf1c | 2014-11-21 15:43:11 -0800 | [diff] [blame] | 653 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 656 | // prepareBlessingsAndDischarges prepares blessings and discharges for |
| 657 | // the call. |
| 658 | // |
| 659 | // This includes: (1) preparing blessings that must be granted to the |
| 660 | // server, (2) preparing blessings that the client authenticates with, |
| 661 | // and, (3) preparing any discharges for third-party caveats on the client's |
| 662 | // blessings. |
Ankur | dda1649 | 2015-04-07 12:35:42 -0700 | [diff] [blame] | 663 | func (fc *flowClient) prepareBlessingsAndDischarges(ctx *context.T, method, suffix string, args []interface{}, rejectedServerBlessings []security.RejectedBlessing, opts []rpc.CallOpt) error { |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 664 | // LocalPrincipal is nil which means we are operating under |
Suharsh Sivakumar | 2c5d810 | 2015-03-23 08:49:12 -0700 | [diff] [blame] | 665 | // SecurityNone. |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 666 | if fc.flow.LocalPrincipal() == nil { |
| 667 | return nil |
Asim Shankar | b54d764 | 2014-06-05 13:08:04 -0700 | [diff] [blame] | 668 | } |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 669 | |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 670 | // Fetch blessings from the client's blessing store that are to be |
| 671 | // shared with the server. |
| 672 | if fc.blessings = fc.flow.LocalPrincipal().BlessingStore().ForPeer(fc.server...); fc.blessings.IsZero() { |
| 673 | // TODO(ataly, ashankar): We need not error out here and instead can just send the <nil> blessings |
| 674 | // to the server. |
| 675 | return verror.New(errNoBlessingsForPeer, fc.ctx, fc.server, rejectedServerBlessings) |
| 676 | } |
| 677 | |
| 678 | // Fetch any discharges for third-party caveats on the client's blessings. |
| 679 | if !fc.blessings.IsZero() && fc.dc != nil { |
| 680 | impetus, err := mkDischargeImpetus(fc.server, method, args) |
| 681 | if err != nil { |
| 682 | // TODO(toddw): Fix up the internal error. |
| 683 | return verror.New(verror.ErrBadProtocol, fc.ctx, fmt.Errorf("couldn't make discharge impetus: %v", err)) |
| 684 | } |
| 685 | fc.discharges = fc.dc.PrepareDischarges(fc.ctx, fc.blessings.ThirdPartyCaveats(), impetus) |
| 686 | } |
Ankur | dda1649 | 2015-04-07 12:35:42 -0700 | [diff] [blame] | 687 | |
| 688 | // Prepare blessings that must be granted to the server (using any |
| 689 | // rpc.Granter implementation in 'opts'). |
| 690 | // |
| 691 | // NOTE(ataly, suharshs): Before invoking the granter, we set the parameters of |
| 692 | // the current call on the context. The context would now have two principals |
| 693 | // set on it -- one available via v23.GetPrincipal(ctx) and the other available |
| 694 | // via security.GetCall(ctx).LocalPrincipal(). While in theory the two principals |
| 695 | // can be different, the flow.LocalPrincipal == nil check at the beginning |
| 696 | // of this method ensures that the two are the same and non-nil at this point |
| 697 | // in the code. |
| 698 | ldischargeMap := make(map[string]security.Discharge) |
| 699 | for _, d := range fc.discharges { |
| 700 | ldischargeMap[d.ID()] = d |
| 701 | } |
| 702 | seccall := security.NewCall(&security.CallParams{ |
| 703 | LocalPrincipal: fc.flow.LocalPrincipal(), |
| 704 | LocalBlessings: fc.blessings, |
| 705 | RemoteBlessings: fc.flow.RemoteBlessings(), |
| 706 | LocalEndpoint: fc.flow.LocalEndpoint(), |
| 707 | RemoteEndpoint: fc.flow.RemoteEndpoint(), |
| 708 | LocalDischarges: ldischargeMap, |
| 709 | RemoteDischarges: fc.flow.RemoteDischarges(), |
| 710 | Method: method, |
| 711 | Suffix: suffix, |
| 712 | }) |
| 713 | ctx = security.SetCall(ctx, seccall) |
| 714 | if err := fc.prepareGrantedBlessings(ctx, opts); err != nil { |
| 715 | return err |
| 716 | } |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 717 | return nil |
| 718 | } |
| 719 | |
Ankur | dda1649 | 2015-04-07 12:35:42 -0700 | [diff] [blame] | 720 | func (fc *flowClient) prepareGrantedBlessings(ctx *context.T, opts []rpc.CallOpt) error { |
Asim Shankar | b54d764 | 2014-06-05 13:08:04 -0700 | [diff] [blame] | 721 | for _, o := range opts { |
| 722 | switch v := o.(type) { |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 723 | case rpc.Granter: |
Ankur | dda1649 | 2015-04-07 12:35:42 -0700 | [diff] [blame] | 724 | if b, err := v.Grant(ctx); err != nil { |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 725 | return verror.New(errBlessingGrant, fc.ctx, fc.server, err) |
| 726 | } else if fc.grantedBlessings, err = security.UnionOfBlessings(fc.grantedBlessings, b); err != nil { |
| 727 | return verror.New(errBlessingAdd, fc.ctx, fc.server, err) |
Asim Shankar | 8f05c22 | 2014-10-06 22:08:19 -0700 | [diff] [blame] | 728 | } |
Asim Shankar | b54d764 | 2014-06-05 13:08:04 -0700 | [diff] [blame] | 729 | } |
| 730 | } |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 731 | return nil |
Asim Shankar | b54d764 | 2014-06-05 13:08:04 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 734 | func (c *client) Close() { |
Mehrdad Afshari | cd9852b | 2014-09-26 11:07:35 -0700 | [diff] [blame] | 735 | defer vlog.LogCall()() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 736 | c.vcMapMu.Lock() |
| 737 | for _, v := range c.vcMap { |
| 738 | c.streamMgr.ShutdownEndpoint(v.remoteEP) |
| 739 | } |
| 740 | c.vcMap = nil |
| 741 | c.vcMapMu.Unlock() |
| 742 | } |
| 743 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 744 | // flowClient implements the RPC client-side protocol for a single RPC, over a |
| 745 | // flow that's already connected to the server. |
| 746 | type flowClient struct { |
Todd Wang | 3425a90 | 2015-01-21 18:43:59 -0800 | [diff] [blame] | 747 | ctx *context.T // context to annotate with call details |
| 748 | dec *vom.Decoder // to decode responses and results from the server |
| 749 | enc *vom.Encoder // to encode requests and args to the server |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 750 | server []string // Blessings bound to the server that authorize it to receive the RPC request from the client. |
Todd Wang | 3425a90 | 2015-01-21 18:43:59 -0800 | [diff] [blame] | 751 | flow stream.Flow // the underlying flow |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 752 | response rpc.Response // each decoded response message is kept here |
Asim Shankar | 1707e43 | 2014-05-29 19:42:41 -0700 | [diff] [blame] | 753 | |
Ankur | e49a86a | 2014-11-11 18:52:43 -0800 | [diff] [blame] | 754 | discharges []security.Discharge // discharges used for this request |
| 755 | dc vc.DischargeClient // client-global discharge-client |
Andres Erbsen | b7f95f3 | 2014-07-07 12:07:56 -0700 | [diff] [blame] | 756 | |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 757 | blessings security.Blessings // the local blessings for the current RPC. |
| 758 | grantedBlessings security.Blessings // the blessings granted to the server. |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 759 | |
Asim Shankar | 1707e43 | 2014-05-29 19:42:41 -0700 | [diff] [blame] | 760 | sendClosedMu sync.Mutex |
| 761 | sendClosed bool // is the send side already closed? GUARDED_BY(sendClosedMu) |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 762 | finished bool // has Finish() already been called? |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 765 | var _ rpc.ClientCall = (*flowClient)(nil) |
| 766 | var _ rpc.Stream = (*flowClient)(nil) |
Benjamin Prosnitz | fdfbf7b | 2014-10-08 09:47:21 -0700 | [diff] [blame] | 767 | |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 768 | func newFlowClient(ctx *context.T, flow stream.Flow, server []string, dc vc.DischargeClient) (*flowClient, error) { |
Todd Wang | 34ed4c6 | 2014-11-26 15:15:52 -0800 | [diff] [blame] | 769 | fc := &flowClient{ |
Ankur | e49a86a | 2014-11-11 18:52:43 -0800 | [diff] [blame] | 770 | ctx: ctx, |
Ankur | e49a86a | 2014-11-11 18:52:43 -0800 | [diff] [blame] | 771 | flow: flow, |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 772 | server: server, |
Ankur | e49a86a | 2014-11-11 18:52:43 -0800 | [diff] [blame] | 773 | dc: dc, |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 774 | } |
Todd Wang | f519f8f | 2015-01-21 10:07:41 -0800 | [diff] [blame] | 775 | var err error |
Jungho Ahn | 60408fa | 2015-03-27 15:28:22 -0700 | [diff] [blame] | 776 | typeenc := flow.VCDataCache().Get(vc.TypeEncoderKey{}) |
| 777 | if typeenc == nil { |
| 778 | if fc.enc, err = vom.NewEncoder(flow); err != nil { |
| 779 | berr := verror.New(verror.ErrBadProtocol, fc.ctx, verror.New(errVomEncoder, fc.ctx, err)) |
| 780 | return nil, fc.close(berr) |
| 781 | } |
| 782 | if fc.dec, err = vom.NewDecoder(flow); err != nil { |
| 783 | berr := verror.New(verror.ErrBadProtocol, fc.ctx, verror.New(errVomDecoder, fc.ctx, err)) |
| 784 | return nil, fc.close(berr) |
| 785 | } |
| 786 | } else { |
| 787 | if fc.enc, err = vom.NewEncoderWithTypeEncoder(flow, typeenc.(*vom.TypeEncoder)); err != nil { |
| 788 | berr := verror.New(verror.ErrBadProtocol, fc.ctx, verror.New(errVomEncoder, fc.ctx, err)) |
| 789 | return nil, fc.close(berr) |
| 790 | } |
| 791 | typedec := flow.VCDataCache().Get(vc.TypeDecoderKey{}) |
| 792 | if fc.dec, err = vom.NewDecoderWithTypeDecoder(flow, typedec.(*vom.TypeDecoder)); err != nil { |
| 793 | berr := verror.New(verror.ErrBadProtocol, fc.ctx, verror.New(errVomDecoder, fc.ctx, err)) |
| 794 | return nil, fc.close(berr) |
| 795 | } |
Todd Wang | 34ed4c6 | 2014-11-26 15:15:52 -0800 | [diff] [blame] | 796 | } |
| 797 | return fc, nil |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 798 | } |
| 799 | |
Cosmos Nicolaou | d1ca686 | 2015-01-30 11:43:39 -0800 | [diff] [blame] | 800 | func (fc *flowClient) close(err error) error { |
Cosmos Nicolaou | b291d6c | 2015-03-27 09:01:46 -0700 | [diff] [blame] | 801 | if _, ok := err.(verror.E); err != nil && !ok { |
| 802 | // TODO(cnicolaou): remove this once the second CL in this |
| 803 | // series of CLs to use verror consistently is complete. |
| 804 | vlog.Infof("WARNING: expected %v to be a verror", err) |
| 805 | } |
| 806 | subErr := verror.SubErr{Err: err, Options: verror.Print} |
| 807 | subErr.Name = "remote=" + fc.flow.RemoteEndpoint().String() |
Cosmos Nicolaou | d1ca686 | 2015-01-30 11:43:39 -0800 | [diff] [blame] | 808 | if cerr := fc.flow.Close(); cerr != nil && err == nil { |
Cosmos Nicolaou | b291d6c | 2015-03-27 09:01:46 -0700 | [diff] [blame] | 809 | return verror.New(verror.ErrInternal, fc.ctx, subErr) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 810 | } |
Cosmos Nicolaou | d1ca686 | 2015-01-30 11:43:39 -0800 | [diff] [blame] | 811 | switch { |
Todd Wang | 8fa3876 | 2015-03-25 14:04:59 -0700 | [diff] [blame] | 812 | case verror.ErrorID(err) == verror.ErrBadProtocol.ID: |
Cosmos Nicolaou | d1ca686 | 2015-01-30 11:43:39 -0800 | [diff] [blame] | 813 | switch fc.ctx.Err() { |
| 814 | case context.DeadlineExceeded: |
Cosmos Nicolaou | b291d6c | 2015-03-27 09:01:46 -0700 | [diff] [blame] | 815 | timeout := verror.New(verror.ErrTimeout, fc.ctx) |
| 816 | err := verror.AddSubErrs(timeout, fc.ctx, subErr) |
| 817 | return err |
Cosmos Nicolaou | d1ca686 | 2015-01-30 11:43:39 -0800 | [diff] [blame] | 818 | case context.Canceled: |
Cosmos Nicolaou | b291d6c | 2015-03-27 09:01:46 -0700 | [diff] [blame] | 819 | canceled := verror.New(verror.ErrCanceled, fc.ctx) |
| 820 | err := verror.AddSubErrs(canceled, fc.ctx, subErr) |
| 821 | return err |
Cosmos Nicolaou | d1ca686 | 2015-01-30 11:43:39 -0800 | [diff] [blame] | 822 | } |
Todd Wang | 8fa3876 | 2015-03-25 14:04:59 -0700 | [diff] [blame] | 823 | case verror.ErrorID(err) == verror.ErrTimeout.ID: |
Cosmos Nicolaou | d1ca686 | 2015-01-30 11:43:39 -0800 | [diff] [blame] | 824 | // Canceled trumps timeout. |
| 825 | if fc.ctx.Err() == context.Canceled { |
| 826 | // TODO(cnicolaou,m3b): reintroduce 'append' when the new verror API is done. |
Jiri Simsa | 074bf36 | 2015-02-17 09:29:45 -0800 | [diff] [blame] | 827 | return verror.New(verror.ErrCanceled, fc.ctx, err.Error()) |
Cosmos Nicolaou | d1ca686 | 2015-01-30 11:43:39 -0800 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | return err |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 831 | } |
| 832 | |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 833 | func (fc *flowClient) start(suffix, method string, args []interface{}, deadline time.Time) error { |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 834 | // Encode the Blessings information for the client to authorize the flow. |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 835 | var blessingsRequest rpc.BlessingsRequest |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 836 | if fc.flow.LocalPrincipal() != nil { |
Jungho Ahn | 44d8daf | 2015-01-16 10:39:15 -0800 | [diff] [blame] | 837 | blessingsRequest = clientEncodeBlessings(fc.flow.VCDataCache(), fc.blessings) |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 838 | } |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 839 | req := rpc.Request{ |
Asim Shankar | 8f05c22 | 2014-10-06 22:08:19 -0700 | [diff] [blame] | 840 | Suffix: suffix, |
| 841 | Method: method, |
| 842 | NumPosArgs: uint64(len(args)), |
Todd Wang | f6a0688 | 2015-02-27 17:38:01 -0800 | [diff] [blame] | 843 | Deadline: vtime.Deadline{deadline}, |
Asim Shankar | b07ec69 | 2015-02-27 23:40:44 -0800 | [diff] [blame] | 844 | GrantedBlessings: fc.grantedBlessings, |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 845 | Blessings: blessingsRequest, |
Asim Shankar | 0864282 | 2015-03-02 21:21:09 -0800 | [diff] [blame] | 846 | Discharges: fc.discharges, |
Matt Rosencrantz | 2803fe9 | 2015-03-09 15:26:32 -0700 | [diff] [blame] | 847 | TraceRequest: vtrace.GetRequest(fc.ctx), |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 848 | } |
| 849 | if err := fc.enc.Encode(req); err != nil { |
Jiri Simsa | 074bf36 | 2015-02-17 09:29:45 -0800 | [diff] [blame] | 850 | berr := verror.New(verror.ErrBadProtocol, fc.ctx, verror.New(errRequestEncoding, fc.ctx, fmt.Sprintf("%#v", req), err)) |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 851 | return fc.close(berr) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 852 | } |
| 853 | for ix, arg := range args { |
| 854 | if err := fc.enc.Encode(arg); err != nil { |
Jiri Simsa | 074bf36 | 2015-02-17 09:29:45 -0800 | [diff] [blame] | 855 | berr := verror.New(verror.ErrBadProtocol, fc.ctx, verror.New(errArgEncoding, fc.ctx, ix, err)) |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 856 | return fc.close(berr) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 857 | } |
| 858 | } |
| 859 | return nil |
| 860 | } |
| 861 | |
| 862 | func (fc *flowClient) Send(item interface{}) error { |
Mehrdad Afshari | cd9852b | 2014-09-26 11:07:35 -0700 | [diff] [blame] | 863 | defer vlog.LogCall()() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 864 | if fc.sendClosed { |
Jiri Simsa | 074bf36 | 2015-02-17 09:29:45 -0800 | [diff] [blame] | 865 | return verror.New(verror.ErrAborted, fc.ctx) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | // The empty request header indicates what follows is a streaming arg. |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 869 | if err := fc.enc.Encode(rpc.Request{}); err != nil { |
| 870 | berr := verror.New(verror.ErrBadProtocol, fc.ctx, verror.New(errRequestEncoding, fc.ctx, rpc.Request{}, err)) |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 871 | return fc.close(berr) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 872 | } |
| 873 | if err := fc.enc.Encode(item); err != nil { |
Jiri Simsa | 074bf36 | 2015-02-17 09:29:45 -0800 | [diff] [blame] | 874 | berr := verror.New(verror.ErrBadProtocol, fc.ctx, verror.New(errArgEncoding, fc.ctx, -1, err)) |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 875 | return fc.close(berr) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 876 | } |
| 877 | return nil |
| 878 | } |
| 879 | |
Matt Rosencrantz | 4f8ac60 | 2014-12-29 14:42:48 -0800 | [diff] [blame] | 880 | func decodeNetError(ctx *context.T, err error) verror.IDAction { |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 881 | if neterr, ok := err.(net.Error); ok { |
| 882 | if neterr.Timeout() || neterr.Temporary() { |
Cosmos Nicolaou | d1ca686 | 2015-01-30 11:43:39 -0800 | [diff] [blame] | 883 | // If a read is canceled in the lower levels we see |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 884 | // a timeout error - see readLocked in vc/reader.go |
| 885 | if ctx.Err() == context.Canceled { |
Jiri Simsa | 074bf36 | 2015-02-17 09:29:45 -0800 | [diff] [blame] | 886 | return verror.ErrCanceled |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 887 | } |
Jiri Simsa | 074bf36 | 2015-02-17 09:29:45 -0800 | [diff] [blame] | 888 | return verror.ErrTimeout |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 889 | } |
| 890 | } |
Jiri Simsa | 074bf36 | 2015-02-17 09:29:45 -0800 | [diff] [blame] | 891 | return verror.ErrBadProtocol |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 892 | } |
| 893 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 894 | func (fc *flowClient) Recv(itemptr interface{}) error { |
Mehrdad Afshari | cd9852b | 2014-09-26 11:07:35 -0700 | [diff] [blame] | 895 | defer vlog.LogCall()() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 896 | switch { |
| 897 | case fc.response.Error != nil: |
Mike Burrows | 2ec2bb3 | 2015-02-26 15:14:43 -0800 | [diff] [blame] | 898 | // TODO(cnicolaou): this will become a verror.E when we convert the |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 899 | // server. |
Jiri Simsa | 074bf36 | 2015-02-17 09:29:45 -0800 | [diff] [blame] | 900 | return verror.New(verror.ErrBadProtocol, fc.ctx, fc.response.Error) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 901 | case fc.response.EndStreamResults: |
| 902 | return io.EOF |
| 903 | } |
| 904 | |
| 905 | // Decode the response header and handle errors and EOF. |
| 906 | if err := fc.dec.Decode(&fc.response); err != nil { |
Todd Wang | ff73e1f | 2015-02-10 21:45:52 -0800 | [diff] [blame] | 907 | berr := verror.New(decodeNetError(fc.ctx, err), fc.ctx, verror.New(errResponseDecoding, fc.ctx, err)) |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 908 | return fc.close(berr) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 909 | } |
| 910 | if fc.response.Error != nil { |
Mike Burrows | 2ec2bb3 | 2015-02-26 15:14:43 -0800 | [diff] [blame] | 911 | // TODO(cnicolaou): this will become a verror.E when we convert the |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 912 | // server. |
Jiri Simsa | 074bf36 | 2015-02-17 09:29:45 -0800 | [diff] [blame] | 913 | return verror.New(verror.ErrBadProtocol, fc.ctx, fc.response.Error) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 914 | } |
| 915 | if fc.response.EndStreamResults { |
| 916 | // Return EOF to indicate to the caller that there are no more stream |
| 917 | // results. Any error sent by the server is kept in fc.response.Error, and |
| 918 | // returned to the user in Finish. |
| 919 | return io.EOF |
| 920 | } |
| 921 | // Decode the streaming result. |
| 922 | if err := fc.dec.Decode(itemptr); err != nil { |
Todd Wang | ff73e1f | 2015-02-10 21:45:52 -0800 | [diff] [blame] | 923 | berr := verror.New(decodeNetError(fc.ctx, err), fc.ctx, verror.New(errResponseDecoding, fc.ctx, err)) |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 924 | // TODO(cnicolaou): should we be caching this? |
| 925 | fc.response.Error = berr |
| 926 | return fc.close(berr) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 927 | } |
| 928 | return nil |
| 929 | } |
| 930 | |
| 931 | func (fc *flowClient) CloseSend() error { |
Mehrdad Afshari | cd9852b | 2014-09-26 11:07:35 -0700 | [diff] [blame] | 932 | defer vlog.LogCall()() |
Tilak Sharma | 0c76611 | 2014-05-20 17:47:27 -0700 | [diff] [blame] | 933 | return fc.closeSend() |
| 934 | } |
| 935 | |
Mike Burrows | 2ec2bb3 | 2015-02-26 15:14:43 -0800 | [diff] [blame] | 936 | // closeSend ensures CloseSend always returns verror.E. |
Mike Burrows | dc6b360 | 2015-02-05 15:52:12 -0800 | [diff] [blame] | 937 | func (fc *flowClient) closeSend() error { |
Asim Shankar | 1707e43 | 2014-05-29 19:42:41 -0700 | [diff] [blame] | 938 | fc.sendClosedMu.Lock() |
| 939 | defer fc.sendClosedMu.Unlock() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 940 | if fc.sendClosed { |
Asim Shankar | 1707e43 | 2014-05-29 19:42:41 -0700 | [diff] [blame] | 941 | return nil |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 942 | } |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 943 | if err := fc.enc.Encode(rpc.Request{EndStreamArgs: true}); err != nil { |
Bogdan Caprita | ad5761f | 2014-09-23 10:56:23 -0700 | [diff] [blame] | 944 | // TODO(caprita): Indiscriminately closing the flow below causes |
| 945 | // a race as described in: |
| 946 | // https://docs.google.com/a/google.com/document/d/1C0kxfYhuOcStdV7tnLZELZpUhfQCZj47B0JrzbE29h8/edit |
| 947 | // |
| 948 | // There should be a finer grained way to fix this (for example, |
| 949 | // encoding errors should probably still result in closing the |
| 950 | // flow); on the flip side, there may exist other instances |
| 951 | // where we are closing the flow but should not. |
| 952 | // |
| 953 | // For now, commenting out the line below removes the flakiness |
| 954 | // from our existing unit tests, but this needs to be revisited |
| 955 | // and fixed correctly. |
| 956 | // |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 957 | // return fc.close(verror.ErrBadProtocolf("rpc: end stream args encoding failed: %v", err)) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 958 | } |
| 959 | fc.sendClosed = true |
| 960 | return nil |
| 961 | } |
| 962 | |
| 963 | func (fc *flowClient) Finish(resultptrs ...interface{}) error { |
Mehrdad Afshari | cd9852b | 2014-09-26 11:07:35 -0700 | [diff] [blame] | 964 | defer vlog.LogCall()() |
Matt Rosencrantz | 9fe6082 | 2014-09-12 10:09:53 -0700 | [diff] [blame] | 965 | err := fc.finish(resultptrs...) |
Matt Rosencrantz | 5f98d94 | 2015-01-08 13:48:30 -0800 | [diff] [blame] | 966 | vtrace.GetSpan(fc.ctx).Finish() |
Matt Rosencrantz | 9fe6082 | 2014-09-12 10:09:53 -0700 | [diff] [blame] | 967 | return err |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 968 | } |
| 969 | |
Mike Burrows | 2ec2bb3 | 2015-02-26 15:14:43 -0800 | [diff] [blame] | 970 | // finish ensures Finish always returns a verror.E. |
Cosmos Nicolaou | d1ca686 | 2015-01-30 11:43:39 -0800 | [diff] [blame] | 971 | func (fc *flowClient) finish(resultptrs ...interface{}) error { |
Ken Ashcraft | 2b8309a | 2014-09-09 10:44:43 -0700 | [diff] [blame] | 972 | if fc.finished { |
Todd Wang | ff73e1f | 2015-02-10 21:45:52 -0800 | [diff] [blame] | 973 | err := verror.New(errClientFinishAlreadyCalled, fc.ctx) |
Jiri Simsa | 074bf36 | 2015-02-17 09:29:45 -0800 | [diff] [blame] | 974 | return fc.close(verror.New(verror.ErrBadState, fc.ctx, err)) |
Ken Ashcraft | 2b8309a | 2014-09-09 10:44:43 -0700 | [diff] [blame] | 975 | } |
| 976 | fc.finished = true |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 977 | |
Todd Wang | ce3033b | 2014-05-23 17:04:44 -0700 | [diff] [blame] | 978 | // Call closeSend implicitly, if the user hasn't already called it. There are |
| 979 | // three cases: |
| 980 | // 1) Server is blocked on Recv waiting for the final request message. |
| 981 | // 2) Server has already finished processing, the final response message and |
| 982 | // out args are queued up on the client, and the flow is closed. |
| 983 | // 3) Between 1 and 2: the server isn't blocked on Recv, but the final |
| 984 | // response and args aren't queued up yet, and the flow isn't closed. |
| 985 | // |
| 986 | // We must call closeSend to handle case (1) and unblock the server; otherwise |
| 987 | // we'll deadlock with both client and server waiting for each other. We must |
| 988 | // ignore the error (if any) to handle case (2). In that case the flow is |
| 989 | // closed, meaning writes will fail and reads will succeed, and closeSend will |
| 990 | // always return an error. But this isn't a "real" error; the client should |
| 991 | // read the rest of the results and succeed. |
| 992 | _ = fc.closeSend() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 993 | // Decode the response header, if it hasn't already been decoded by Recv. |
| 994 | if fc.response.Error == nil && !fc.response.EndStreamResults { |
| 995 | if err := fc.dec.Decode(&fc.response); err != nil { |
Todd Wang | ff73e1f | 2015-02-10 21:45:52 -0800 | [diff] [blame] | 996 | berr := verror.New(decodeNetError(fc.ctx, err), fc.ctx, verror.New(errResponseDecoding, fc.ctx, err)) |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 997 | return fc.close(berr) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 998 | } |
| 999 | // The response header must indicate the streaming results have ended. |
| 1000 | if fc.response.Error == nil && !fc.response.EndStreamResults { |
Jiri Simsa | 074bf36 | 2015-02-17 09:29:45 -0800 | [diff] [blame] | 1001 | berr := verror.New(verror.ErrBadProtocol, fc.ctx, verror.New(errRemainingStreamResults, fc.ctx)) |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 1002 | return fc.close(berr) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1003 | } |
| 1004 | } |
Suharsh Sivakumar | 720b704 | 2014-12-22 17:33:23 -0800 | [diff] [blame] | 1005 | if fc.response.AckBlessings { |
| 1006 | clientAckBlessings(fc.flow.VCDataCache(), fc.blessings) |
| 1007 | } |
Matt Rosencrantz | 9fe6082 | 2014-09-12 10:09:53 -0700 | [diff] [blame] | 1008 | // Incorporate any VTrace info that was returned. |
Matt Rosencrantz | 2803fe9 | 2015-03-09 15:26:32 -0700 | [diff] [blame] | 1009 | vtrace.GetStore(fc.ctx).Merge(fc.response.TraceResponse) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1010 | if fc.response.Error != nil { |
Jiri Simsa | 074bf36 | 2015-02-17 09:29:45 -0800 | [diff] [blame] | 1011 | // TODO(cnicolaou): remove verror.ErrNoAccess with verror version |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 1012 | // when rpc.Server is converted. |
Todd Wang | 8fa3876 | 2015-03-25 14:04:59 -0700 | [diff] [blame] | 1013 | if verror.ErrorID(fc.response.Error) == verror.ErrNoAccess.ID && fc.dc != nil { |
Andres Erbsen | b7f95f3 | 2014-07-07 12:07:56 -0700 | [diff] [blame] | 1014 | // In case the error was caused by a bad discharge, we do not want to get stuck |
| 1015 | // with retrying again and again with this discharge. As there is no direct way |
| 1016 | // to detect it, we conservatively flush all discharges we used from the cache. |
| 1017 | // TODO(ataly,andreser): add verror.BadDischarge and handle it explicitly? |
Asim Shankar | 77befba | 2015-01-09 12:49:04 -0800 | [diff] [blame] | 1018 | vlog.VI(3).Infof("Discarding %d discharges as RPC failed with %v", len(fc.discharges), fc.response.Error) |
Ankur | e49a86a | 2014-11-11 18:52:43 -0800 | [diff] [blame] | 1019 | fc.dc.Invalidate(fc.discharges...) |
Andres Erbsen | b7f95f3 | 2014-07-07 12:07:56 -0700 | [diff] [blame] | 1020 | } |
Jiri Simsa | 074bf36 | 2015-02-17 09:29:45 -0800 | [diff] [blame] | 1021 | return fc.close(verror.Convert(verror.ErrInternal, fc.ctx, fc.response.Error)) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1022 | } |
| 1023 | if got, want := fc.response.NumPosResults, uint64(len(resultptrs)); got != want { |
Jiri Simsa | 074bf36 | 2015-02-17 09:29:45 -0800 | [diff] [blame] | 1024 | berr := verror.New(verror.ErrBadProtocol, fc.ctx, verror.New(errMismatchedResults, fc.ctx, got, want)) |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 1025 | return fc.close(berr) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1026 | } |
| 1027 | for ix, r := range resultptrs { |
| 1028 | if err := fc.dec.Decode(r); err != nil { |
Todd Wang | ff73e1f | 2015-02-10 21:45:52 -0800 | [diff] [blame] | 1029 | berr := verror.New(decodeNetError(fc.ctx, err), fc.ctx, verror.New(errResultDecoding, fc.ctx, ix, err)) |
Cosmos Nicolaou | 5a8a125 | 2014-12-01 14:14:25 -0800 | [diff] [blame] | 1030 | return fc.close(berr) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1031 | } |
| 1032 | } |
| 1033 | return fc.close(nil) |
| 1034 | } |
| 1035 | |
Asim Shankar | 2d731a9 | 2014-09-29 17:46:38 -0700 | [diff] [blame] | 1036 | func (fc *flowClient) RemoteBlessings() ([]string, security.Blessings) { |
Asim Shankar | 8f05c22 | 2014-10-06 22:08:19 -0700 | [diff] [blame] | 1037 | return fc.server, fc.flow.RemoteBlessings() |
Asim Shankar | 2d731a9 | 2014-09-29 17:46:38 -0700 | [diff] [blame] | 1038 | } |
Ankur | 50a5f39 | 2015-02-27 18:46:30 -0800 | [diff] [blame] | 1039 | |
| 1040 | func bpatterns(patterns []string) []security.BlessingPattern { |
| 1041 | if patterns == nil { |
| 1042 | return nil |
| 1043 | } |
| 1044 | bpatterns := make([]security.BlessingPattern, len(patterns)) |
| 1045 | for i, p := range patterns { |
| 1046 | bpatterns[i] = security.BlessingPattern(p) |
| 1047 | } |
| 1048 | return bpatterns |
| 1049 | } |