Commit 12e73e1d authored by jamesge's avatar jamesge

reuse DiscoveryRegisterParam in DiscoveryClient

parent bc7165fb
...@@ -166,11 +166,11 @@ int DiscoveryClient::DoRenew() const { ...@@ -166,11 +166,11 @@ int DiscoveryClient::DoRenew() const {
cntl.http_request().uri() = "/discovery/renew"; cntl.http_request().uri() = "/discovery/renew";
cntl.http_request().set_content_type("application/x-www-form-urlencoded"); cntl.http_request().set_content_type("application/x-www-form-urlencoded");
butil::IOBufBuilder os; butil::IOBufBuilder os;
os << "appid=" << _appid os << "appid=" << _params.appid
<< "&hostname=" << _hostname << "&hostname=" << _params.hostname
<< "&env=" << _env << "&env=" << _params.env
<< "&region=" << _region << "&region=" << _params.region
<< "&zone=" << _zone; << "&zone=" << _params.zone;
os.move_to(cntl.request_attachment()); os.move_to(cntl.request_attachment());
GetDiscoveryChannel()->CallMethod(NULL, &cntl, NULL, NULL, NULL); GetDiscoveryChannel()->CallMethod(NULL, &cntl, NULL, NULL, NULL);
if (cntl.Failed()) { if (cntl.Failed()) {
...@@ -179,7 +179,7 @@ int DiscoveryClient::DoRenew() const { ...@@ -179,7 +179,7 @@ int DiscoveryClient::DoRenew() const {
} }
std::string error_text; std::string error_text;
if (ParseCommonResult(cntl.response_attachment(), &error_text) != 0) { if (ParseCommonResult(cntl.response_attachment(), &error_text) != 0) {
LOG(ERROR) << "Fail to renew " << _hostname << " to " << _appid LOG(ERROR) << "Fail to renew " << _params.hostname << " to " << _params.appid
<< ": " << error_text; << ": " << error_text;
return -1; return -1;
} }
...@@ -220,22 +220,14 @@ void* DiscoveryClient::PeriodicRenew(void* arg) { ...@@ -220,22 +220,14 @@ void* DiscoveryClient::PeriodicRenew(void* arg) {
} }
int DiscoveryClient::Register(const DiscoveryRegisterParam& req) { int DiscoveryClient::Register(const DiscoveryRegisterParam& req) {
if (!req.IsValid()) {
return -1;
}
if (_registered.load(butil::memory_order_relaxed) || if (_registered.load(butil::memory_order_relaxed) ||
_registered.exchange(true, butil::memory_order_release)) { _registered.exchange(true, butil::memory_order_release)) {
return 0; return 0;
} }
_appid = req.appid; if (!req.IsValid()) {
_hostname = req.hostname; return -1;
_addrs = req.addrs; }
_env = req.env; _params = req;
_region = req.region;
_zone = req.zone;
_status = req.status;
_version = req.version;
_metadata = req.metadata;
if (DoRegister() != 0) { if (DoRegister() != 0) {
return -1; return -1;
...@@ -253,24 +245,24 @@ int DiscoveryClient::DoRegister() const { ...@@ -253,24 +245,24 @@ int DiscoveryClient::DoRegister() const {
cntl.http_request().uri() = "/discovery/register"; cntl.http_request().uri() = "/discovery/register";
cntl.http_request().set_content_type("application/x-www-form-urlencoded"); cntl.http_request().set_content_type("application/x-www-form-urlencoded");
butil::IOBufBuilder os; butil::IOBufBuilder os;
os << "appid=" << _appid os << "appid=" << _params.appid
<< "&hostname=" << _hostname << "&hostname=" << _params.hostname
<< "&addrs=" << _addrs << "&addrs=" << _params.addrs
<< "&env=" << _env << "&env=" << _params.env
<< "&zone=" << _zone << "&zone=" << _params.zone
<< "&region=" << _region << "&region=" << _params.region
<< "&status=" << _status << "&status=" << _params.status
<< "&version=" << _version << "&version=" << _params.version
<< "&metadata=" << _metadata; << "&metadata=" << _params.metadata;
os.move_to(cntl.request_attachment()); os.move_to(cntl.request_attachment());
GetDiscoveryChannel()->CallMethod(NULL, &cntl, NULL, NULL, NULL); GetDiscoveryChannel()->CallMethod(NULL, &cntl, NULL, NULL, NULL);
if (cntl.Failed()) { if (cntl.Failed()) {
LOG(ERROR) << "Fail to register " << _appid << ": " << cntl.ErrorText(); LOG(ERROR) << "Fail to register " << _params.appid << ": " << cntl.ErrorText();
return -1; return -1;
} }
std::string error_text; std::string error_text;
if (ParseCommonResult(cntl.response_attachment(), &error_text) != 0) { if (ParseCommonResult(cntl.response_attachment(), &error_text) != 0) {
LOG(ERROR) << "Fail to register " << _hostname << " to " << _appid LOG(ERROR) << "Fail to register " << _params.hostname << " to " << _params.appid
<< ": " << error_text; << ": " << error_text;
return -1; return -1;
} }
...@@ -283,11 +275,11 @@ int DiscoveryClient::DoCancel() const { ...@@ -283,11 +275,11 @@ int DiscoveryClient::DoCancel() const {
cntl.http_request().uri() = "/discovery/cancel"; cntl.http_request().uri() = "/discovery/cancel";
cntl.http_request().set_content_type("application/x-www-form-urlencoded"); cntl.http_request().set_content_type("application/x-www-form-urlencoded");
butil::IOBufBuilder os; butil::IOBufBuilder os;
os << "appid=" << _appid os << "appid=" << _params.appid
<< "&hostname=" << _hostname << "&hostname=" << _params.hostname
<< "&env=" << _env << "&env=" << _params.env
<< "&region=" << _region << "&region=" << _params.region
<< "&zone=" << _zone; << "&zone=" << _params.zone;
os.move_to(cntl.request_attachment()); os.move_to(cntl.request_attachment());
GetDiscoveryChannel()->CallMethod(NULL, &cntl, NULL, NULL, NULL); GetDiscoveryChannel()->CallMethod(NULL, &cntl, NULL, NULL, NULL);
if (cntl.Failed()) { if (cntl.Failed()) {
...@@ -296,7 +288,7 @@ int DiscoveryClient::DoCancel() const { ...@@ -296,7 +288,7 @@ int DiscoveryClient::DoCancel() const {
} }
std::string error_text; std::string error_text;
if (ParseCommonResult(cntl.response_attachment(), &error_text) != 0) { if (ParseCommonResult(cntl.response_attachment(), &error_text) != 0) {
LOG(ERROR) << "Fail to cancel " << _hostname << " in " << _appid LOG(ERROR) << "Fail to cancel " << _params.hostname << " in " << _params.appid
<< ": " << error_text; << ": " << error_text;
return -1; return -1;
} }
......
...@@ -61,15 +61,7 @@ private: ...@@ -61,15 +61,7 @@ private:
private: private:
bthread_t _th; bthread_t _th;
butil::atomic<bool> _registered; butil::atomic<bool> _registered;
std::string _appid; DiscoveryRegisterParam _params;
std::string _hostname;
std::string _addrs;
std::string _env;
std::string _region;
std::string _zone;
int _status;
std::string _version;
std::string _metadata;
}; };
class DiscoveryNamingService : public PeriodicNamingService { class DiscoveryNamingService : public PeriodicNamingService {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment