/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include #include #include #include #include #include #include #include using apache::thrift::ClientConnectionIf; using apache::thrift::H2ClientConnection; using apache::thrift::HeaderClientChannel; using apache::thrift::RocketClientChannel; using apache::thrift::ThriftClient; using apache::thrift::ThriftServerAsyncProcessorFactory; using apache::thrift::server::ServerConfigsMock; namespace apache { namespace thrift { namespace perf { folly::AsyncSocket::UniquePtr getSocket( folly::EventBase* evb, const folly::SocketAddress& addr, bool encrypted, std::list advertizedProtocols = {}); } // namespace perf } // namespace thrift } // namespace apache template static std::unique_ptr newHeaderClient( folly::EventBase* evb, const folly::SocketAddress& addr) { auto sock = apache::thrift::perf::getSocket(evb, addr, false); auto chan = HeaderClientChannel::newChannel(std::move(sock)); return std::make_unique(std::move(chan)); } template static std::unique_ptr newHTTP2Client( folly::EventBase* evb, const folly::SocketAddress& addr, bool encrypted) { auto sock = apache::thrift::perf::getSocket(evb, addr, encrypted, {"h2"}); std::shared_ptr conn = H2ClientConnection::newHTTP2Connection(std::move(sock)); auto client = ThriftClient::Ptr(new ThriftClient(conn, evb)); client->setProtocolId(apache::thrift::protocol::T_COMPACT_PROTOCOL); client->setTimeout(500); return std::make_unique(std::move(client)); } template static std::unique_ptr newRocketClient( folly::EventBase* evb, const folly::SocketAddress& addr, bool encrypted) { auto sock = apache::thrift::perf::getSocket(evb, addr, encrypted, {"rs2"}); RocketClientChannel::Ptr channel = RocketClientChannel::newChannel(std::move(sock)); return std::make_unique(std::move(channel)); } template static std::unique_ptr newClient( folly::EventBase* evb, const folly::SocketAddress& addr, folly::StringPiece transport, bool encrypted = false) { if (transport == "header") { return newHeaderClient(evb, addr); } if (transport == "rocket") { return newRocketClient(evb, addr, encrypted); } if (transport == "http2") { return newHTTP2Client(evb, addr, encrypted); } return nullptr; } template class ConnectionThread : public folly::ScopedEventBaseThread { public: ~ConnectionThread() { getEventBase()->runInEventBaseThreadAndWait([&] { connection_.reset(); }); } std::shared_ptr newSyncClient( const folly::SocketAddress& addr, folly::StringPiece transport, bool encrypted = false) { DCHECK(connection_ == nullptr); getEventBase()->runInEventBaseThreadAndWait([&]() { connection_ = newClient(getEventBase(), addr, transport, encrypted); }); return connection_; } private: std::shared_ptr connection_; };