Skip to content
This repository was archived by the owner on Dec 16, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 34 additions & 16 deletions api/wasm/cpp/proxy_wasm_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ class ContextBase;
class RootContext;
class Context;

// Note: exceptions are currently not supported.
#define WASM_EXCEPTIONS 0
#if WASM_EXCEPTIONS
class ProxyException : std::runtime_error {
public:
ProxyException(const std::string& message) : std::runtime_error(message) {}
};
#endif

inline void logTrace(const std::string& logMessage) {
proxy_log(LogLevel::trace, logMessage.c_str(), logMessage.size());
Expand Down Expand Up @@ -248,11 +252,13 @@ class ContextBase {
using HttpCallCallback = std::function<void(std::unique_ptr<WasmData> header_pairs,
std::unique_ptr<WasmData> body, std::unique_ptr<WasmData> trailer_pairs)>;
using GrpcSimpleCallCallback = std::function<void(GrpcStatus status, std::unique_ptr<WasmData> message)>;
void httpCall(StringView uri, const HeaderStringPairs& request_headers,
// Returns false on setup error.
bool httpCall(StringView uri, const HeaderStringPairs& request_headers,
StringView request_body, const HeaderStringPairs& request_trailers,
uint32_t timeout_milliseconds, HttpCallCallback callback);
// NB: the message is the response if status == OK and an error message otherwise.
void grpcSimpleCall(StringView service, StringView service_name, StringView method_name,
// Returns false on setup error.
bool grpcSimpleCall(StringView service, StringView service_name, StringView method_name,
const google::protobuf::MessageLite &request, uint32_t timeout_milliseconds, GrpcSimpleCallCallback callback);
template<typename Response> void grpcSimpleCall(StringView service, StringView service_name,
StringView method_name, const google::protobuf::MessageLite &request, uint32_t timeout_milliseconds,
Expand All @@ -267,10 +273,12 @@ class ContextBase {
};
grpcSimpleCall(service, service_name, method_name, request, timeout_milliseconds, callback);
}
void grpcCallHandler(StringView service, StringView service_name,
// Returns false on setup error.
bool grpcCallHandler(StringView service, StringView service_name,
StringView method_name, const google::protobuf::MessageLite &request, uint32_t timeout_milliseconds,
std::unique_ptr<GrpcCallHandlerBase> handler);
void grpcStreamHandler(StringView service, StringView service_name,
// Returns false on setup error.
bool grpcStreamHandler(StringView service, StringView service_name,
StringView method_name, std::unique_ptr<GrpcStreamHandlerBase> handler);

private:
Expand Down Expand Up @@ -953,14 +961,24 @@ inline std::string MetricBase::prefixWithFields(const std::vector<std::string>&

inline uint32_t MetricBase::resolveWithFields(const std::vector<std::string>& fields) {
if (fields.size() != tags.size()) {
#if WASM_EXCEPTIONS
throw ProxyException("metric fields.size() != tags.size()");
#else
logError("metric fields.size() != tags.size()");
abort();
#endif
}
return resolveFullName(prefixWithFields(fields) + name);
}

inline void MetricBase::partiallyResolveWithFields(const std::vector<std::string>& fields) {
if (fields.size() >= tags.size()) {
#if WASM_EXCEPTIONS
throw ProxyException("metric fields.size() >= tags.size()");
#else
logError("metric fields.size() >= tags.size()");
abort();
#endif
}
prefix = prefixWithFields(fields);
tags.erase(tags.begin(), tags.begin()+(fields.size()));
Expand Down Expand Up @@ -1220,15 +1238,15 @@ inline void grpcSend(uint32_t token, StringView message, bool end_stream) {
return proxy_grpcSend(token, message.data(), message.size(), end_stream ? 1 : 0);
}

inline void ContextBase::httpCall(StringView uri, const HeaderStringPairs& request_headers,
inline bool ContextBase::httpCall(StringView uri, const HeaderStringPairs& request_headers,
StringView request_body, const HeaderStringPairs& request_trailers,
uint32_t timeout_milliseconds, HttpCallCallback callback) {
auto token = makeHttpCall(uri, request_headers, request_body, request_trailers, timeout_milliseconds);
if (token) {
http_calls_[token] = std::move(callback);
} else {
throw ProxyException("httpCall failed");
return true;
}
return false;
}

inline void ContextBase::onHttpCallResponse(uint32_t token, std::unique_ptr<WasmData> header_pairs,
Expand All @@ -1240,14 +1258,14 @@ inline void ContextBase::onHttpCallResponse(uint32_t token, std::unique_ptr<Wasm
}
}

inline void ContextBase::grpcSimpleCall(StringView service, StringView service_name, StringView method_name,
inline bool ContextBase::grpcSimpleCall(StringView service, StringView service_name, StringView method_name,
const google::protobuf::MessageLite &request, uint32_t timeout_milliseconds, Context::GrpcSimpleCallCallback callback) {
auto token = grpcCall(service, service_name, method_name, request, timeout_milliseconds);
if (token) {
simple_grpc_calls_[token] = std::move(callback);
} else {
throw ProxyException("grpcCall failed");
return true;
}
return false;
}

inline void GrpcCallHandlerBase::cancel() {
Expand Down Expand Up @@ -1384,25 +1402,25 @@ inline void ContextBase::onGrpcClose(uint32_t token, GrpcStatus status, std::uni
}
}

inline void ContextBase::grpcCallHandler(StringView service, StringView service_name,
inline bool ContextBase::grpcCallHandler(StringView service, StringView service_name,
StringView method_name, const google::protobuf::MessageLite &request, uint32_t timeout_milliseconds,
std::unique_ptr<GrpcCallHandlerBase> handler) {
auto token = grpcCall(service, service_name, method_name, request, timeout_milliseconds);
if (token) {
handler->token_ = token;
grpc_calls_[token] = std::move(handler);
} else {
throw ProxyException("grpcCall failed");
return true;
}
return false;
}

inline void ContextBase::grpcStreamHandler(StringView service, StringView service_name,
inline bool ContextBase::grpcStreamHandler(StringView service, StringView service_name,
StringView method_name, std::unique_ptr<GrpcStreamHandlerBase> handler) {
auto token = grpcStream(service, service_name, method_name);
if (token) {
handler->token_ = token;
grpc_streams_[token] = std::move(handler);
} else {
throw ProxyException("grpcStream failed");
return true;
}
return false;
}
9 changes: 5 additions & 4 deletions api/wasm/cpp/proxy_wasm_intrinsics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ static Context* ensureContext(uint32_t context_id, uint32_t root_context_id) {
}
auto factory = context_factories->find(root_id);
if (factory == context_factories->end()) {
throw ProxyException("no context factory for root_id: " + root_id);
logError("no context factory for root_id: " + root_id);
return nullptr;
} else {
e.first->second = factory->second(context_id, root);
}
Expand Down Expand Up @@ -67,23 +68,23 @@ static RootContext* ensureRootContext(uint32_t context_id, std::unique_ptr<WasmD
static ContextBase* getContextBase(uint32_t context_id) {
auto it = context_map.find(context_id);
if (it == context_map.end() || !(it->second->asContext() || it->second->asRoot())) {
throw ProxyException("no base context context_id: " + std::to_string(context_id));
return nullptr;
}
return it->second.get();
}

static Context* getContext(uint32_t context_id) {
auto it = context_map.find(context_id);
if (it == context_map.end() || !it->second->asContext()) {
throw ProxyException("no context context_id: " + std::to_string(context_id));
return nullptr;
}
return it->second->asContext();
}

static RootContext* getRootContext(uint32_t context_id) {
auto it = context_map.find(context_id);
if (it == context_map.end() || !it->second->asRoot()) {
throw ProxyException("no root context_id: " + std::to_string(context_id));
return nullptr;
}
return it->second->asRoot();
}
Expand Down
Binary file modified examples/wasm/envoy_filter_http_wasm_example.wasm
Binary file not shown.
Loading