|
| 1 | +// Copyright 2021 SkyAPM |
| 2 | + |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | + |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "cds_impl.h" |
| 16 | + |
| 17 | +#include <condition_variable> |
| 18 | + |
| 19 | +#include "spdlog/spdlog.h" |
| 20 | + |
| 21 | +namespace cpp2sky { |
| 22 | + |
| 23 | +using namespace spdlog; |
| 24 | + |
| 25 | +GrpcAsyncConfigDiscoveryServiceClient::GrpcAsyncConfigDiscoveryServiceClient( |
| 26 | + const std::string& address, grpc::CompletionQueue& cq, |
| 27 | + UnaryStreamBuilderPtr<CdsRequest, CdsResponse> builder, |
| 28 | + std::shared_ptr<grpc::ChannelCredentials> cred) |
| 29 | + : builder_(std::move(builder)), |
| 30 | + cq_(cq), |
| 31 | + stub_(grpc::CreateChannel(address, cred)) {} |
| 32 | + |
| 33 | +GrpcAsyncConfigDiscoveryServiceClient:: |
| 34 | + ~GrpcAsyncConfigDiscoveryServiceClient() { |
| 35 | + resetStream(); |
| 36 | +} |
| 37 | + |
| 38 | +void GrpcAsyncConfigDiscoveryServiceClient::sendMessage(CdsRequest request) { |
| 39 | + resetStream(); |
| 40 | + stream_ = builder_->create(*this, request); |
| 41 | + info("[CDS] Stream {} had created", fmt::ptr(stream_.get())); |
| 42 | +} |
| 43 | + |
| 44 | +void GrpcAsyncConfigDiscoveryServiceClient::resetStream() { |
| 45 | + if (stream_) { |
| 46 | + info("[CDS] Stream {} has destroyed", fmt::ptr(this)); |
| 47 | + stream_.reset(); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +GrpcAsyncConfigDiscoveryServiceStream::GrpcAsyncConfigDiscoveryServiceStream( |
| 52 | + AsyncClient<CdsRequest, CdsResponse>& parent, CdsRequest request, |
| 53 | + DynamicConfig& config) |
| 54 | + : client_(parent), config_(config) { |
| 55 | + sendMessage(request); |
| 56 | +} |
| 57 | + |
| 58 | +void GrpcAsyncConfigDiscoveryServiceStream::sendMessage(CdsRequest request) { |
| 59 | + response_reader_ = client_.stub().PrepareUnaryCall( |
| 60 | + &ctx_, "/skywalking.v3.ConfigurationDiscoveryService/fetchConfigurations", |
| 61 | + request, &client_.completionQueue()); |
| 62 | + response_reader_->StartCall(); |
| 63 | + response_reader_->Finish(&commands_, &status_, |
| 64 | + reinterpret_cast<void*>(&read_done_)); |
| 65 | +} |
| 66 | + |
| 67 | +void GrpcAsyncConfigDiscoveryServiceStream::onReadDone() { |
| 68 | + info("[CDS] Stream {} read finished with gRPC status {}", fmt::ptr(this), |
| 69 | + static_cast<int>(status_.error_code())); |
| 70 | + |
| 71 | + if (status_.ok()) { |
| 72 | + config_.onConfigChange(commands_); |
| 73 | + } |
| 74 | + |
| 75 | + // Stream which finished to read done won't be destroyed here. |
| 76 | + // But it will be destroyed when new stream created. |
| 77 | +} |
| 78 | + |
| 79 | +AsyncStreamPtr<CdsRequest, CdsResponse> |
| 80 | +GrpcAsyncConfigDiscoveryServiceStreamBuilder::create( |
| 81 | + AsyncClient<CdsRequest, CdsResponse>& client, CdsRequest request) { |
| 82 | + return std::make_shared<GrpcAsyncConfigDiscoveryServiceStream>( |
| 83 | + client, request, config_); |
| 84 | +} |
| 85 | + |
| 86 | +} // namespace cpp2sky |
0 commit comments