Avoid duplicate const string tags. (#62)

On behalf of Asplund, Rickard, thanks!

Signed-off-by: Tao He <sighingnow@gmail.com>
This commit is contained in:
Tao He 2021-05-28 23:22:32 +08:00 committed by GitHub
parent 46f36dac6d
commit 230ebfd08b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 45 additions and 22 deletions

View File

@ -13,6 +13,17 @@ namespace etcdv3
extern char const * LOCK_ACTION;
extern char const * UNLOCK_ACTION;
extern char const * TXN_ACTION;
extern char const * NUL;
extern char const * KEEPALIVE_CREATE;
extern char const * KEEPALIVE_WRITE;
extern char const * KEEPALIVE_READ;
extern char const * KEEPALIVE_DONE;
extern char const * WATCH_CREATE;
extern char const * WATCH_WRITE;
extern char const * WATCH_WRITES_DONE;
}
#endif

View File

@ -1,4 +1,5 @@
#include <grpc/support/log.h>
#include "etcd/v3/action_constants.hpp"
#include "etcd/v3/Action.hpp"
etcdv3::Action::Action(etcdv3::ActionParameters params)
@ -50,7 +51,5 @@ std::string etcdv3::detail::string_plus_one(std::string const &value) {
return ret;
}
}
// see: noPrefixEnd in etcd, however c++ doesn't allows '\0' inside a string, thus we use
// the UTF-8 char U+0000 (i.e., "\xC0\x80").
return {"\xC0\x80"};
return {etcdv3::NUL};
}

View File

