Skip to content

Commit c00a9a6

Browse files
committed
Plugins can now be configured to be shared, added tests to validate.
If shared, 1 instance will be shared between request invocations. If not shared a new plugin instance is created for each request. Signed-off-by: Hiram Chirino <hiram@hiramchirino.com>
1 parent efa48ad commit c00a9a6

File tree

7 files changed

+95
-27
lines changed

7 files changed

+95
-27
lines changed

proxy-wasm-java-host/src/test/go-examples/http_headers/main.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,16 @@ type pluginContext struct {
5050
// plugin configuration during OnPluginStart.
5151
headerName string
5252
headerValue string
53+
counter int
5354
}
5455

5556
// NewHttpContext implements types.PluginContext.
5657
func (p *pluginContext) NewHttpContext(contextID uint32) types.HttpContext {
5758
return &httpHeaders{
58-
contextID: contextID,
59-
headerName: p.headerName,
60-
headerValue: p.headerValue,
59+
contextID: contextID,
60+
pluginContext: p,
61+
headerName: p.headerName,
62+
headerValue: p.headerValue,
6163
}
6264
}
6365

@@ -97,10 +99,10 @@ type httpHeaders struct {
9799
// Embed the default http context here,
98100
// so that we don't need to reimplement all the methods.
99101
types.DefaultHttpContext
100-
contextID uint32
101-
headerName string
102-
headerValue string
103-
counter int
102+
contextID uint32
103+
headerName string
104+
headerValue string
105+
pluginContext *pluginContext
104106
}
105107

106108
// OnHttpRequestHeaders implements types.HttpContext.
@@ -130,8 +132,8 @@ func (ctx *httpHeaders) OnHttpResponseHeaders(_ int, _ bool) types.Action {
130132
proxywasm.LogCriticalf("failed to set response constant header: %v", err)
131133
}
132134

133-
ctx.counter++
134-
if err := proxywasm.AddHttpResponseHeader("x-proxy-wasm-counter", fmt.Sprintf("%d", ctx.counter)); err != nil {
135+
ctx.pluginContext.counter++
136+
if err := proxywasm.AddHttpResponseHeader("x-proxy-wasm-counter", fmt.Sprintf("%d", ctx.pluginContext.counter)); err != nil {
135137
proxywasm.LogCriticalf("failed to set response counter header: %v", err)
136138
}
137139

231 Bytes
Binary file not shown.

proxy-wasm-jaxrs/src/main/java/io/roastedroot/proxywasm/jaxrs/WasmPlugin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ public static class Builder implements Cloneable {
6060

6161
private PluginHandler handler = new PluginHandler();
6262
private ProxyWasm.Builder proxyWasmBuilder = ProxyWasm.builder().withPluginHandler(handler);
63-
private boolean shared;
63+
private boolean shared = true;
6464

6565
public WasmPlugin.Builder withName(String name) {
6666
this.handler.name = name;
6767
return this;
6868
}
6969

70-
public WasmPlugin.Builder withVmConfig(boolean shared) {
70+
public WasmPlugin.Builder withShared(boolean shared) {
7171
this.shared = shared;
7272
return this;
7373
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package io.roastedroot.proxywasm.jaxrs;
2+
3+
import static io.restassured.RestAssured.given;
4+
import static io.roastedroot.proxywasm.jaxrs.TestHelpers.EXAMPLES_DIR;
5+
6+
import com.dylibso.chicory.wasm.Parser;
7+
import io.quarkus.test.junit.QuarkusTest;
8+
import io.roastedroot.proxywasm.StartException;
9+
import jakarta.enterprise.inject.Produces;
10+
import java.nio.file.Path;
11+
import org.junit.jupiter.api.Test;
12+
13+
@QuarkusTest
14+
public class HttpHeadersNotSharedTest {
15+
16+
@Produces
17+
public WasmPluginFactory create() throws StartException {
18+
return () ->
19+
WasmPlugin.builder()
20+
.withName("notSharedHttpHeaders")
21+
.withShared(false)
22+
.withPluginConfig("{\"header\": \"x-wasm-header\", \"value\": \"foo\"}")
23+
.build(
24+
Parser.parse(
25+
Path.of(
26+
EXAMPLES_DIR
27+
+ "/go-examples/http_headers/main.wasm")));
28+
}
29+
30+
@Test
31+
public void testRequest() {
32+
33+
// since the plugin is not shared, the counter should not increment since each request gets
34+
// a new plugin instance.
35+
given().when()
36+
.get("/test/notSharedHttpHeaders")
37+
.then()
38+
.statusCode(200)
39+
.header("x-proxy-wasm-counter", "1");
40+
41+
given().when()
42+
.get("/test/notSharedHttpHeaders")
43+
.then()
44+
.statusCode(200)
45+
.header("x-proxy-wasm-counter", "1");
46+
}
47+
}

proxy-wasm-jaxrs/src/test/java/io/roastedroot/proxywasm/jaxrs/HttpHeadersResource.java

Lines changed: 0 additions & 14 deletions
This file was deleted.

proxy-wasm-jaxrs/src/test/java/io/roastedroot/proxywasm/jaxrs/HttpHeadersTest.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public class HttpHeadersTest {
1818
public WasmPluginFactory create() throws StartException {
1919
return () ->
2020
WasmPlugin.builder()
21-
.withName("http_headers")
21+
.withName("httpHeaders")
22+
.withShared(true)
2223
.withPluginConfig("{\"header\": \"x-wasm-header\", \"value\": \"foo\"}")
2324
.build(
2425
Parser.parse(
@@ -30,11 +31,21 @@ public WasmPluginFactory create() throws StartException {
3031
@Test
3132
public void testRequest() {
3233
given().when()
33-
.get("/http_headers")
34+
.get("/test/httpHeaders")
3435
.then()
3536
.statusCode(200)
3637
.header("x-proxy-wasm-go-sdk-example", "http_headers")
3738
.header("x-wasm-header", "foo")
39+
.header("x-proxy-wasm-counter", "1")
40+
.body(equalTo("hello world"));
41+
42+
given().when()
43+
.get("/test/httpHeaders")
44+
.then()
45+
.statusCode(200)
46+
.header("x-proxy-wasm-go-sdk-example", "http_headers")
47+
.header("x-wasm-header", "foo")
48+
.header("x-proxy-wasm-counter", "2")
3849
.body(equalTo("hello world"));
3950
}
4051
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.roastedroot.proxywasm.jaxrs;
2+
3+
import jakarta.ws.rs.GET;
4+
import jakarta.ws.rs.Path;
5+
6+
@Path("/test")
7+
public class Resources {
8+
9+
@Path("/httpHeaders")
10+
@GET
11+
@NamedWasmPlugin("httpHeaders")
12+
public String httpHeaders() {
13+
return "hello world";
14+
}
15+
16+
@Path("/notSharedHttpHeaders")
17+
@GET
18+
@NamedWasmPlugin("notSharedHttpHeaders")
19+
public String notSharedHttpHeaders() {
20+
return "hello world";
21+
}
22+
}

0 commit comments

Comments
 (0)