Skip to content

Commit a6b0f83

Browse files
authored
Add support for closing of connections in the ABI/SDK. (proxy-wasm#26)
* Add support for closing of connections in the ABI/SDK. Signed-off-by: John Plevyak <jplevyak@gmail.com>
1 parent f54a861 commit a6b0f83

File tree

8 files changed

+30
-43
lines changed

8 files changed

+30
-43
lines changed

WORKSPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
55

66
git_repository(
77
name = "proxy_wasm_cpp_sdk",
8-
commit = "f750d1f5da6a2f20cc55da75dd9772b7ba1650ca",
8+
commit = "f44562520bca7bfeee77d6284a96d2900f2f13ac",
99
remote = "https://github.com/proxy-wasm/proxy-wasm-cpp-sdk",
1010
)
1111

include/proxy-wasm/context.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class ContextBase : public RootInterface,
195195
unimplemented();
196196
return nullptr;
197197
}
198-
bool endOfStream(StreamType /* type */) override {
198+
bool endOfStream(WasmStreamType /* type */) override {
199199
unimplemented();
200200
return true;
201201
}
@@ -254,7 +254,8 @@ class ContextBase : public RootInterface,
254254
}
255255

256256
// Continue
257-
WasmResult continueStream(StreamType /* stream_type */) override { return unimplemented(); }
257+
WasmResult continueStream(WasmStreamType /* stream_type */) override { return unimplemented(); }
258+
WasmResult closeStream(WasmStreamType /* stream_type */) override { return unimplemented(); }
258259
WasmResult sendLocalResponse(uint32_t /* response_code */, string_view /* body_text */,
259260
Pairs /* additional_headers */, GrpcStatusCode /* grpc_status */,
260261
string_view /* details */) override {

include/proxy-wasm/context_interface.h

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,13 @@ using GrpcStatusCode = uint32_t;
4040
using SharedQueueDequeueToken = uint32_t;
4141
using SharedQueueEnqueueToken = uint32_t;
4242

43-
// TODO: move to SDK.
44-
enum StreamType {
45-
Request,
46-
Response,
47-
Downstream,
48-
Upstream,
49-
};
50-
5143
// TODO: update SDK and use this.
5244
enum class ProxyAction : uint32_t {
5345
Illegal = 0,
5446
Continue = 1,
5547
Pause = 2,
5648
};
5749

58-
/*
59-
* The source of the close event.
60-
* Unknown is when the source is not known.
61-
* Local is when the close was initiated by the proxy.
62-
* Remote is when the close was received from the peer.
63-
*/
64-
// TODO: update SDK.
65-
enum class CloseType : uint32_t {
66-
Unknown = 0,
67-
Local = 1,
68-
Remote = 2,
69-
};
70-
7150
struct PluginBase;
7251
class WasmBase;
7352

@@ -331,8 +310,11 @@ struct NetworkInterface {
331310
**/
332311
struct StreamInterface {
333312
virtual ~StreamInterface() = default;
334-
// Continue processing a request e.g. after returning ProxyAction::Pause.
335-
virtual WasmResult continueStream(StreamType type) = 0;
313+
// Continue processing e.g. after returning ProxyAction::Pause.
314+
virtual WasmResult continueStream(WasmStreamType type) = 0;
315+
316+
// Close a stream.
317+
virtual WasmResult closeStream(WasmStreamType type) = 0;
336318

337319
/**
338320
* Provides a BufferInterface to be used to return buffered data to the VM.
@@ -345,7 +327,7 @@ struct StreamInterface {
345327
* when a connection is closed (or half-closed) either locally or remotely.
346328
* @param stream_type is the flow
347329
*/
348-
virtual bool endOfStream(StreamType type) = 0;
330+
virtual bool endOfStream(WasmStreamType type) = 0;
349331
};
350332

351333
// Header/Trailer/Metadata Maps

include/proxy-wasm/exports.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ Word log(void *raw_context, Word level, Word address, Word size);
2929
Word get_property(void *raw_context, Word path_ptr, Word path_size, Word value_ptr_ptr,
3030
Word value_size_ptr);
3131
Word set_property(void *raw_context, Word key_ptr, Word key_size, Word value_ptr, Word value_size);
32-
Word continue_request(void *raw_context);
33-
Word continue_response(void *raw_context);
32+
Word continue_stream(void *raw_context, Word stream_type);
33+
Word close_stream(void *raw_context, Word stream_type);
3434
Word send_local_response(void *raw_context, Word response_code, Word response_code_details_ptr,
3535
Word response_code_details_size, Word body_ptr, Word body_size,
3636
Word additional_response_header_pairs_ptr,

include/proxy-wasm/wasm_api_impl.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ inline WasmResult proxy_set_property(const char *key_ptr, size_t key_size, const
6565
}
6666

6767
// Continue
68-
inline WasmResult proxy_continue_request() {
69-
return wordToWasmResult(exports::continue_request(current_context_));
68+
inline WasmResult proxy_continue_stream(WasmStreamType stream_type) {
69+
return wordToWasmResult(exports::continue_stream(current_context_, WS(stream_type)));
7070
}
71-
inline WasmResult proxy_continue_response() {
72-
return wordToWasmResult(exports::continue_response(current_context_));
71+
inline WasmResult proxy_close_stream(WasmStreamType stream_type) {
72+
return wordToWasmResult(exports::close_stream(current_context_, WS(stream_type)));
7373
}
7474
inline WasmResult
7575
proxy_send_local_response(uint32_t response_code, const char *response_code_details_ptr,

src/exports.cc

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,20 @@ Word get_status(void *raw_context, Word code_ptr, Word value_ptr_ptr, Word value
164164
// HTTP
165165

166166
// Continue/Reply/Route
167-
Word continue_request(void *raw_context) {
167+
Word continue_stream(void *raw_context, Word type) {
168168
auto context = WASM_CONTEXT(raw_context);
169-
context->continueStream(StreamType::Request);
170-
return WasmResult::Ok;
169+
if (type > static_cast<uint64_t>(WasmStreamType::MAX)) {
170+
return WasmResult::BadArgument;
171+
}
172+
return context->continueStream(static_cast<WasmStreamType>(type.u64_));
171173
}
172174

173-
Word continue_response(void *raw_context) {
175+
Word close_stream(void *raw_context, Word type) {
174176
auto context = WASM_CONTEXT(raw_context);
175-
context->continueStream(StreamType::Response);
176-
return WasmResult::Ok;
177+
if (type > static_cast<uint64_t>(WasmStreamType::MAX)) {
178+
return WasmResult::BadArgument;
179+
}
180+
return context->closeStream(static_cast<WasmStreamType>(type.u64_));
177181
}
178182

179183
Word send_local_response(void *raw_context, Word response_code, Word response_code_details_ptr,

src/null/null_plugin.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,11 @@ uint64_t NullPlugin::onUpstreamData(uint64_t context_id, uint64_t data_length,
376376
}
377377

378378
void NullPlugin::onDownstreamConnectionClose(uint64_t context_id, uint64_t close_type) {
379-
getContext(context_id)->onDownstreamConnectionClose(static_cast<PeerType>(close_type));
379+
getContext(context_id)->onDownstreamConnectionClose(static_cast<CloseType>(close_type));
380380
}
381381

382382
void NullPlugin::onUpstreamConnectionClose(uint64_t context_id, uint64_t close_type) {
383-
getContext(context_id)->onUpstreamConnectionClose(static_cast<PeerType>(close_type));
383+
getContext(context_id)->onUpstreamConnectionClose(static_cast<CloseType>(close_type));
384384
}
385385

386386
uint64_t NullPlugin::onRequestHeaders(uint64_t context_id, uint64_t headers,

src/wasm.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ void WasmBase::registerCallbacks() {
146146
_REGISTER_PROXY(set_property);
147147
_REGISTER_PROXY(get_property);
148148

149-
_REGISTER_PROXY(continue_request);
150-
_REGISTER_PROXY(continue_response);
149+
_REGISTER_PROXY(continue_stream);
150+
_REGISTER_PROXY(close_stream);
151151
_REGISTER_PROXY(send_local_response);
152152

153153
_REGISTER_PROXY(get_shared_data);

0 commit comments

Comments
 (0)