@ -12,7 +12,7 @@ etcdv3::AsyncDeleteAction::AsyncDeleteAction(ActionParameters param)
if(parameters.withPrefix)
{
if (parameters.key.empty()) {
del_request.set_range_end(detail::string_plus_one("\0"));
del_request.set_range_end(detail::string_plus_one(etcdv3::NUL));
} else {
del_request.set_range_end(detail::string_plus_one(parameters.key));
}

View File

@ -11,7 +11,7 @@ etcdv3::AsyncGetAction::AsyncGetAction(etcdv3::ActionParameters param)
{
RangeRequest get_request;
if (parameters.key.empty()) {
get_request.set_key("\0");
get_request.set_key(etcdv3::NUL);
} else {
get_request.set_key(parameters.key);
}
@ -19,7 +19,7 @@ etcdv3::AsyncGetAction::AsyncGetAction(etcdv3::ActionParameters param)
if(parameters.withPrefix)
{
if (parameters.key.empty()) {
get_request.set_range_end(detail::string_plus_one("\0"));
get_request.set_range_end(detail::string_plus_one(etcdv3::NUL));
} else {
get_request.set_range_end(detail::string_plus_one(parameters.key));
}

View File

@ -59,11 +59,11 @@ etcdv3::AsyncLeaseKeepAliveAction::AsyncLeaseKeepAliveAction(etcdv3::ActionParam
: etcdv3::Action(param)
{
isCancelled = false;
stream = parameters.lease_stub->AsyncLeaseKeepAlive(&context, &cq_, (void*)"keepalive create");
stream = parameters.lease_stub->AsyncLeaseKeepAlive(&context, &cq_, (void*)etcdv3::KEEPALIVE_CREATE);
void *got_tag = nullptr;
bool ok = false;
if (cq_.Next(&got_tag, &ok) && ok && got_tag == (void *)"keepalive create") {
if (cq_.Next(&got_tag, &ok) && ok && got_tag == (void *)etcdv3::KEEPALIVE_CREATE) {
// ok
} else {
throw std::runtime_error("Failed to create a lease keep-alive connection");
@ -100,12 +100,12 @@ etcd::Response etcdv3::AsyncLeaseKeepAliveAction::Refresh()
void *got_tag = nullptr;
bool ok = false;
stream->Write(leasekeepalive_request, (void *)"keepalive write");
stream->Write(leasekeepalive_request, (void *)etcdv3::KEEPALIVE_WRITE);
// wait write finish
if (cq_.Next(&got_tag, &ok) && ok && got_tag == (void *)"keepalive write") {
stream->Read(&reply, (void*)"keepalive read");
if (cq_.Next(&got_tag, &ok) && ok && got_tag == (void *)etcdv3::KEEPALIVE_WRITE) {
stream->Read(&reply, (void*)etcdv3::KEEPALIVE_READ);
// wait read finish
if (cq_.Next(&got_tag, &ok) && ok && got_tag == (void *)"keepalive read") {
if (cq_.Next(&got_tag, &ok) && ok && got_tag == (void *)etcdv3::KEEPALIVE_READ) {
auto resp = ParseResponse();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::high_resolution_clock::now() - start_timepoint);
@ -121,7 +121,7 @@ void etcdv3::AsyncLeaseKeepAliveAction::CancelKeepAlive()
if(isCancelled == false)
{
isCancelled = true;
stream->WritesDone((void*)"keepalive done");
stream->WritesDone((void*)etcdv3::KEEPALIVE_DONE);
grpc::Status status;
stream->Finish(&status, (void *)this);
cq_.Shutdown();

View File

@ -10,7 +10,7 @@ etcdv3::AsyncWatchAction::AsyncWatchAction(etcdv3::ActionParameters param)
: etcdv3::Action(param)
{
isCancelled.store(false);
stream = parameters.watch_stub->AsyncWatch(&context,&cq_,(void*)"create");
stream = parameters.watch_stub->AsyncWatch(&context,&cq_,(void*)etcdv3::WATCH_CREATE);
WatchRequest watch_req;
WatchCreateRequest watch_create_req;
@ -21,7 +21,7 @@ etcdv3::AsyncWatchAction::AsyncWatchAction(etcdv3::ActionParameters param)
if(parameters.withPrefix)
{
if (parameters.key.empty()) {
watch_create_req.set_range_end(detail::string_plus_one("\0"));
watch_create_req.set_range_end(detail::string_plus_one(etcdv3::NUL));
} else {
watch_create_req.set_range_end(detail::string_plus_one(parameters.key));
}
@ -35,14 +35,14 @@ etcdv3::AsyncWatchAction::AsyncWatchAction(etcdv3::ActionParameters param)
// wait "create" success (the stream becomes ready)
void *got_tag;
bool ok = false;
if (cq_.Next(&got_tag, &ok) && ok && got_tag == (void *)"create") {
stream->Write(watch_req, (void *)"write");
if (cq_.Next(&got_tag, &ok) && ok && got_tag == (void *)etcdv3::WATCH_CREATE) {
stream->Write(watch_req, (void *)etcdv3::WATCH_WRITE);
} else {
throw std::runtime_error("failed to create a watch connection");
}
// wait "write" (WatchCreateRequest) success, and start to read the first reply
if (cq_.Next(&got_tag, &ok) && ok && got_tag == (void *)"write") {
if (cq_.Next(&got_tag, &ok) && ok && got_tag == (void *)etcdv3::WATCH_WRITE) {
stream->Read(&reply, (void*)this);
} else {
throw std::runtime_error("failed to write WatchCreateRequest to server");
@ -60,7 +60,7 @@ void etcdv3::AsyncWatchAction::waitForResponse()
{
break;
}
if(got_tag == (void*)"writes done") {
if(got_tag == (void*)etcdv3::WATCH_WRITES_DONE) {
isCancelled.store(true);
cq_.Shutdown();
break;
@ -78,7 +78,7 @@ void etcdv3::AsyncWatchAction::waitForResponse()
// 1. watch for a future revision, return immediately with empty events set
// 2. receive any effective events.
isCancelled.store(true);
stream->WritesDone((void*)"writes done");
stream->WritesDone((void*)etcdv3::WATCH_WRITES_DONE);
grpc::Status status;
stream->Finish(&status, (void *)this);
cq_.Shutdown();
@ -101,7 +101,7 @@ void etcdv3::AsyncWatchAction::CancelWatch()
{
std::lock_guard<std::mutex> scope_lock(this->protect_is_cancalled);
if (!isCancelled.exchange(true)) {
stream->WritesDone((void*)"writes done");
stream->WritesDone((void*)etcdv3::WATCH_WRITES_DONE);
grpc::Status status;
stream->Finish(&status, (void *)this);
cq_.Shutdown();
@ -123,7 +123,7 @@ void etcdv3::AsyncWatchAction::waitForResponse(std::function<void(etcd::Response
{
break;
}
if(got_tag == (void*)"writes done")
if(got_tag == (void*)etcdv3::WATCH_WRITES_DONE)
{
isCancelled.store(true);
cq_.Shutdown();

View File

@ -10,3 +10,16 @@ char const * etcdv3::COMPAREDELETE_ACTION = "compareAndDelete";
char const * etcdv3::LOCK_ACTION = "lock";
char const * etcdv3::UNLOCK_ACTION = "unlock";
char const * etcdv3::TXN_ACTION = "txn";
// see: noPrefixEnd in etcd, however c++ doesn't allows '\0' inside a string, thus we use
// the UTF-8 char U+0000 (i.e., "\xC0\x80").
char const * etcdv3::NUL = "\xC0\x80";
char const * etcdv3::KEEPALIVE_CREATE = "keepalive create";
char const * etcdv3::KEEPALIVE_WRITE = "keepalive write";
char const * etcdv3::KEEPALIVE_READ = "keepalive read";
char const * etcdv3::KEEPALIVE_DONE = "keepalive done";
char const * etcdv3::WATCH_CREATE = "watch create";
char const * etcdv3::WATCH_WRITE = "watch write";
char const * etcdv3::WATCH_WRITES_DONE = "watch writes done";