Skip to content

Commit

Permalink
feat: support load balancing
Browse files Browse the repository at this point in the history
Signed-off-by: ik <[email protected]>
  • Loading branch information
hiiiik committed Jun 4, 2024
1 parent d5fab0b commit d424b8c
Show file tree
Hide file tree
Showing 15 changed files with 756 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ else()
opengemini/impl/http/IHttpClient.cpp
opengemini/impl/http/HttpClient.cpp
opengemini/impl/http/HttpsClient.cpp
opengemini/impl/lb/LoadBalancer.cpp
)
opengemini_target_setting(Client PUBLIC)
endif()
Expand Down
20 changes: 20 additions & 0 deletions include/opengemini/Error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ class Error {
///
const std::error_code& Code() const noexcept { return code_; }

void Clear() noexcept
{
code_.clear();
info_.clear();
}

void Assign(const std::error_code& code, std::string info = {}) noexcept
{
code_.assign(code.value(), code.category());
info_ = std::move(info);
}

friend void swap(Error& lhs, Error& rhs) noexcept
{
using std::swap;
Expand Down Expand Up @@ -120,6 +132,14 @@ enum class LogicErrors {
InvalidArgument,
};

enum class ServerErrors {
NoAvailableServer = 1,
};

enum class RuntimeErrors {
Unexcepted = 1,

Check warning on line 140 in include/opengemini/Error.hpp

View workflow job for this annotation

GitHub Actions / typo check

"Unexcepted" should be "Unexpected".
};

} // namespace opengemini::errc

#include "opengemini/impl/ErrorCode.hpp"
Expand Down
68 changes: 68 additions & 0 deletions include/opengemini/impl/ErrorCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,27 @@

namespace opengemini::errc {

OPENGEMINI_INLINE_SPECIFIER
std::error_code make_error_code(LogicErrors error)
{
return std::error_code(static_cast<int>(error),
impl::LogicCategory::Instance());
}

OPENGEMINI_INLINE_SPECIFIER
std::error_code make_error_code(ServerErrors error)
{
return std::error_code(static_cast<int>(error),
impl::ServerCategory::Instance());
}

OPENGEMINI_INLINE_SPECIFIER
std::error_code make_error_code(RuntimeErrors error)
{
return std::error_code(static_cast<int>(error),
impl::RuntimeCategory::Instance());
}

} // namespace opengemini::errc

namespace opengemini::errc::impl {
Expand All @@ -50,6 +65,59 @@ std::string LogicCategory::message(int value) const
case LogicErrors::NotImplemented: return "Not implemented";
case LogicErrors::InvalidArgument: return "Invalid argument";
}
return "Unknown";
}

} // namespace opengemini::errc::impl

namespace opengemini::errc::impl {

OPENGEMINI_INLINE_SPECIFIER
const ServerCategory& ServerCategory::Instance()
{
static ServerCategory instance;
return instance;
}

OPENGEMINI_INLINE_SPECIFIER
const char* ServerCategory::name() const noexcept
{
return "opengemini.server";
}

OPENGEMINI_INLINE_SPECIFIER
std::string ServerCategory::message(int value) const
{
switch (static_cast<ServerErrors>(value)) {
case ServerErrors::NoAvailableServer: return "No available server";
}
return "Unknown";
}

} // namespace opengemini::errc::impl

namespace opengemini::errc::impl {

OPENGEMINI_INLINE_SPECIFIER
const RuntimeCategory& RuntimeCategory::Instance()
{
static RuntimeCategory instance;
return instance;
}

OPENGEMINI_INLINE_SPECIFIER
const char* RuntimeCategory::name() const noexcept
{
return "opengemini.runtime";
}

OPENGEMINI_INLINE_SPECIFIER
std::string RuntimeCategory::message(int value) const
{
switch (static_cast<RuntimeErrors>(value)) {
case RuntimeErrors::Unexcepted: return "Unexcepted error happened";

Check warning on line 118 in include/opengemini/impl/ErrorCode.cpp

View workflow job for this annotation

GitHub Actions / typo check

"Unexcepted" should be "Unexpected".

Check warning on line 118 in include/opengemini/impl/ErrorCode.cpp

View workflow job for this annotation

GitHub Actions / typo check

"Unexcepted" should be "Unexpected".
}
return "Unknown";
}

} // namespace opengemini::errc::impl
26 changes: 25 additions & 1 deletion include/opengemini/impl/ErrorCode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,21 @@ template<>
struct is_error_code_enum<opengemini::errc::LogicErrors> :
public std::true_type { };

