Skip to content

Commit 038a9bf

Browse files
committed
internal: pass engine handles throughout API (#2149)
In preparation for removing the singleton. This is split off into its own independent change to minimize the scope of the main singleton removal work. We were already doing this in many places, this just adds more instances of passing the engine handle so we do so comprehensively. Description: Pass engine handles throughout API Risk Level: Medium, changing some JNI interfaces, removing some static state Testing: Unit tests and sample apps Docs Changes: N/A internal only Release Notes: N/A internal only [Optional Fixes #Issue] [Optional Deprecated:] Signed-off-by: JP Simard [email protected] Signed-off-by: JP Simard <[email protected]>
1 parent 57fec25 commit 038a9bf

28 files changed

+354
-249
lines changed

mobile/library/cc/stream.cc

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,30 @@
77
namespace Envoy {
88
namespace Platform {
99

10-
Stream::Stream(envoy_stream_t handle) : handle_(handle) {}
10+
Stream::Stream(envoy_engine_t engine_handle, envoy_stream_t handle)
11+
: engine_handle_(engine_handle), handle_(handle) {}
1112

1213
Stream& Stream::sendHeaders(RequestHeadersSharedPtr headers, bool end_stream) {
1314
envoy_headers raw_headers = rawHeaderMapAsEnvoyHeaders(headers->allHeaders());
14-
::send_headers(this->handle_, raw_headers, end_stream);
15+
::send_headers(this->engine_handle_, this->handle_, raw_headers, end_stream);
1516
return *this;
1617
}
1718

1819
Stream& Stream::sendData(envoy_data data) {
19-
::send_data(this->handle_, data, false);
20+
::send_data(this->engine_handle_, this->handle_, data, false);
2021
return *this;
2122
}
2223

2324
void Stream::close(RequestTrailersSharedPtr trailers) {
2425
envoy_headers raw_headers = rawHeaderMapAsEnvoyHeaders(trailers->allHeaders());
25-
::send_trailers(this->handle_, raw_headers);
26+
::send_trailers(this->engine_handle_, this->handle_, raw_headers);
2627
}
2728

28-
void Stream::close(envoy_data data) { ::send_data(this->handle_, data, true); }
29+
void Stream::close(envoy_data data) {
30+
::send_data(this->engine_handle_, this->handle_, data, true);
31+
}
2932

30-
void Stream::cancel() { reset_stream(this->handle_); }
33+
void Stream::cancel() { reset_stream(this->engine_handle_, this->handle_); }
3134

3235
} // namespace Platform
3336
} // namespace Envoy

mobile/library/cc/stream.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ using EngineSharedPtr = std::shared_ptr<Engine>;
1515

1616
class Stream {
1717
public:
18-
Stream(envoy_stream_t handle);
18+
Stream(envoy_engine_t engine_handle, envoy_stream_t handle);
1919

2020
Stream& sendHeaders(RequestHeadersSharedPtr headers, bool end_stream);
2121
Stream& sendData(envoy_data data);
@@ -24,6 +24,7 @@ class Stream {
2424
void cancel();
2525

2626
private:
27+
envoy_engine_t engine_handle_;
2728
envoy_stream_t handle_;
2829
};
2930

mobile/library/cc/stream_prototype.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ StreamPrototype::StreamPrototype(EngineSharedPtr engine) : engine_(engine) {
1111

1212
StreamSharedPtr StreamPrototype::start() {
1313
auto envoy_stream = init_stream(this->engine_->engine_);
14-
start_stream(envoy_stream, this->callbacks_->asEnvoyHttpCallbacks(), false);
15-
return std::make_shared<Stream>(envoy_stream);
14+
start_stream(this->engine_->engine_, envoy_stream, this->callbacks_->asEnvoyHttpCallbacks(),
15+
false);
16+
return std::make_shared<Stream>(this->engine_->engine_, envoy_stream);
1617
}
1718

1819
StreamPrototype& StreamPrototype::setOnHeaders(OnHeadersCallback closure) {

mobile/library/common/jni/android_jni_interface.cc

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,3 @@ Java_io_envoyproxy_envoymobile_engine_AndroidJniLibrary_initialize(JNIEnv* env,
2121

2222
return ares_library_init_android(connectivity_manager);
2323
}
24-
25-
extern "C" JNIEXPORT jint JNICALL
26-
Java_io_envoyproxy_envoymobile_engine_AndroidJniLibrary_setPreferredNetwork(JNIEnv* env,
27-
jclass, // class
28-
jint network) {
29-
jni_log("[Envoy]", "setting preferred network");
30-
return set_preferred_network(static_cast<envoy_network_t>(network));
31-
}

mobile/library/common/jni/android_test_jni_interface.cc

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,3 @@ Java_io_envoyproxy_envoymobile_engine_AndroidJniLibrary_initialize(JNIEnv* env,
1414

1515
return 0;
1616
}
17-
18-
extern "C" JNIEXPORT jint JNICALL
19-
Java_io_envoyproxy_envoymobile_engine_AndroidJniLibrary_setPreferredNetwork(JNIEnv* env,
20-
jclass, // class
21-
jint network) {
22-
jni_log("[Envoy]", "setting preferred network");
23-
return set_preferred_network(static_cast<envoy_network_t>(network));
24-
}

mobile/library/common/jni/jni_interface.cc

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,8 @@ extern "C" JNIEXPORT jlong JNICALL Java_io_envoyproxy_envoymobile_engine_JniLibr
839839
}
840840

841841
extern "C" JNIEXPORT jint JNICALL Java_io_envoyproxy_envoymobile_engine_JniLibrary_startStream(
842-
JNIEnv* env, jclass, jlong stream_handle, jobject j_context, jboolean explicit_flow_control) {
842+
JNIEnv* env, jclass, jlong engine_handle, jlong stream_handle, jobject j_context,
843+
jboolean explicit_flow_control) {
843844

844845
// TODO: To be truly safe we may need stronger guarantees of operation ordering on this ref.
845846
jobject retained_context = env->NewGlobalRef(j_context);
@@ -852,7 +853,8 @@ extern "C" JNIEXPORT jint JNICALL Java_io_envoyproxy_envoymobile_engine_JniLibra
852853
jvm_on_cancel,
853854
jvm_on_send_window_available,
854855
retained_context};
855-
envoy_status_t result = start_stream(static_cast<envoy_stream_t>(stream_handle), native_callbacks,
856+
envoy_status_t result = start_stream(static_cast<envoy_engine_t>(engine_handle),
857+
static_cast<envoy_stream_t>(stream_handle), native_callbacks,
856858
explicit_flow_control);
857859
if (result != ENVOY_SUCCESS) {
858860
env->DeleteGlobalRef(retained_context); // No callbacks are fired and we need to release
@@ -934,60 +936,62 @@ Java_io_envoyproxy_envoymobile_engine_EnvoyHTTPFilterCallbacksImpl_callReleaseCa
934936
// EnvoyHTTPStream
935937

936938
extern "C" JNIEXPORT jint JNICALL Java_io_envoyproxy_envoymobile_engine_JniLibrary_readData(
937-
JNIEnv* env, jclass, jlong stream_handle, jlong byte_count) {
938-
939-
return read_data(static_cast<envoy_stream_t>(stream_handle), byte_count);
939+
JNIEnv* env, jclass, jlong engine_handle, jlong stream_handle, jlong byte_count) {
940+
return read_data(static_cast<envoy_engine_t>(engine_handle),
941+
static_cast<envoy_stream_t>(stream_handle), byte_count);
940942
}
941943

942-
// Note: JLjava_nio_ByteBuffer_2IZ is the mangled signature of the java method.
943-
// https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/design.html
944944
// The Java counterpart guarantees to invoke this method with a non-null direct ByteBuffer where the
945945
// provided length is between 0 and ByteBuffer.capacity(), inclusively.
946-
extern "C" JNIEXPORT jint JNICALL
947-
Java_io_envoyproxy_envoymobile_engine_JniLibrary_sendData__JLjava_nio_ByteBuffer_2IZ(
948-
JNIEnv* env, jclass, jlong stream_handle, jobject data, jint length, jboolean end_stream) {
946+
extern "C" JNIEXPORT jint JNICALL Java_io_envoyproxy_envoymobile_engine_JniLibrary_sendData(
947+
JNIEnv* env, jclass, jlong engine_handle, jlong stream_handle, jobject data, jint length,
948+
jboolean end_stream) {
949949
if (end_stream) {
950950
jni_log("[Envoy]", "jvm_send_data_end_stream");
951951
}
952-
953-
return send_data(static_cast<envoy_stream_t>(stream_handle),
952+
return send_data(static_cast<envoy_engine_t>(engine_handle),
953+
static_cast<envoy_stream_t>(stream_handle),
954954
buffer_to_native_data(env, data, length), end_stream);
955955
}
956956

957-
// Note: J_3BIZ is the mangled signature of the java method.
958-
// https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/design.html
959957
// The Java counterpart guarantees to invoke this method with a non-null jbyteArray where the
960958
// provided length is between 0 and the size of the jbyteArray, inclusively. And given that this
961959
// jbyteArray comes from a ByteBuffer, it is also guaranteed that its length will not be greater
962960
// than 2^31 - this is why the length type is jint.
963-
extern "C" JNIEXPORT jint JNICALL Java_io_envoyproxy_envoymobile_engine_JniLibrary_sendData__J_3BIZ(
964-
JNIEnv* env, jclass, jlong stream_handle, jbyteArray data, jint length, jboolean end_stream) {
961+
extern "C" JNIEXPORT jint JNICALL
962+
Java_io_envoyproxy_envoymobile_engine_JniLibrary_sendDataByteArray(JNIEnv* env, jclass,
963+
jlong engine_handle,
964+
jlong stream_handle,
965+
jbyteArray data, jint length,
966+
jboolean end_stream) {
965967
if (end_stream) {
966968
jni_log("[Envoy]", "jvm_send_data_end_stream");
967969
}
968-
969-
return send_data(static_cast<envoy_stream_t>(stream_handle),
970+
return send_data(static_cast<envoy_engine_t>(engine_handle),
971+
static_cast<envoy_stream_t>(stream_handle),
970972
array_to_native_data(env, data, length), end_stream);
971973
}
972974

973975
extern "C" JNIEXPORT jint JNICALL Java_io_envoyproxy_envoymobile_engine_JniLibrary_sendHeaders(
974-
JNIEnv* env, jclass, jlong stream_handle, jobjectArray headers, jboolean end_stream) {
975-
976-
return send_headers(static_cast<envoy_stream_t>(stream_handle), to_native_headers(env, headers),
976+
JNIEnv* env, jclass, jlong engine_handle, jlong stream_handle, jobjectArray headers,
977+
jboolean end_stream) {
978+
return send_headers(static_cast<envoy_engine_t>(engine_handle),
979+
static_cast<envoy_stream_t>(stream_handle), to_native_headers(env, headers),
977980
end_stream);
978981
}
979982

980983
extern "C" JNIEXPORT jint JNICALL Java_io_envoyproxy_envoymobile_engine_JniLibrary_sendTrailers(
981-
JNIEnv* env, jclass, jlong stream_handle, jobjectArray trailers) {
984+
JNIEnv* env, jclass, jlong engine_handle, jlong stream_handle, jobjectArray trailers) {
982985
jni_log("[Envoy]", "jvm_send_trailers");
983-
return send_trailers(static_cast<envoy_stream_t>(stream_handle),
986+
return send_trailers(static_cast<envoy_engine_t>(engine_handle),
987+
static_cast<envoy_stream_t>(stream_handle),
984988
to_native_headers(env, trailers));
985989
}
986990

987991
extern "C" JNIEXPORT jint JNICALL Java_io_envoyproxy_envoymobile_engine_JniLibrary_resetStream(
988-
JNIEnv* env, jclass, jlong stream_handle) {
989-
990-
return reset_stream(static_cast<envoy_stream_t>(stream_handle));
992+
JNIEnv* env, jclass, jlong engine_handle, jlong stream_handle) {
993+
return reset_stream(static_cast<envoy_engine_t>(engine_handle),
994+
static_cast<envoy_stream_t>(stream_handle));
991995
}
992996

993997
// EnvoyStringAccessor
@@ -1018,3 +1022,12 @@ Java_io_envoyproxy_envoymobile_engine_JniLibrary_drainConnections(JNIEnv* env,
10181022
jni_log("[Envoy]", "drainConnections");
10191023
drain_connections(engine);
10201024
}
1025+
1026+
extern "C" JNIEXPORT jint JNICALL
1027+
Java_io_envoyproxy_envoymobile_engine_JniLibrary_setPreferredNetwork(JNIEnv* env,
1028+
jclass, // class
1029+
jlong engine, jint network) {
1030+
jni_log("[Envoy]", "setting preferred network");
1031+
return set_preferred_network(static_cast<envoy_engine_t>(engine),
1032+
static_cast<envoy_network_t>(network));
1033+
}

mobile/library/common/main_interface.cc

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,64 +17,65 @@ static std::atomic<envoy_stream_t> current_stream_handle_{0};
1717

1818
envoy_stream_t init_stream(envoy_engine_t) { return current_stream_handle_++; }
1919

20-
envoy_status_t start_stream(envoy_stream_t stream, envoy_http_callbacks callbacks,
21-
bool explicit_flow_control) {
20+
envoy_status_t start_stream(envoy_engine_t engine, envoy_stream_t stream,
21+
envoy_http_callbacks callbacks, bool explicit_flow_control) {
2222
return Envoy::EngineHandle::runOnEngineDispatcher(
23-
1 /* engine */, [stream, callbacks, explicit_flow_control](auto& engine) -> void {
23+
engine, [stream, callbacks, explicit_flow_control](auto& engine) -> void {
2424
engine.httpClient().startStream(stream, callbacks, explicit_flow_control);
2525
});
2626
}
2727

28-
envoy_status_t send_headers(envoy_stream_t stream, envoy_headers headers, bool end_stream) {
28+
envoy_status_t send_headers(envoy_engine_t engine, envoy_stream_t stream, envoy_headers headers,
29+
bool end_stream) {
2930
return Envoy::EngineHandle::runOnEngineDispatcher(
30-
1 /* engine */, ([stream, headers, end_stream](auto& engine) -> void {
31+
engine, ([stream, headers, end_stream](auto& engine) -> void {
3132
engine.httpClient().sendHeaders(stream, headers, end_stream);
3233
}));
3334
}
3435

35-
envoy_status_t read_data(envoy_stream_t stream, size_t bytes_to_read) {
36+
envoy_status_t read_data(envoy_engine_t engine, envoy_stream_t stream, size_t bytes_to_read) {
3637
return Envoy::EngineHandle::runOnEngineDispatcher(
37-
1 /* engine */, [stream, bytes_to_read](auto& engine) -> void {
38+
engine, [stream, bytes_to_read](auto& engine) -> void {
3839
engine.httpClient().readData(stream, bytes_to_read);
3940
});
4041
}
4142

42-
envoy_status_t send_data(envoy_stream_t stream, envoy_data data, bool end_stream) {
43+
envoy_status_t send_data(envoy_engine_t engine, envoy_stream_t stream, envoy_data data,
44+
bool end_stream) {
4345
return Envoy::EngineHandle::runOnEngineDispatcher(
44-
1 /* engine */, [stream, data, end_stream](auto& engine) -> void {
46+
engine, [stream, data, end_stream](auto& engine) -> void {
4547
engine.httpClient().sendData(stream, data, end_stream);
4648
});
4749
}
4850

4951
// TODO: implement.
50-
envoy_status_t send_metadata(envoy_stream_t, envoy_headers) { return ENVOY_FAILURE; }
52+
envoy_status_t send_metadata(envoy_engine_t, envoy_stream_t, envoy_headers) {
53+
return ENVOY_FAILURE;
54+
}
5155

52-
envoy_status_t send_trailers(envoy_stream_t stream, envoy_headers trailers) {
56+
envoy_status_t send_trailers(envoy_engine_t engine, envoy_stream_t stream, envoy_headers trailers) {
5357
return Envoy::EngineHandle::runOnEngineDispatcher(
54-
1 /* engine */, [stream, trailers](auto& engine) -> void {
58+
engine, [stream, trailers](auto& engine) -> void {
5559
engine.httpClient().sendTrailers(stream, trailers);
5660
});
5761
}
5862

59-
envoy_status_t reset_stream(envoy_stream_t stream) {
63+
envoy_status_t reset_stream(envoy_engine_t engine, envoy_stream_t stream) {
6064
return Envoy::EngineHandle::runOnEngineDispatcher(
61-
1 /* engine */, [stream](auto& engine) -> void { engine.httpClient().cancelStream(stream); });
65+
engine, [stream](auto& engine) -> void { engine.httpClient().cancelStream(stream); });
6266
}
6367

64-
envoy_status_t set_preferred_network(envoy_network_t network) {
68+
envoy_status_t set_preferred_network(envoy_engine_t engine, envoy_network_t network) {
6569
envoy_netconf_t configuration_key = Envoy::Network::Configurator::setPreferredNetwork(network);
66-
Envoy::EngineHandle::runOnEngineDispatcher(
67-
1 /* engine */, [configuration_key](auto& engine) -> void {
68-
engine.networkConfigurator().refreshDns(configuration_key);
69-
});
70+
Envoy::EngineHandle::runOnEngineDispatcher(engine, [configuration_key](auto& engine) -> void {
71+
engine.networkConfigurator().refreshDns(configuration_key);
72+
});
7073
// TODO(snowp): Should this return failure ever?
7174
return ENVOY_SUCCESS;
7275
}
7376

7477
envoy_status_t record_counter_inc(envoy_engine_t e, const char* elements, envoy_stats_tags tags,
7578
uint64_t count) {
76-
// TODO: use specific engine once multiple engine support is in place.
77-
// https://github.com/envoyproxy/envoy-mobile/issues/332
7879
return Envoy::EngineHandle::runOnEngineDispatcher(
7980
e, [name = std::string(elements), tags, count](auto& engine) -> void {
8081
engine.recordCounterInc(name, tags, count);
@@ -83,8 +84,6 @@ envoy_status_t record_counter_inc(envoy_engine_t e, const char* elements, envoy_
8384

8485
envoy_status_t record_gauge_set(envoy_engine_t e, const char* elements, envoy_stats_tags tags,
8586
uint64_t value) {
86-
// TODO: use specific engine once multiple engine support is in place.
87-
// https://github.com/envoyproxy/envoy-mobile/issues/332
8887
return Envoy::EngineHandle::runOnEngineDispatcher(
8988
e, [name = std::string(elements), tags, value](auto& engine) -> void {
9089
engine.recordGaugeSet(name, tags, value);
@@ -93,8 +92,6 @@ envoy_status_t record_gauge_set(envoy_engine_t e, const char* elements, envoy_st
9392

9493
envoy_status_t record_gauge_add(envoy_engine_t e, const char* elements, envoy_stats_tags tags,
9594
uint64_t amount) {
96-
// TODO: use specific engine once multiple engine support is in place.
97-
// https://github.com/envoyproxy/envoy-mobile/issues/332
9895
return Envoy::EngineHandle::runOnEngineDispatcher(
9996
e, [name = std::string(elements), tags, amount](auto& engine) -> void {
10097
engine.recordGaugeAdd(name, tags, amount);
@@ -103,8 +100,6 @@ envoy_status_t record_gauge_add(envoy_engine_t e, const char* elements, envoy_st
103100

104101
envoy_status_t record_gauge_sub(envoy_engine_t e, const char* elements, envoy_stats_tags tags,
105102
uint64_t amount) {
106-
// TODO: use specific engine once multiple engine support is in place.
107-
// https://github.com/envoyproxy/envoy-mobile/issues/332
108103
return Envoy::EngineHandle::runOnEngineDispatcher(
109104
e, [name = std::string(elements), tags, amount](auto& engine) -> void {
110105
engine.recordGaugeSub(name, tags, amount);
@@ -113,8 +108,6 @@ envoy_status_t record_gauge_sub(envoy_engine_t e, const char* elements, envoy_st
113108

114109
envoy_status_t record_histogram_value(envoy_engine_t e, const char* elements, envoy_stats_tags tags,
115110
uint64_t value, envoy_histogram_stat_unit_t unit_measure) {
116-
// TODO: use specific engine once multiple engine support is in place.
117-
// https://github.com/envoyproxy/envoy-mobile/issues/332
118111
return Envoy::EngineHandle::runOnEngineDispatcher(
119112
e, [name = std::string(elements), tags, value, unit_measure](auto& engine) -> void {
120113
engine.recordHistogramValue(name, tags, value, unit_measure);

0 commit comments

Comments
 (0)