| 190 | } |
| 191 | |
| 192 | void Proxy::AsyncRequest(const string& method, |
| 193 | const google::protobuf::Message& req, |
| 194 | google::protobuf::Message* response, |
| 195 | RpcController* controller, |
| 196 | const ResponseCallback& callback) { |
| 197 | CHECK(!controller->call_) << "Controller should be reset"; |
| 198 | base::subtle::NoBarrier_Store(&is_started_, true); |
| 199 | // TODO(awong): it would be great if we didn't have to heap allocate the |
| 200 | // payload. |
| 201 | auto req_payload = RequestPayload::CreateRequestPayload( |
| 202 | RemoteMethod{service_name_, method}, |
| 203 | req, controller->ReleaseOutboundSidecars()); |
| 204 | if (!dns_resolver_) { |
| 205 | // NOTE: we don't expect the user-provided callback to free sidecars, so |
| 206 | // make sure the outbound call frees it for us. |
| 207 | EnqueueRequest(method, std::move(req_payload), response, controller, callback, |
| 208 | OutboundCall::CallbackBehavior::kFreeSidecars); |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | // If we haven't successfully initialized the remote, e.g. because the DNS |
| 213 | // lookup failed, refresh the DNS entry and enqueue the request. |
| 214 | bool remote_initialized; |
| 215 | { |
| 216 | std::lock_guard<simple_spinlock> l(lock_); |
| 217 | remote_initialized = conn_id_.remote().is_initialized(); |
| 218 | } |
| 219 | if (!remote_initialized) { |
| 220 | RefreshDnsAndEnqueueRequest(method, std::move(req_payload), response, controller, callback); |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | // Otherwise, just enqueue the request, but retry if there's an error, since |
| 225 | // it's possible the physical address of the host was changed. We only retry |
| 226 | // once more before calling the callback. |
| 227 | auto refresh_dns_and_cb = [this, &method, callback, response, controller] () { |
| 228 | // TODO(awong): we should be more specific here -- consider having the RPC |
| 229 | // layer set a flag in the controller that warrants a retry. |
| 230 | if (PREDICT_FALSE(!controller->status().ok())) { |
| 231 | KLOG_EVERY_N_SECS(WARNING, 5) |
| 232 | << Substitute("Call had error, refreshing address and retrying: $0", |
| 233 | controller->status().ToString()); |
| 234 | auto req_payload = controller->ReleaseRequestPayload(); |
| 235 | controller->Reset(); |
| 236 | RefreshDnsAndEnqueueRequest(method, std::move(req_payload), response, controller, callback); |
| 237 | return; |
| 238 | } |
| 239 | // For any other status, OK or otherwise, just run the callback. |
| 240 | controller->FreeOutboundSidecars(); |
| 241 | SCOPED_WATCH_STACK(100); |
| 242 | callback(); |
| 243 | }; |
| 244 | // Since we may end up using the request payload in the event of a retry, |
| 245 | // ensure the outbound call doesn't free the sidecars, and instead free |
| 246 | // manually from within our callback. |
| 247 | EnqueueRequest(method, std::move(req_payload), response, controller, refresh_dns_and_cb, |
| 248 | OutboundCall::CallbackBehavior::kDontFreeSidecars); |
| 249 | } |