template<>
struct is_error_code_enum<opengemini::errc::ServerErrors> :
public std::true_type { };

template<>
struct is_error_code_enum<opengemini::errc::RuntimeErrors> :
public std::true_type { };

} // namespace std

namespace opengemini::errc {

inline std::error_code make_error_code(LogicErrors error);
std::error_code make_error_code(LogicErrors error);
std::error_code make_error_code(ServerErrors error);
std::error_code make_error_code(RuntimeErrors error);

} // namespace opengemini::errc

Expand All @@ -44,6 +54,20 @@ class LogicCategory : public std::error_category {
std::string message(int value) const override;
};

class ServerCategory : public std::error_category {
public:
static const ServerCategory& Instance();
const char* name() const noexcept override;
std::string message(int value) const override;
};

class RuntimeCategory : public std::error_category {
public:
static const RuntimeCategory& Instance();
const char* name() const noexcept override;
std::string message(int value) const override;
};

} // namespace opengemini::errc::impl

#ifndef OPENGEMINI_SEPARATE_COMPILATION
Expand Down
26 changes: 26 additions & 0 deletions include/opengemini/impl/comm/UrlTargets.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Copyright 2024 Huawei Cloud Computing Technologies Co., Ltd.
//
// 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.
//

#ifndef OPENGEMINI_IMPL_COMM_URLTARGETS_HPP
#define OPENGEMINI_IMPL_COMM_URLTARGETS_HPP

namespace opengemini::impl::url {

inline constexpr auto PING = "/ping";

} // namespace opengemini::impl::url

#endif // !OPENGEMINI_IMPL_COMM_URLTARGETS_HPP
30 changes: 30 additions & 0 deletions include/opengemini/impl/http/IHttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <fmt/format.h>

#include "opengemini/Version.hpp"
#include "opengemini/impl/util/ErrorHandling.hpp"
#include "opengemini/impl/util/Preprocessor.hpp"

namespace opengemini::impl::http {
Expand Down Expand Up @@ -50,6 +51,20 @@ Response IHttpClient::Get(Endpoint endpoint,
return SendRequest(std::move(endpoint), std::move(request), yield);
}

OPENGEMINI_INLINE_SPECIFIER
Response IHttpClient::Get(Endpoint endpoint,
std::string target,
boost::asio::yield_context yield,
Error& error)
try {
error.Clear();
return Get(std::move(endpoint), std::move(target), yield);
}
catch (...) {
util::ConvertError(error);
return {};
}

OPENGEMINI_INLINE_SPECIFIER
Response IHttpClient::Post(Endpoint endpoint,
std::string target,
Expand All @@ -63,6 +78,21 @@ Response IHttpClient::Post(Endpoint endpoint,
return SendRequest(std::move(endpoint), std::move(request), yield);
}

OPENGEMINI_INLINE_SPECIFIER
Response IHttpClient::Post(Endpoint endpoint,
std::string target,
std::string body,
boost::asio::yield_context yield,
Error& error)
try {
error.Clear();
return Post(std::move(endpoint), std::move(target), std::move(body), yield);
}
catch (...) {
util::ConvertError(error);
return {};
}

OPENGEMINI_INLINE_SPECIFIER
std::unordered_map<std::string, std::string>&
IHttpClient::DefaultHeaders() noexcept
Expand Down
12 changes: 12 additions & 0 deletions include/opengemini/impl/http/IHttpClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <boost/beast.hpp>

#include "opengemini/Endpoint.hpp"
#include "opengemini/Error.hpp"
#include "opengemini/impl/comm/TaskSlot.hpp"

namespace opengemini::impl::http {
Expand All @@ -44,11 +45,22 @@ class IHttpClient : public TaskSlot {
std::string target,
boost::asio::yield_context yield);

Response Get(Endpoint endpoint,
std::string target,
boost::asio::yield_context yield,
Error& error);

Response Post(Endpoint endpoint,
std::string target,
std::string body,
boost::asio::yield_context yield);

Response Post(Endpoint endpoint,
std::string target,
std::string body,
boost::asio::yield_context yield,
Error& error);

std::unordered_map<std::string, std::string>& DefaultHeaders() noexcept;

protected:
Expand Down
Loading

0 comments on commit d424b8c

Please sign in to comment.