Skip to content

Commit

Permalink
use uuid for deduplication_id (envoyproxy#85)
Browse files Browse the repository at this point in the history
* use uuid for deduplication_id

* Only call uuid once.
  • Loading branch information
qiwzhang committed Jun 30, 2017
1 parent d6da245 commit ef6dd6f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions mixerclient/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ cc_test(
"-lm",
"-lpthread",
"-lrt",
"-luuid",
],
linkstatic = 1,
deps = [
Expand Down
24 changes: 22 additions & 2 deletions mixerclient/src/client_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "src/client_impl.h"
#include "utils/protobuf.h"

#include <uuid/uuid.h>

using namespace std::chrono;
using ::istio::mixer::v1::CheckRequest;
using ::istio::mixer::v1::CheckResponse;
Expand All @@ -25,16 +27,33 @@ using ::google::protobuf::util::error::Code;

namespace istio {
namespace mixer_client {
namespace {

// Maximum 36 byte string for UUID
const int kMaxUUIDBufSize = 40;

// Genereates a UUID string
std::string GenerateUUID() {
char uuid_buf[kMaxUUIDBufSize];
uuid_t uuid;
uuid_generate(uuid);
uuid_unparse(uuid, uuid_buf);
return uuid_buf;
}

} // namespace

MixerClientImpl::MixerClientImpl(const MixerClientOptions &options)
: options_(options), deduplication_id_(0) {
: options_(options) {
check_cache_ =
std::unique_ptr<CheckCache>(new CheckCache(options.check_options));
report_batch_ = std::unique_ptr<ReportBatch>(
new ReportBatch(options.report_options, options_.report_transport,
options.timer_create_func, converter_));
quota_cache_ =
std::unique_ptr<QuotaCache>(new QuotaCache(options.quota_options));

deduplication_id_base_ = GenerateUUID();
}

MixerClientImpl::~MixerClientImpl() {}
Expand Down Expand Up @@ -68,7 +87,8 @@ void MixerClientImpl::Check(const Attributes &attributes, DoneFunc on_done) {

converter_.Convert(attributes, request.mutable_attributes());
request.set_global_word_count(converter_.global_word_count());
request.set_deduplication_id(std::to_string(deduplication_id_++));
request.set_deduplication_id(deduplication_id_base_ +
std::to_string(deduplication_id_.fetch_add(1)));

auto response = new CheckResponse;
// Lambda capture could not pass unique_ptr, use raw pointer.
Expand Down
7 changes: 5 additions & 2 deletions mixerclient/src/client_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "src/quota_cache.h"
#include "src/report_batch.h"

#include <atomic>

namespace istio {
namespace mixer_client {

Expand Down Expand Up @@ -50,8 +52,9 @@ class MixerClientImpl : public MixerClient {
// Cache for Quota call.
std::unique_ptr<QuotaCache> quota_cache_;

// For quota deduplication
int64_t deduplication_id_;
// for deduplication_id
std::string deduplication_id_base_;
std::atomic<std::uint64_t> deduplication_id_;

GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MixerClientImpl);
};
Expand Down

0 comments on commit ef6dd6f

Please sign in to comment.