Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/brpc/global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,8 @@ static void GlobalInitializeOrDieImpl() {
ParseTransportHandshake, nullptr, nullptr,
ProcessTransportHandshake, nullptr,
nullptr, nullptr, nullptr,
CONNECTION_TYPE_ALL, "rdma_handshake" };
// Retain the existing enum value and registered name to avoid changing
// public protocol identifiers while widening the implementation from RDMA
// to all transport upgrades.
if (RegisterProtocol(PROTOCOL_RDMA_HANDSHAKE,
CONNECTION_TYPE_ALL, "transport_handshake" };
if (RegisterProtocol(PROTOCOL_TRANSPORT_HANDSHAKE,
transport_handshake_protocol) != 0) {
exit(1);
}
Expand Down
2 changes: 1 addition & 1 deletion src/brpc/options.proto
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ enum ConnectionType {

enum ProtocolType {
PROTOCOL_UNKNOWN = 0;
PROTOCOL_RDMA_HANDSHAKE = 1;
PROTOCOL_TRANSPORT_HANDSHAKE = 1;
PROTOCOL_BAIDU_STD = 2;
PROTOCOL_STREAMING_RPC = 3;
PROTOCOL_HULU_PBRPC = 4;
Expand Down
8 changes: 4 additions & 4 deletions src/brpc/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,8 +618,8 @@ BUTIL_FORCE_INLINE bool is_http_protocol(const char* name) {
return strcmp(name, "http") == 0 || strcmp(name, "h2") == 0;
}

BUTIL_FORCE_INLINE bool is_rdma_handshake_protocol(const char* name) {
return strcmp(name, "rdma_handshake") == 0;
BUTIL_FORCE_INLINE bool is_transport_handshake_protocol(const char* name) {
return strcmp(name, "transport_handshake") == 0;
}

Acceptor* Server::BuildAcceptor() {
Expand All @@ -640,12 +640,12 @@ Acceptor* Server::BuildAcceptor() {
continue;
}
// Erase whatever the exemptions below say. http, h2 and
// rdma_handshake are always served, but they are still
// transport_handshake are always served, but they are still
// valid names for the whitelist.
bool in_whitelist = (whitelist.erase(protocols[i].name) != 0);
if (has_whitelist && !in_whitelist &&
!is_http_protocol(protocols[i].name) &&
!is_rdma_handshake_protocol(protocols[i].name)) {
!is_transport_handshake_protocol(protocols[i].name)) {
// the protocol is not allowed to serve.
RPC_VLOG << "Skip protocol=" << protocols[i].name;
continue;
Expand Down
4 changes: 2 additions & 2 deletions src/brpc/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,9 @@ struct ServerOptions {

// Only enable these protocols, separated by spaces.
// All names inside must be valid, check protocols name in global.cpp
// http/h2 and rdma_handshake are served whatever this field says:
// http/h2 and transport_handshake are served whatever this field says:
// the builtin services are only reachable over http/h2, and
// rdma_handshake is a transport level handshake dispatching no request.
// transport_handshake is a transport level handshake dispatching no request.
// Naming them here is allowed and changes nothing.
// Default: empty (all protocols)
std::string enabled_protocols;
Expand Down
6 changes: 3 additions & 3 deletions test/brpc_server_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,16 +584,16 @@ TEST_F(ServerTest, only_allow_protocols_in_enabled_protocols) {
ASSERT_EQ(0, server.Join());
}

// http, h2 and rdma_handshake are served whatever enabled_protocols says, but
// http, h2 and transport_handshake are served whatever enabled_protocols says, but
// naming them in it used to make Start() fail: the exemption was tested
// before whitelist.erase() and `&&' short-circuited it, so the names survived
// into the leftover check and came back as "unknown protocols=`http '".
TEST_F(ServerTest, enabled_protocols_can_name_always_enabled_protocols) {
std::string cases[] = {
"baidu_std http",
"baidu_std h2",
"baidu_std rdma_handshake",
"baidu_std http h2 rdma_handshake",
"baidu_std transport_handshake",
"baidu_std http h2 transport_handshake",
};
for (size_t i = 0; i < arraysize(cases); ++i) {
brpc::Server server;
Expand Down
16 changes: 16 additions & 0 deletions test/brpc_transport_handshake_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
#include "butil/fd_guard.h"
#include "butil/sys_byteorder.h"
#include "brpc/adapter_transport.h"
#include "brpc/global.h"
#include "brpc/handshake/handshake_io.h"
#include "brpc/handshake/handshake_adapter.h"
#include "brpc/input_messenger.h"
#include "brpc/policy/transport_handshake_protocol.h"
#include "brpc/protocol.h"
#if BRPC_WITH_RDMA
#include "brpc/rdma_transport.h"
#endif
Expand Down Expand Up @@ -275,6 +277,20 @@ class TestUpgradeMessenger : public InputMessenger {
}
};

TEST(TransportHandshakeTest, registered_protocol_uses_transport_name) {
GlobalInitializeOrDie();
EXPECT_EQ(1, PROTOCOL_TRANSPORT_HANDSHAKE);
EXPECT_EQ(PROTOCOL_TRANSPORT_HANDSHAKE,
StringToProtocolType("transport_handshake"));
EXPECT_STREQ("transport_handshake",
ProtocolTypeToString(PROTOCOL_TRANSPORT_HANDSHAKE));

const Protocol* protocol = FindProtocol(PROTOCOL_TRANSPORT_HANDSHAKE);
ASSERT_NE(nullptr, protocol);
EXPECT_EQ(policy::ParseTransportHandshake, protocol->parse);
EXPECT_EQ(policy::ProcessTransportHandshake, protocol->process_request);
}

TEST(TransportHandshakeTest, adapter_retains_context_for_fragmented_extension) {
int fds[2];
ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
Expand Down
Loading