simplify some codes

This commit is contained in:
XieHan
2021-11-19 20:41:05 +08:00
parent fb28c578c0
commit a16e760dac
5 changed files with 36 additions and 48 deletions
+11 -6
View File
@@ -64,8 +64,11 @@ int UpstreamManager::upstream_create_consistent_hash(const std::string& name,
upstream_route_t consistent_hash)
{
auto *ns = WFGlobal::get_name_service();
UPSConsistentHashPolicy *policy = new UPSConsistentHashPolicy(std::move(consistent_hash));
UPSConsistentHashPolicy *policy;
policy = new UPSConsistentHashPolicy(
consistent_hash ? std::move(consistent_hash) :
UpstreamManager::default_consistent_hash);
if (ns->add_policy(name.c_str(), policy) >= 0)
{
__UpstreamManager::get_instance()->add_policy_name(name);
@@ -110,12 +113,14 @@ int UpstreamManager::upstream_create_vswrr(const std::string& name)
int UpstreamManager::upstream_create_manual(const std::string& name,
upstream_route_t select,
bool try_another,
upstream_route_t consitent_hash)
upstream_route_t consistent_hash)
{
auto *ns = WFGlobal::get_name_service();
UPSManualPolicy *policy = new UPSManualPolicy(try_another,
std::move(select),
std::move(consitent_hash));
UPSManualPolicy *policy;
policy = new UPSManualPolicy(try_another, std::move(select),
consistent_hash ? std::move(consistent_hash) :
UpstreamManager::default_consistent_hash);
if (ns->add_policy(name.c_str(), policy) >= 0)
{
__UpstreamManager::get_instance()->add_policy_name(name);
+13
View File
@@ -21,6 +21,7 @@
#include <string>
#include <vector>
#include <functional>
#include "WFServiceGovernance.h"
#include "UpstreamPolicies.h"
#include "WFGlobal.h"
@@ -201,6 +202,18 @@ public:
const std::string& address,
const struct AddressParams *address_params);
private:
static unsigned int default_consistent_hash(const char *path,
const char *query,
const char *fragment)
{
static std::hash<std::string> std_hash;
std::string str(path);
str += query;
str += fragment;
return std_hash(str);
}
};
#endif
+8 -20
View File
@@ -636,16 +636,10 @@ int UPSVNSWRRPolicy::remove_server_locked(const std::string& address)
EndpointAddress *UPSConsistentHashPolicy::first_strategy(const ParsedURI& uri,
WFNSTracing *tracing)
{
unsigned int hash_value;
if (this->consistent_hash)
hash_value = this->consistent_hash(uri.path ? uri.path : "",
uri.query ? uri.query : "",
uri.fragment ? uri.fragment : "");
else
hash_value = this->default_consistent_hash(uri.path ? uri.path : "",
uri.query ? uri.query : "",
uri.fragment ? uri.fragment : "");
unsigned int hash_value = this->consistent_hash(
uri.path ? uri.path : "",
uri.query ? uri.query : "",
uri.fragment ? uri.fragment : "");
return this->consistent_hash_with_group(hash_value);
}
@@ -665,16 +659,10 @@ EndpointAddress *UPSManualPolicy::first_strategy(const ParsedURI& uri,
EndpointAddress *UPSManualPolicy::another_strategy(const ParsedURI& uri,
WFNSTracing *tracing)
{
unsigned int hash_value;
if (this->try_another_select)
hash_value = this->try_another_select(uri.path ? uri.path : "",
uri.query ? uri.query : "",
uri.fragment ? uri.fragment : "");
else
hash_value = UPSConsistentHashPolicy::default_consistent_hash(uri.path ? uri.path : "",
uri.query ? uri.query : "",
uri.fragment ? uri.fragment : "");
unsigned int hash_value = this->another_select(
uri.path ? uri.path : "",
uri.query ? uri.query : "",
uri.fragment ? uri.fragment : "");
return this->consistent_hash_with_group(hash_value);
}
+2 -20
View File
@@ -128,11 +128,6 @@ private:
class UPSConsistentHashPolicy : public UPSGroupPolicy
{
public:
UPSConsistentHashPolicy() :
consistent_hash(UPSConsistentHashPolicy::default_consistent_hash)
{
}
UPSConsistentHashPolicy(upstream_route_t&& consistent_hash) :
consistent_hash(std::move(consistent_hash))
{
@@ -144,19 +139,6 @@ protected:
private:
upstream_route_t consistent_hash;
public:
static unsigned int default_consistent_hash(const char *path,
const char *query,
const char *fragment)
{
static std::hash<std::string> std_hash;
std::string str(path);
str += query;
str += fragment;
return std_hash(str);
}
};
class UPSManualPolicy : public UPSGroupPolicy
@@ -165,7 +147,7 @@ public:
UPSManualPolicy(bool try_another, upstream_route_t&& select,
upstream_route_t&& try_another_select) :
manual_select(std::move(select)),
try_another_select(std::move(try_another_select))
another_select(std::move(try_another_select))
{
this->try_another = try_another;
}
@@ -177,7 +159,7 @@ public:
private:
upstream_route_t manual_select;
upstream_route_t try_another_select;
upstream_route_t another_select;
};
#endif
+2 -2
View File
@@ -66,7 +66,7 @@ public:
dns_additional_cursor_init(&cursor, parser);
}
bool next(dns_record **next_record)
bool next(struct dns_record **next_record)
{
int ret = dns_record_cursor_next(&record, &cursor);
if (ret != 0)
@@ -85,7 +85,7 @@ public:
private:
const dns_parser_t *parser;
dns_record_cursor_t cursor;
dns_record *record;
struct dns_record *record;
};
#endif