Skip to content

Commit d8b7b81

Browse files
authored
refactor the grpc client to fix #116 (#132)
1 parent c96eb92 commit d8b7b81

20 files changed

+621
-712
lines changed

.github/workflows/main.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ jobs:
2727
- uses: actions/checkout@v3
2828
- name: Run bazel test with GCC c++11
2929
run: |
30-
bazel test --cxxopt=-std=c++0x //...
30+
bazel test --test_output=all --cxxopt=-std=c++0x //...
3131
- name: Run bazel test with GCC c++17
3232
run: |
33-
bazel test --cxxopt=-std=c++17 //...
33+
bazel test --test_output=all --cxxopt=-std=c++17 //...
3434
- name: Run bazel test with CLANG c++11
3535
run: |
36-
bazel test --config=clang --cxxopt=-std=c++0x //...
36+
bazel test --test_output=all -c dbg --config=clang --cxxopt=-std=c++0x //...
3737
- name: Run bazel test with CLANG c++17
3838
run: |
39-
bazel test --config=clang --cxxopt=-std=c++17 //...
39+
bazel test --test_output=all -c opt --config=clang --cxxopt=-std=c++17 //...
4040
- name: Install cmake dependencies and run cmake compile
4141
run: |
4242
sudo apt update

BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ refresh_compile_commands(
99
"//cpp2sky/...": "",
1010
"//source/...": "",
1111
"//test/...": "",
12+
"//example/...": "",
1213
},
1314
)

cpp2sky/internal/BUILD

-6
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ cc_library(
2323
visibility = ["//visibility:public"],
2424
)
2525

