Merge pull request #24731 from yang-g/lame_channel

Return given code/message for lame channel rather than Unknown.
pull/24577/head
Yang Gao 4 years ago committed by GitHub
commit 8dc7d6a37e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 56
      src/core/lib/surface/lame_client.cc
  2. 9
      test/core/surface/lame_client_test.cc

@ -39,59 +39,25 @@ namespace grpc_core {
namespace {
struct CallData {
CallCombiner* call_combiner;
grpc_linked_mdelem status;
grpc_linked_mdelem details;
Atomic<bool> filled_metadata;
};
struct ChannelData {
ChannelData() : state_tracker("lame_channel", GRPC_CHANNEL_SHUTDOWN) {}
~ChannelData() { GRPC_ERROR_UNREF(error); }
grpc_status_code error_code;
const char* error_message;
grpc_error* error = GRPC_ERROR_NONE;
Mutex mu;
ConnectivityStateTracker state_tracker;
};
static void fill_metadata(grpc_call_element* elem, grpc_metadata_batch* mdb) {
CallData* calld = static_cast<CallData*>(elem->call_data);
bool expected = false;
if (!calld->filled_metadata.CompareExchangeStrong(
&expected, true, MemoryOrder::RELAXED, MemoryOrder::RELAXED)) {
return;
}
ChannelData* chand = static_cast<ChannelData*>(elem->channel_data);
char tmp[GPR_LTOA_MIN_BUFSIZE];
gpr_ltoa(chand->error_code, tmp);
calld->status.md = grpc_mdelem_from_slices(
GRPC_MDSTR_GRPC_STATUS, grpc_core::UnmanagedMemorySlice(tmp));
calld->details.md = grpc_mdelem_from_slices(
GRPC_MDSTR_GRPC_MESSAGE,
grpc_core::UnmanagedMemorySlice(chand->error_message));
calld->status.prev = calld->details.next = nullptr;
calld->status.next = &calld->details;
calld->details.prev = &calld->status;
mdb->list.head = &calld->status;
mdb->list.tail = &calld->details;
mdb->list.count = 2;
mdb->deadline = GRPC_MILLIS_INF_FUTURE;
}
struct CallData {
CallCombiner* call_combiner;
};
static void lame_start_transport_stream_op_batch(
grpc_call_element* elem, grpc_transport_stream_op_batch* op) {
CallData* calld = static_cast<CallData*>(elem->call_data);
if (op->recv_initial_metadata) {
fill_metadata(elem,
op->payload->recv_initial_metadata.recv_initial_metadata);
} else if (op->recv_trailing_metadata) {
fill_metadata(elem,
op->payload->recv_trailing_metadata.recv_trailing_metadata);
}
ChannelData* chand = static_cast<ChannelData*>(elem->channel_data);
grpc_transport_stream_op_batch_finish_with_failure(
op, GRPC_ERROR_CREATE_FROM_STATIC_STRING("lame client channel"),
calld->call_combiner);
op, GRPC_ERROR_REF(chand->error), calld->call_combiner);
}
static void lame_get_channel_info(grpc_channel_element* /*elem*/,
@ -184,8 +150,12 @@ grpc_channel* grpc_lame_client_channel_create(const char* target,
3, (target, (int)error_code, error_message));
GPR_ASSERT(elem->filter == &grpc_lame_filter);
auto chand = static_cast<grpc_core::ChannelData*>(elem->channel_data);
chand->error_code = error_code;
chand->error_message = error_message;
chand->error = grpc_error_set_str(
grpc_error_set_int(
GRPC_ERROR_CREATE_FROM_STATIC_STRING("lame client channel"),
GRPC_ERROR_INT_GRPC_STATUS, error_code),
GRPC_ERROR_STR_GRPC_MESSAGE,
grpc_slice_from_static_string(error_message));
return channel;
}

@ -76,8 +76,10 @@ int main(int argc, char** argv) {
grpc_metadata_array_init(&initial_metadata_recv);
grpc_metadata_array_init(&trailing_metadata_recv);
chan = grpc_lame_client_channel_create(
"lampoon:national", GRPC_STATUS_UNKNOWN, "Rpc sent on a lame channel.");
const char* error_message = "Rpc sent on a lame channel.";
grpc_status_code error_code = GRPC_STATUS_ABORTED;
chan = grpc_lame_client_channel_create("lampoon:national", error_code,
error_message);
GPR_ASSERT(chan);
test_transport_op(chan);
@ -136,6 +138,9 @@ int main(int argc, char** argv) {
GPR_ASSERT(strcmp(peer, "lampoon:national") == 0);
gpr_free(peer);
GPR_ASSERT(status == error_code);
GPR_ASSERT(grpc_slice_str_cmp(details, error_message) == 0);
grpc_call_unref(call);
grpc_channel_destroy(chan);
cq_verifier_destroy(cqv);

Loading…
Cancel
Save