Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private ProxyWasm(Builder other) throws StartException {

public void start() throws StartException {
if (pluginContext != null) {
throw new IllegalStateException("already started");
return;
}

this.pluginContext = new PluginContext(this, pluginHandler);
Expand Down
2 changes: 0 additions & 2 deletions proxy-wasm-jaxrs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,8 @@
<executions>
<execution>
<goals>
<goal>build</goal>
<goal>generate-code</goal>
<goal>generate-code-tests</goal>
<goal>native-image-agent</goal>
</goals>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import io.roastedroot.proxywasm.StreamType;
import io.roastedroot.proxywasm.WasmException;
import io.roastedroot.proxywasm.WasmResult;
import io.roastedroot.proxywasm.jaxrs.spi.HttpServer;
import io.roastedroot.proxywasm.jaxrs.spi.HttpServerRequest;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerResponseContext;
import jakarta.ws.rs.core.Response;
Expand All @@ -64,10 +64,10 @@
class HttpHandler extends ChainedHandler {

private final PluginHandler next;
private final HttpServer httpServer;
private final HttpServerRequest httpServer;
private final long startedAt;

HttpHandler(PluginHandler pluginHandler, HttpServer httpServer) {
HttpHandler(PluginHandler pluginHandler, HttpServerRequest httpServer) {
this.next = pluginHandler;
this.httpServer = httpServer;
this.startedAt = System.currentTimeMillis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class PluginHandler extends ChainedHandler {
// Filter Chain Methods
// //////////////////////////////////////////////////////////////////////
private Handler next;
WasmPlugin plugin;

PluginHandler() {
this(new Handler() {});
Expand All @@ -37,6 +38,16 @@ protected Handler next() {
return next;
}

// //////////////////////////////////////////////////////////////////////
// Cleanup
// //////////////////////////////////////////////////////////////////////
public void close() {
if (cancelTick != null) {
cancelTick.run();
cancelTick = null;
}
}

// //////////////////////////////////////////////////////////////////////
// Properties
// //////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -103,15 +114,46 @@ public LogLevel getLogLevel() throws WasmException {
// Timers
// //////////////////////////////////////////////////////////////////////

int minTickPeriodMilliseconds;
private int tickPeriodMilliseconds;
private Runnable cancelTick;

public int getTickPeriodMilliseconds() {
return tickPeriodMilliseconds;
}

@Override
public WasmResult setTickPeriodMilliseconds(int tickPeriodMilliseconds) {

// check for no change
if (tickPeriodMilliseconds == this.tickPeriodMilliseconds) {
return WasmResult.OK;
}

// cancel the current tick, if any
if (cancelTick != null) {
cancelTick.run();
cancelTick = null;
}

// set the new tick period, if any
this.tickPeriodMilliseconds = tickPeriodMilliseconds;
if (this.tickPeriodMilliseconds == 0) {
return WasmResult.OK;
}

// schedule the new tick
this.cancelTick =
this.plugin.httpServer.scheduleTick(
Math.max(minTickPeriodMilliseconds, this.tickPeriodMilliseconds),
() -> {
this.plugin.lock();
try {
this.plugin.wasm.tick();
} finally {
this.plugin.unlock();
}
});
return WasmResult.OK;
}

Expand All @@ -132,6 +174,10 @@ public WasmResult setFuncCallData(byte[] data) {
return WasmResult.OK;
}

public void setPlugin(WasmPlugin plugin) {
this.plugin = plugin;
}

// //////////////////////////////////////////////////////////////////////
// HTTP calls
// //////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -296,6 +342,9 @@ public WasmResult removeMetric(int metricId) {

@Override
public ForeignFunction getForeignFunction(String name) {
return super.getForeignFunction(name);
if (foreignFunctions == null) {
return null;
}
return foreignFunctions.get(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
import io.roastedroot.proxywasm.Action;
import io.roastedroot.proxywasm.HttpContext;
import io.roastedroot.proxywasm.StartException;
import io.roastedroot.proxywasm.jaxrs.spi.HttpServer;
import io.roastedroot.proxywasm.jaxrs.spi.HttpServerRequest;
import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerRequestFilter;
import jakarta.ws.rs.container.ContainerResponseContext;
import jakarta.ws.rs.container.ContainerResponseFilter;
import jakarta.ws.rs.container.PreMatching;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ReaderInterceptor;
import jakarta.ws.rs.ext.ReaderInterceptorContext;
Expand All @@ -20,7 +18,6 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;

@PreMatching
public class ProxyWasmFilter
implements ContainerRequestFilter,
ReaderInterceptor,
Expand All @@ -30,14 +27,17 @@ public class ProxyWasmFilter

private final WasmPluginPool pluginPool;

Instance<HttpServer> httpServer;
Instance<HttpServerRequest> httpServer;

@Inject
public ProxyWasmFilter(WasmPluginPool pluginPool, Instance<HttpServer> httpServer) {
public ProxyWasmFilter(WasmPluginPool pluginPool, Instance<HttpServerRequest> httpServer) {
this.pluginPool = pluginPool;
this.httpServer = httpServer;
}

String name() {
return pluginPool.name();
}

// TODO: the HttpContext and ProxyWasm object's should be closed once the request is done.
// is there an easy way to hook up cleanup code for this?
static class WasmHttpFilterContext {
Expand All @@ -46,22 +46,27 @@ static class WasmHttpFilterContext {
final HttpHandler httpHandler;
final HttpContext httpContext;

public WasmHttpFilterContext(WasmPlugin plugin, HttpServer httpServer) {
public WasmHttpFilterContext(WasmPlugin plugin, HttpServerRequest httpServer) {
this.plugin = plugin;
this.pluginHandler = plugin.pluginHandler();
this.httpHandler = new HttpHandler(plugin.pluginHandler(), httpServer);
this.httpContext = plugin.proxyWasm().createHttpContext(this.httpHandler);
this.pluginHandler = plugin.handler;
this.httpHandler = new HttpHandler(plugin.handler, httpServer);
this.httpContext = plugin.wasm.createHttpContext(this.httpHandler);
}
}

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {

WasmPlugin plugin = null;
WasmPlugin plugin;
try {
plugin = pluginPool.borrow();
plugin.lock();
} catch (StartException e) {
requestContext.abortWith(interalServerError());
return;
}

plugin.lock();
try {
var ctx = new WasmHttpFilterContext(plugin, this.httpServer.get());
requestContext.setProperty(FILTER_CONTEXT_PROPERTY_NAME, ctx);

Expand All @@ -81,24 +86,23 @@ public void filter(ContainerRequestContext requestContext) throws IOException {
requestContext.abortWith(sendResponse.toResponse());
}
}

} catch (StartException e) {
requestContext.abortWith(
Response.status(Response.Status.INTERNAL_SERVER_ERROR).build());
} finally {
plugin.unlock(); // allow another request to use the plugin.
}
}

private static Response interalServerError() {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}

@Override
public Object aroundReadFrom(ReaderInterceptorContext ctx)
throws IOException, WebApplicationException {

var wasmHttpFilterContext =
(WasmHttpFilterContext) ctx.getProperty(FILTER_CONTEXT_PROPERTY_NAME);
if (wasmHttpFilterContext == null) {
throw new WebApplicationException(
Response.status(Response.Status.INTERNAL_SERVER_ERROR).build());
throw new WebApplicationException(interalServerError());
}

// the plugin may not be interested in the request body.
Expand Down Expand Up @@ -147,8 +151,7 @@ public void filter(
var wasmHttpFilterContext =
(WasmHttpFilterContext) requestContext.getProperty(FILTER_CONTEXT_PROPERTY_NAME);
if (wasmHttpFilterContext == null) {
throw new WebApplicationException(
Response.status(Response.Status.INTERNAL_SERVER_ERROR).build());
throw new WebApplicationException(interalServerError());
}

// the plugin may not be interested in the request headers.
Expand Down Expand Up @@ -182,8 +185,7 @@ public void aroundWriteTo(WriterInterceptorContext ctx)
var wasmHttpFilterContext =
(WasmHttpFilterContext) ctx.getProperty(FILTER_CONTEXT_PROPERTY_NAME);
if (wasmHttpFilterContext == null) {
throw new WebApplicationException(
Response.status(Response.Status.INTERNAL_SERVER_ERROR).build());
throw new WebApplicationException(interalServerError());
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,32 @@
import io.roastedroot.proxywasm.ForeignFunction;
import io.roastedroot.proxywasm.ProxyWasm;
import io.roastedroot.proxywasm.StartException;
import io.roastedroot.proxywasm.jaxrs.spi.HttpServer;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.locks.ReentrantLock;

public class WasmPlugin {

private final ProxyWasm proxyWasm;
private final PluginHandler handler;
final PluginHandler handler;
private final ReentrantLock lock;
final ProxyWasm wasm;
HttpServer httpServer;

private WasmPlugin(ProxyWasm proxyWasm, PluginHandler handler, boolean shared) {
Objects.requireNonNull(proxyWasm);
Objects.requireNonNull(handler);
this.proxyWasm = proxyWasm;
this.wasm = proxyWasm;
this.handler = handler;
this.lock = shared ? new ReentrantLock() : null;
this.handler.setPlugin(this);
}

public String name() {
return handler.getName();
}

ProxyWasm proxyWasm() {
return proxyWasm;
}

PluginHandler pluginHandler() {
return handler;
}

public static WasmPlugin.Builder builder() {
return new WasmPlugin.Builder();
}
Expand All @@ -59,10 +54,25 @@ public boolean isShared() {
return lock != null;
}

public void setHttpServer(HttpServer httpServer) {
this.httpServer = httpServer;
}

public void close() {
lock();
try {
wasm.close();
handler.close();
} finally {
unlock();
}
}

public static class Builder implements Cloneable {

private PluginHandler handler = new PluginHandler();
private ProxyWasm.Builder proxyWasmBuilder = ProxyWasm.builder().withPluginHandler(handler);
private ProxyWasm.Builder proxyWasmBuilder =
ProxyWasm.builder().withPluginHandler(handler).withStart(false);
private boolean shared = true;

public WasmPlugin.Builder withName(String name) {
Expand All @@ -75,6 +85,11 @@ public Builder withForeignFunctions(Map<String, ForeignFunction> functions) {
return this;
}

public Builder withMinTickPeriodMilliseconds(int minTickPeriodMilliseconds) {
this.handler.minTickPeriodMilliseconds = minTickPeriodMilliseconds;
return this;
}

public Builder withLogger(Logger logger) {
this.handler.logger = logger;
return this;
Expand Down
Loading
Loading