mirror of https://github.com/grpc/grpc.git
Add a class for generating readable connection id for binder (#27771)
* Add a class for generating readable connection id for binder This class will be used to generate a connection IDs that are used to identify binder transport connections. For now we have not migrated to client channel yet so we simply use this new class to generate a connection id for the only connection we support. * Regenerate projects.pull/27818/head
parent
6d2a641dd3
commit
57af085d87
6 changed files with 160 additions and 4 deletions
@ -0,0 +1,67 @@ |
||||
// Copyright 2021 gRPC authors.
|
||||
//
|
||||
// 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.
|
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/ext/transport/binder/client/connection_id_generator.h" |
||||
|
||||
#include "absl/strings/str_cat.h" |
||||
|
||||
namespace { |
||||
// Make sure `s` does not contain characters other than numbers, alphabets,
|
||||
// period and underscore
|
||||
std::string Normalize(absl::string_view str_view) { |
||||
std::string s = std::string(str_view); |
||||
for (size_t i = 0; i < s.length(); i++) { |
||||
if (!isalnum(s[i]) && s[i] != '.') { |
||||
s[i] = '_'; |
||||
} |
||||
} |
||||
return s; |
||||
} |
||||
|
||||
// Remove prefix of the string if the string is longer than len
|
||||
std::string StripToLength(const std::string& s, size_t len) { |
||||
if (s.length() > len) { |
||||
return s.substr(s.length() - len, len); |
||||
} |
||||
return s; |
||||
} |
||||
} // namespace
|
||||
|
||||
namespace grpc_binder { |
||||
|
||||
std::string ConnectionIdGenerator::Generate(absl::string_view package_name, |
||||
absl::string_view class_name) { |
||||
// reserve some room for serial number
|
||||
const size_t kReserveForNumbers = 15; |
||||
std::string s = StripToLength( |
||||
absl::StrCat(Normalize(package_name), "-", Normalize(class_name)), |
||||
kPathLengthLimit - kReserveForNumbers); |
||||
std::string ret; |
||||
{ |
||||
grpc_core::MutexLock l(&m_); |
||||
// Insert a hyphen before serial number
|
||||
ret = absl::StrCat(s, "-", ++count_); |
||||
} |
||||
GPR_ASSERT(ret.length() < kPathLengthLimit); |
||||
return ret; |
||||
} |
||||
|
||||
ConnectionIdGenerator* GetConnectionIdGenerator() { |
||||
static ConnectionIdGenerator* cig = new ConnectionIdGenerator(); |
||||
return cig; |
||||
} |
||||
|
||||
} // namespace grpc_binder
|
@ -0,0 +1,52 @@ |
||||
// Copyright 2021 gRPC authors.
|
||||
//
|
||||
// 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 GRPC_CORE_EXT_TRANSPORT_BINDER_CLIENT_CONNECTION_ID_GENERATOR_H |
||||
#define GRPC_CORE_EXT_TRANSPORT_BINDER_CLIENT_CONNECTION_ID_GENERATOR_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include <map> |
||||
|
||||
#include "absl/strings/string_view.h" |
||||
|
||||
#include "src/core/lib/gprpp/sync.h" |
||||
|
||||
namespace grpc_binder { |
||||
|
||||
// Generates somewhat human-readable unique identifiers from package name and
|
||||
// class name. We will generate a Id that only contains unreserved URI
|
||||
// characters (uppercase and lowercase letters, decimal digits, hyphen, period,
|
||||
// underscore, and tilde).
|
||||
class ConnectionIdGenerator { |
||||
public: |
||||
std::string Generate(absl::string_view package_name, |
||||
absl::string_view class_name); |
||||
|
||||
private: |
||||
// Our generated Id need to fit in unix socket path length limit. We use 100
|
||||
// here to be safe.
|
||||
const size_t kPathLengthLimit = 100; |
||||
|
||||
grpc_core::Mutex m_; |
||||
// Every generated identifier will followed by the value of this counter to
|
||||
// make sure every generated id is unique.
|
||||
int count_ ABSL_GUARDED_BY(m_); |
||||
}; |
||||
|
||||
ConnectionIdGenerator* GetConnectionIdGenerator(); |
||||
|
||||
} // namespace grpc_binder
|
||||
|
||||
#endif // GRPC_CORE_EXT_TRANSPORT_BINDER_CLIENT_CONNECTION_ID_GENERATOR_H
|
Loading…
Reference in new issue