Skip to content

Commit a96408d

Browse files
authored
Support raw/non-protobuf gRPC. (proxy-wasm#20)
Signed-off-by: John Plevyak <[email protected]>
1 parent 96927d8 commit a96408d

File tree

1 file changed

+66
-26
lines changed

1 file changed

+66
-26
lines changed

proxy_wasm_api.h

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ class GrpcStreamHandlerBase {
224224

225225
// NB: with end_of_stream == true, callbacks can still occur: reset() to
226226
// prevent further callbacks.
227-
void send(StringView message, bool end_of_stream);
227+
WasmResult send(StringView message, bool end_of_stream);
228228
void close(); // NB: callbacks can still occur: reset() to prevent further
229229
// callbacks.
230230
void reset();
@@ -251,13 +251,14 @@ class GrpcStreamHandler : public GrpcStreamHandlerBase {
251251
GrpcStreamHandler() : GrpcStreamHandlerBase() {}
252252
virtual ~GrpcStreamHandler() {}
253253

254-
void send(const Request &message, bool end_of_stream) {
254+
WasmResult send(const Request &message, bool end_of_stream) {
255255
std::string output;
256256
if (!message.SerializeToString(&output)) {
257-
return;
257+
return WasmResult::SerializationFailure;
258258
}
259259
GrpcStreamHandlerBase::send(output, end_of_stream);
260260
local_close_ = local_close_ || end_of_stream;
261+
return WasmResult::Ok;
261262
}
262263

263264
virtual void onReceive(size_t body_size) = 0;
@@ -335,14 +336,11 @@ class RootContext : public ContextBase {
335336
uint32_t timeout_milliseconds, HttpCallCallback callback);
336337
// NB: the message is the response if status == OK and an error message
337338
// otherwise. Returns false on setup error.
338-
#ifdef PROXY_WASM_PROTOBUF
339339
WasmResult grpcSimpleCall(StringView service, StringView service_name, StringView method_name,
340-
const HeaderStringPairs &initial_metadata,
341-
const google::protobuf::MessageLite &request,
340+
const HeaderStringPairs &initial_metadata, StringView request,
342341
uint32_t timeout_milliseconds, GrpcSimpleCallCallback callback);
343342
WasmResult grpcSimpleCall(StringView service, StringView service_name, StringView method_name,
344-
const HeaderStringPairs &initial_metadata,
345-
const google::protobuf::MessageLite &request,
343+
const HeaderStringPairs &initial_metadata, StringView request,
346344
uint32_t timeout_milliseconds,
347345
std::function<void(size_t body_size)> success_callback,
348346
std::function<void(GrpcStatus status)> failure_callback) {
@@ -356,12 +354,48 @@ class RootContext : public ContextBase {
356354
return grpcSimpleCall(service, service_name, method_name, initial_metadata, request,
357355
timeout_milliseconds, callback);
358356
}
357+
WasmResult grpcCallHandler(StringView service, StringView service_name, StringView method_name,
358+
const HeaderStringPairs &initial_metadata, StringView request,
359+
uint32_t timeout_milliseconds,
360+
std::unique_ptr<GrpcCallHandlerBase> handler);
361+
#ifdef PROXY_WASM_PROTOBUF
362+
WasmResult grpcSimpleCall(StringView service, StringView service_name, StringView method_name,
363+
const HeaderStringPairs &initial_metadata,
364+
const google::protobuf::MessageLite &request,
365+
uint32_t timeout_milliseconds, GrpcSimpleCallCallback callback) {
366+
std::string serialized_request;
367+
if (!request.SerializeToString(&serialized_request)) {
368+
return WasmResult::SerializationFailure;
369+
}
370+
return grpcSimpleCall(service, service_name, method_name, initial_metadata, serialized_request,
371+
timeout_milliseconds, callback);
372+
}
373+
WasmResult grpcSimpleCall(StringView service, StringView service_name, StringView method_name,
374+
const HeaderStringPairs &initial_metadata,
375+
const google::protobuf::MessageLite &request,
376+
uint32_t timeout_milliseconds,
377+
std::function<void(size_t body_size)> success_callback,
378+
std::function<void(GrpcStatus status)> failure_callback) {
379+
std::string serialized_request;
380+
if (!request.SerializeToString(&serialized_request)) {
381+
return WasmResult::SerializationFailure;
382+
}
383+
return grpcSimpleCall(service, service_name, method_name, initial_metadata, serialized_request,
384+
timeout_milliseconds, success_callback, failure_callback);
385+
}
359386
// Returns false on setup error.
360387
WasmResult grpcCallHandler(StringView service, StringView service_name, StringView method_name,
361388
const HeaderStringPairs &initial_metadata,
362389
const google::protobuf::MessageLite &request,
363390
uint32_t timeout_milliseconds,
364-
std::unique_ptr<GrpcCallHandlerBase> handler);
391+
std::unique_ptr<GrpcCallHandlerBase> handler) {
392+
std::string serialized_request;
393+
if (!request.SerializeToString(&serialized_request)) {
394+
return WasmResult::SerializationFailure;
395+
}
396+
return grpcCallHandler(service, service_name, method_name, initial_metadata, serialized_request,
397+
timeout_milliseconds, std::move(handler));
398+
}
365399
#endif
366400
// Returns false on setup error.
367401
WasmResult grpcStreamHandler(StringView service, StringView service_name, StringView method_name,
@@ -1185,20 +1219,28 @@ inline Histogram<Tags...> *Histogram<Tags...>::New(StringView name,
11851219
std::vector<MetricTag>({toMetricTag(descriptors)...}));
11861220
}
11871221

1188-
#ifdef PROXY_WASM_PROTOBUF
11891222
inline WasmResult grpcCall(StringView service, StringView service_name, StringView method_name,
1190-
const HeaderStringPairs &initial_metadata,
1191-
const google::protobuf::MessageLite &request,
1223+
const HeaderStringPairs &initial_metadata, StringView request,
11921224
uint32_t timeout_milliseconds, uint32_t *token_ptr) {
11931225
void *metadata_ptr = nullptr;
11941226
size_t metadata_size = 0;
11951227
MakeHeaderStringPairsBuffer(initial_metadata, &metadata_ptr, &metadata_size);
1196-
std::string serialized_request;
1197-
request.SerializeToString(&serialized_request);
11981228
return proxy_grpc_call(service.data(), service.size(), service_name.data(), service_name.size(),
11991229
method_name.data(), method_name.size(), metadata_ptr, metadata_size,
1200-
serialized_request.data(), serialized_request.size(), timeout_milliseconds,
1201-
token_ptr);
1230+
request.data(), request.size(), timeout_milliseconds, token_ptr);
1231+
}
1232+
1233+
#ifdef PROXY_WASM_PROTOBUF
1234+
inline WasmResult grpcCall(StringView service, StringView service_name, StringView method_name,
1235+
const HeaderStringPairs &initial_metadata,
1236+
const google::protobuf::MessageLite &request,
1237+
uint32_t timeout_milliseconds, uint32_t *token_ptr) {
1238+
std::string serialized_request;
1239+
if (!request.SerializeToString(&serialized_request)) {
1240+
return WasmResult::SerializationFailure;
1241+
}
1242+
return grpcCall(service, service_name, method_name, initial_metadata, serialized_request,
1243+
timeout_milliseconds, token_ptr);
12021244
}
12031245
#endif
12041246

@@ -1242,12 +1284,10 @@ inline void RootContext::onHttpCallResponse(uint32_t token, uint32_t headers, si
12421284
}
12431285
}
12441286

1245-
#ifdef PROXY_WASM_PROTOBUF
12461287
inline WasmResult RootContext::grpcSimpleCall(StringView service, StringView service_name,
12471288
StringView method_name,
12481289
const HeaderStringPairs &initial_metadata,
1249-
const google::protobuf::MessageLite &request,
1250-
uint32_t timeout_milliseconds,
1290+
StringView request, uint32_t timeout_milliseconds,
12511291
Context::GrpcSimpleCallCallback callback) {
12521292
uint32_t token = 0;
12531293
WasmResult result = grpcCall(service, service_name, method_name, initial_metadata, request,
@@ -1257,7 +1297,6 @@ inline WasmResult RootContext::grpcSimpleCall(StringView service, StringView ser
12571297
}
12581298
return result;
12591299
}
1260-
#endif
12611300

12621301
inline void GrpcCallHandlerBase::cancel() {
12631302
grpcCancel(token_);
@@ -1278,15 +1317,19 @@ inline void GrpcStreamHandlerBase::close() {
12781317
// NB: else callbacks can still occur: reset() to prevent further callbacks.
12791318
}
12801319

1281-
inline void GrpcStreamHandlerBase::send(StringView message, bool end_of_stream) {
1282-
grpcSend(token_, message, end_of_stream);
1320+
inline WasmResult GrpcStreamHandlerBase::send(StringView message, bool end_of_stream) {
1321+
WasmResult r = grpcSend(token_, message, end_of_stream);
1322+
if (r != WasmResult::Ok) {
1323+
return r;
1324+
}
12831325
if (end_of_stream) {
12841326
// NB: callbacks can still occur: reset() to prevent further callbacks.
12851327
local_close_ = local_close_ || end_of_stream;
12861328
if (local_close_ && remote_close_) {
12871329
context_->grpc_streams_.erase(token_);
12881330
}
12891331
}
1332+
return WasmResult::Ok;
12901333
}
12911334

12921335
inline void RootContext::onGrpcReceiveInitialMetadata(uint32_t token, uint32_t headers) {
@@ -1377,12 +1420,10 @@ inline void RootContext::onGrpcClose(uint32_t token, GrpcStatus status) {
13771420
}
13781421
}
13791422

1380-
#ifdef PROXY_WASM_PROTOBUF
13811423
inline WasmResult RootContext::grpcCallHandler(StringView service, StringView service_name,
13821424
StringView method_name,
13831425
const HeaderStringPairs &initial_metadata,
1384-
const google::protobuf::MessageLite &request,
1385-
uint32_t timeout_milliseconds,
1426+
StringView request, uint32_t timeout_milliseconds,
13861427
std::unique_ptr<GrpcCallHandlerBase> handler) {
13871428
uint32_t token = 0;
13881429
auto result = grpcCall(service, service_name, method_name, initial_metadata, request,
@@ -1394,7 +1435,6 @@ inline WasmResult RootContext::grpcCallHandler(StringView service, StringView se
13941435
}
13951436
return result;
13961437
}
1397-
#endif
13981438

13991439
inline WasmResult RootContext::grpcStreamHandler(StringView service, StringView service_name,
14001440
StringView method_name,

0 commit comments

Comments
 (0)