@@ -197,19 +197,15 @@ void SendRpcResponse(int64_t correlation_id, Controller* cntl,
197
197
const google::protobuf::Message* req = NULL == messages ? NULL : messages->Request ();
198
198
const google::protobuf::Message* res = NULL == messages ? NULL : messages->Response ();
199
199
200
- ResponseWriteInfo args;
201
-
202
200
// Recycle resources at the end of this function.
203
201
BRPC_SCOPE_EXIT {
204
202
{
205
203
// Remove concurrency and record latency at first.
206
204
ConcurrencyRemover concurrency_remover (method_status, cntl, received_us);
207
- concurrency_remover.set_sent_us (args.sent_us );
208
205
}
209
206
210
207
std::unique_ptr<Controller, LogErrorTextAndDelete> recycle_cntl (cntl);
211
208
212
-
213
209
if (NULL == messages) {
214
210
return ;
215
211
}
@@ -305,12 +301,13 @@ void SendRpcResponse(int64_t correlation_id, Controller* cntl,
305
301
}
306
302
}
307
303
304
+ ResponseWriteInfo args;
305
+ bthread_id_t response_id = INVALID_BTHREAD_ID;
308
306
if (span) {
309
307
span->set_response_size (res_buf.size ());
308
+ CHECK_EQ (0 , bthread_id_create (&response_id, &args, HandleResponseWritten));
310
309
}
311
310
312
- bthread_id_t response_id;
313
- CHECK_EQ (0 , bthread_id_create2 (&response_id, &args, HandleResponseWritten));
314
311
// Send rpc response over stream even if server side failed to create
315
312
// stream for some reason.
316
313
if (cntl->has_remote_stream ()) {
@@ -328,45 +325,13 @@ void SendRpcResponse(int64_t correlation_id, Controller* cntl,
328
325
Stream::SetFailed (response_stream_ids, error_code,
329
326
" Fail to write into %s" ,
330
327
sock->description ().c_str ());
331
- }
332
- } else {
333
- // Have the risk of unlimited pending responses, in which case, tell
334
- // users to set max_concurrency.
335
- Socket::WriteOptions wopt;
336
- wopt.id_wait = response_id;
337
- wopt.notify_on_success = true ;
338
- wopt.ignore_eovercrowded = true ;
339
- if (sock->Write (&res_buf, &wopt) != 0 ) {
340
- const int errcode = errno;
341
- PLOG_IF (WARNING, errcode != EPIPE) << " Fail to write into " << *sock;
342
- cntl->SetFailed (errcode, " Fail to write into %s" ,
343
- sock->description ().c_str ());
344
- return ;
345
- }
346
- }
347
-
348
- bthread_id_join (response_id);
349
-
350
- error_code = args.error_code ;
351
- if (cntl->has_remote_stream ()) {
352
- if (0 != error_code) {
353
- LOG_IF (WARNING, error_code != EPIPE)
354
- << " Fail to write into " << *sock
355
- << " , error text= " << args.error_text
356
- << " : " << berror (error_code);
357
- cntl->SetFailed (error_code, " Fail to write into %s: %s" ,
358
- sock->description ().c_str (),
359
- args.error_text .c_str ());
360
- Stream::SetFailed (response_stream_ids, error_code,
361
- " Fail to write into %s" ,
362
- args.error_text .c_str ());
363
328
return ;
364
329
}
365
330
366
331
// Now it's ok the mark these server-side streams as connected as all the
367
332
// written user data would follower the RPC response.
368
333
// Reuse stream_ptr to avoid address first stream id again
369
- if (NULL != stream_ptr) {
334
+ if (stream_ptr) {
370
335
((Stream*)stream_ptr->conn ())->SetConnected ();
371
336
}
372
337
for (size_t i = 1 ; i < response_stream_ids.size (); ++i) {
@@ -384,17 +349,24 @@ void SendRpcResponse(int64_t correlation_id, Controller* cntl,
384
349
} else {
385
350
// Have the risk of unlimited pending responses, in which case, tell
386
351
// users to set max_concurrency.
387
- if (0 != error_code) {
388
- LOG_IF (WARNING, error_code != EPIPE) << " Fail to write into " << *sock
389
- << " , error text= " << args.error_text
390
- << " : " << strerror (error_code);
391
- cntl->SetFailed (error_code, " Fail to write into %s: %s" ,
392
- sock->description ().c_str (), args.error_text .c_str ());
352
+ Socket::WriteOptions wopt;
353
+ wopt.ignore_eovercrowded = true ;
354
+ if (INVALID_BTHREAD_ID != response_id) {
355
+ wopt.id_wait = response_id;
356
+ wopt.notify_on_success = true ;
357
+ }
358
+ if (sock->Write (&res_buf, &wopt) != 0 ) {
359
+ const int errcode = errno;
360
+ PLOG_IF (WARNING, errcode != EPIPE) << " Fail to write into " << *sock;
361
+ cntl->SetFailed (errcode, " Fail to write into %s" ,
362
+ sock->description ().c_str ());
393
363
return ;
394
364
}
395
365
}
396
366
397
367
if (span) {
368
+ bthread_id_join (response_id);
369
+ // Do not care about the result of background writing.
398
370
// TODO: this is not sent
399
371
span->set_sent_us (args.sent_us );
400
372
}
0 commit comments