26-
cc_library(
27-
name = "stream_builder_interface",
28-
hdrs = ["stream_builder.h"],
29-
visibility = ["//visibility:public"],
30-
)
31-
3226
cc_library(
3327
name = "matcher_interface",
3428
hdrs = ["matcher.h"],

cpp2sky/internal/async_client.h

+56-85
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@
1414

1515
#pragma once
1616

17-
#include <google/protobuf/message.h>
18-
#include <grpcpp/generic/generic_stub.h>
19-
#include <grpcpp/grpcpp.h>
20-
17+
#include <functional>
2118
#include <memory>
2219

23-
#include "source/utils/circular_buffer.h"
24-
25-
using google::protobuf::Message;
20+
#include "google/protobuf/message.h"
21+
#include "grpcpp/generic/generic_stub.h"
22+
#include "grpcpp/grpcpp.h"
23+
#include "language-agent/Tracing.pb.h"
2624

2725
namespace cpp2sky {
2826

27+
/**
28+
* Template base class for gRPC async client.
29+
*/
2930
template <class RequestType, class ResponseType>
3031
class AsyncClient {
3132
public:
@@ -37,108 +38,78 @@ class AsyncClient {
3738
virtual void sendMessage(RequestType message) = 0;
3839

3940
/**
40-
* Pending message queue reference.
41-
*/
42-
virtual CircularBuffer<RequestType>& pendingMessages() = 0;
43-
44-
/**
45-
* Start stream if there is no living stream.
46-
*/
47-
virtual void startStream() = 0;
48-
49-
/**
50-
* Completion queue.
41+
* Reset the client. This should be called when the client is no longer
42+
* needed.
5143
*/
52-
virtual grpc::CompletionQueue& completionQueue() = 0;
53-
54-
/**
55-
* gRPC Stub
56-
*/
57-
virtual grpc::TemplatedGenericStub<RequestType, ResponseType>& stub() = 0;
44+
virtual void resetClient() = 0;
5845
};
5946

6047
template <class RequestType, class ResponseType>
6148
using AsyncClientPtr = std::unique_ptr<AsyncClient<RequestType, ResponseType>>;
6249

50+
/**
51+
* Template base class for gRPC async stream. The stream is used to represent
52+
* a single gRPC stream/request.
53+
*/
6354
template <class RequestType, class ResponseType>
6455
class AsyncStream {
6556
public:
6657
virtual ~AsyncStream() = default;
6758

6859
/**
69-
* Send message. It will move the state from Init to Write.
60+
* Send the specified protobuf message.
7061
*/
7162
virtual void sendMessage(RequestType message) = 0;
7263
};
7364

74-
enum class StreamState : uint8_t {
75-
Initialized = 0,
76-
Ready = 1,
77-
Idle = 2,
78-
WriteDone = 3,
79-
ReadDone = 4,
65+
template <class RequestType, class ResponseType>
66+
using AsyncStreamPtr = std::unique_ptr<AsyncStream<RequestType, ResponseType>>;
67+
68+
/**
69+
* Tag for async operation. The callback should be called when the operation is
70+
* done.
71+
*/
72+
struct AsyncEventTag {
73+
std::function<void(bool)> callback;
8074
};
75+
using AsyncEventTagPtr = std::unique_ptr<AsyncEventTag>;
76+
77+
using GrpcClientContextPtr = std::unique_ptr<grpc::ClientContext>;
78+
using GrpcCompletionQueue = grpc::CompletionQueue;
8179

82-
class AsyncStreamCallback {
80+
/**
81+
* Factory for creating async stream.
82+
*/
83+
template <class RequestType, class ResponseType>
84+
class AsyncStreamFactory {
8385
public:
84-
/**
85-
* Callback when stream ready event occured.
86-
*/
87-
virtual void onReady() = 0;
86+
virtual ~AsyncStreamFactory() = default;
8887

89-
/**
90-
* Callback when idle event occured.
91-
*/
92-
virtual void onIdle() = 0;
88+
using StreamPtr = AsyncStreamPtr<RequestType, ResponseType>;
89+
using GrpcStub = grpc::TemplatedGenericStub<RequestType, ResponseType>;
9390

94-
/**
95-
* Callback when write done event occured.
96-
*/
97-
virtual void onWriteDone() = 0;
91+
virtual StreamPtr createStream(GrpcClientContextPtr client_ctx,
92+
GrpcStub& stub, GrpcCompletionQueue& cq,
93+
AsyncEventTag& basic_event_tag,
94+
AsyncEventTag& write_event_tag) = 0;
95+
};
9896

99-
/**
100-
* Callback when read done event occured.
101-
*/
102-
virtual void onReadDone() = 0;
97+
template <class RequestType, class ResponseType>
98+
using AsyncStreamFactoryPtr =
99+
std::unique_ptr<AsyncStreamFactory<RequestType, ResponseType>>;
103100

104-
/**
105-
* Callback when stream had finished with arbitrary error.
106-
*/
107-
virtual void onStreamFinish() = 0;
108-
};
101+
using TraceRequestType = skywalking::v3::SegmentObject;
102+
using TraceResponseType = skywalking::v3::Commands;
109103

110-
struct StreamCallbackTag {
111-
public:
112-
void callback(bool stream_finished) {
113-
if (stream_finished) {
114-
callback_->onStreamFinish();
115-
return;
116-
}
117-
118-
switch (state_) {
119-
case StreamState::Ready:
120-
callback_->onReady();
121-
break;
122-
case StreamState::WriteDone:
123-
callback_->onWriteDone();
124-
break;
125-
case StreamState::Idle:
126-
callback_->onIdle();
127-
break;
128-
case StreamState::ReadDone:
129-
callback_->onReadDone();
130-
break;
131-
default:
132-
break;
133-
}
134-
}
135-
136-
StreamState state_;
137-
AsyncStreamCallback* callback_;
138-
};
104+
using TraceAsyncStream = AsyncStream<TraceRequestType, TraceResponseType>;
105+
using TraceAsyncStreamPtr = AsyncStreamPtr<TraceRequestType, TraceResponseType>;
139106

140-
template <class RequestType, class ResponseType>
141-
using AsyncStreamSharedPtr =
142-
std::shared_ptr<AsyncStream<RequestType, ResponseType>>;
107+
using TraceAsyncStreamFactory =
108+
AsyncStreamFactory<TraceRequestType, TraceResponseType>;
109+
using TraceAsyncStreamFactoryPtr =
110+
AsyncStreamFactoryPtr<TraceRequestType, TraceResponseType>;
111+
112+
using TraceAsyncClient = AsyncClient<TraceRequestType, TraceResponseType>;
113+
using TraceAsyncClientPtr = std::unique_ptr<TraceAsyncClient>;
143114

144115
} // namespace cpp2sky

cpp2sky/internal/stream_builder.h

-56
This file was deleted.

cpp2sky/tracer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ class Tracer {
4040

4141
using TracerPtr = std::unique_ptr<Tracer>;
4242

43-
TracerPtr createInsecureGrpcTracer(TracerConfig& cfg);
43+
TracerPtr createInsecureGrpcTracer(const TracerConfig& cfg);
4444

4545
} // namespace cpp2sky

source/BUILD

+3-5
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,20 @@ cc_library(
44
name = "cpp2sky_lib",
55
srcs = [
66
"grpc_async_client_impl.cc",
7-
"propagation_impl.cc",
87
"tracer_impl.cc",
9-
"tracing_context_impl.cc",
108
],
119
hdrs = [
1210
"grpc_async_client_impl.h",
13-
"propagation_impl.h",
1411
"tracer_impl.h",
15-
"tracing_context_impl.h",
1612
],
1713
visibility = ["//visibility:public"],
1814
deps = [
15+
":cpp2sky_data_lib",
16+
"//cpp2sky:config_cc_proto",
17+
"//cpp2sky:cpp2sky_data_interface",
1918
"//cpp2sky:cpp2sky_interface",
2019
"//cpp2sky/internal:async_client_interface",
2120
"//cpp2sky/internal:matcher_interface",
22-
"//cpp2sky/internal:stream_builder_interface",
2321
"//source/matchers:suffix_matcher_lib",
2422
"//source/utils:util_lib",
2523
"@com_github_gabime_spdlog//:spdlog",

0 commit comments

Comments
 (0)