Skip to content

Commit 0318597

Browse files
authored
Make HTTP/gRPC callout IDs unique per WasmVM instance. (#186)
Fixes #185. Signed-off-by: Piotr Sikora <[email protected]>
1 parent 7811c57 commit 0318597

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

include/proxy-wasm/wasm.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,35 @@ class WasmBase : public std::enable_shared_from_this<WasmBase> {
176176
uint32_t nextGaugeMetricId() { return next_gauge_metric_id_ += kMetricIdIncrement; }
177177
uint32_t nextHistogramMetricId() { return next_histogram_metric_id_ += kMetricIdIncrement; }
178178

179+
enum class CalloutType : uint32_t {
180+
HttpCall = 0,
181+
GrpcCall = 1,
182+
GrpcStream = 2,
183+
};
184+
static const uint32_t kCalloutTypeMask = 0x3; // Enough to cover the 3 types.
185+
static const uint32_t kCalloutIncrement = 0x4; // Enough to cover the 3 types.
186+
bool isHttpCallId(uint32_t callout_id) {
187+
return (callout_id & kCalloutTypeMask) == static_cast<uint32_t>(CalloutType::HttpCall);
188+
}
189+
bool isGrpcCallId(uint32_t callout_id) {
190+
return (callout_id & kCalloutTypeMask) == static_cast<uint32_t>(CalloutType::GrpcCall);
191+
}
192+
bool isGrpcStreamId(uint32_t callout_id) {
193+
return (callout_id & kCalloutTypeMask) == static_cast<uint32_t>(CalloutType::GrpcStream);
194+
}
195+
uint32_t nextHttpCallId() {
196+
// TODO(PiotrSikora): re-add rollover protection (requires at least 1 billion callouts).
197+
return next_http_call_id_ += kCalloutIncrement;
198+
}
199+
uint32_t nextGrpcCallId() {
200+
// TODO(PiotrSikora): re-add rollover protection (requires at least 1 billion callouts).
201+
return next_grpc_call_id_ += kCalloutIncrement;
202+
}
203+
uint32_t nextGrpcStreamId() {
204+
// TODO(PiotrSikora): re-add rollover protection (requires at least 1 billion callouts).
205+
return next_grpc_stream_id_ += kCalloutIncrement;
206+
}
207+
179208
protected:
180209
friend class ContextBase;
181210
class ShutdownHandle;
@@ -279,6 +308,11 @@ class WasmBase : public std::enable_shared_from_this<WasmBase> {
279308
uint32_t next_gauge_metric_id_ = static_cast<uint32_t>(MetricType::Gauge);
280309
uint32_t next_histogram_metric_id_ = static_cast<uint32_t>(MetricType::Histogram);
281310

311+
// HTTP/gRPC callouts.
312+
uint32_t next_http_call_id_ = static_cast<uint32_t>(CalloutType::HttpCall);
313+
uint32_t next_grpc_call_id_ = static_cast<uint32_t>(CalloutType::GrpcCall);
314+
uint32_t next_grpc_stream_id_ = static_cast<uint32_t>(CalloutType::GrpcStream);
315+
282316
// Actions to be done after the call into the VM returns.
283317
std::deque<std::function<void()>> after_vm_call_actions_;
284318

0 commit comments

Comments
 (0)