Merge pull request #23643 from iposva-google/render_server_sockets

Fix pagination in ServerNode::RenderServerSockets.
pull/23650/head
Yang Gao 4 years ago committed by GitHub
commit 9e095b2076
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 27
      src/core/lib/channel/channelz.cc
  2. 4
      src/core/lib/channel/channelz_registry.cc

@ -310,27 +310,26 @@ void ServerNode::RemoveChildListenSocket(intptr_t child_uuid) {
std::string ServerNode::RenderServerSockets(intptr_t start_socket_id,
intptr_t max_results) {
GPR_ASSERT(start_socket_id >= 0);
GPR_ASSERT(max_results >= 0);
// If user does not set max_results, we choose 500.
size_t pagination_limit = max_results == 0 ? 500 : max_results;
Json::Object object;
{
MutexLock lock(&child_mu_);
size_t sockets_rendered = 0;
if (!child_sockets_.empty()) {
// Create list of socket refs.
Json::Array array;
const size_t limit = GPR_MIN(child_sockets_.size(), pagination_limit);
for (auto it = child_sockets_.lower_bound(start_socket_id);
it != child_sockets_.end() && sockets_rendered < limit;
++it, ++sockets_rendered) {
array.emplace_back(Json::Object{
{"socketId", std::to_string(it->first)},
{"name", it->second->name()},
});
}
object["socketRef"] = std::move(array);
// Create list of socket refs.
Json::Array array;
auto it = child_sockets_.lower_bound(start_socket_id);
for (; it != child_sockets_.end() && sockets_rendered < pagination_limit;
++it, ++sockets_rendered) {
array.emplace_back(Json::Object{
{"socketId", std::to_string(it->first)},
{"name", it->second->name()},
});
}
if (sockets_rendered == child_sockets_.size()) object["end"] = true;
object["socketRef"] = std::move(array);
if (it == child_sockets_.end()) object["end"] = true;
}
Json json = std::move(object);
return json.Dump();

@ -208,10 +208,12 @@ char* grpc_channelz_get_server(intptr_t server_id) {
char* grpc_channelz_get_server_sockets(intptr_t server_id,
intptr_t start_socket_id,
intptr_t max_results) {
// Validate inputs before handing them of to the renderer.
grpc_core::RefCountedPtr<grpc_core::channelz::BaseNode> base_node =
grpc_core::channelz::ChannelzRegistry::Get(server_id);
if (base_node == nullptr ||
base_node->type() != grpc_core::channelz::BaseNode::EntityType::kServer) {
base_node->type() != grpc_core::channelz::BaseNode::EntityType::kServer ||
start_socket_id < 0 || max_results < 0) {
return nullptr;
}
// This cast is ok since we have just checked to make sure base_node is

Loading…
Cancel
Save