Skip to content
Permalink
Browse files
fix: convert the conf req to an object and put it in the cache (#73)
  • Loading branch information
tzssangglass committed Sep 27, 2021
1 parent 62d3a57 commit a5ca9ca8045a19694b94c57986ae7d298eff986e
Showing 5 changed files with 50 additions and 36 deletions.
@@ -31,8 +31,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -52,17 +52,20 @@ public void handle(A6Request request, A6Response response) {
Req req = ((A6ConfigRequest) request).getReq();
long token = ((A6ConfigResponse) response).getConfToken();
PluginFilterChain chain = createFilterChain(req);
ByteBuffer bb = req.getByteBuffer();

/**
* to reset vtable_start and vtable_size of req,
* so that req can be reused after being got from the cache.
* {@link org.apache.apisix.plugin.runner.handler.A6HttpCallHandler#handle cache.getIfPresent()}
* @see <a href="Issues63"> https://github.com/apache/apisix-java-plugin-runner/issues/63</a>
* */
req.__init(bb.getInt(bb.position()) + bb.position(), bb);
A6Conf config = new A6Conf(req, chain);
cache.put(token, config);
/*
* to reset vtable_start and vtable_size of req,
* so that req can be reused after being got from the cache.
* {@link org.apache.apisix.plugin.runner.handler.A6HttpCallHandler#handle cache.getIfPresent()}
* @see <a href="Issues63"> https://github.com/apache/apisix-java-plugin-runner/issues/63</a>
* */
Map<String, String> config = new HashMap<>();
for (int i = 0; i < req.confLength(); i++) {
TextEntry conf = req.conf(i);
config.put(conf.name(), conf.value());
}
A6Conf a6Conf = new A6Conf(config, chain);
cache.put(token, a6Conf);
}

private PluginFilterChain createFilterChain(Req req) {
@@ -52,7 +52,7 @@ public void handle(A6Request request, A6Response response) {
return;
}

req.initCtx(rsp, conf.getReq());
req.initCtx(rsp, conf.getConfig());
PluginFilterChain chain = conf.getChain();
chain.filter(req, rsp);
}
@@ -100,7 +100,6 @@ void testAddFilter1() {
Assertions.assertEquals(config.getChain().getFilters().size(), 1);
Assertions.assertEquals(config.getChain().getIndex(), 0);
Assertions.assertEquals(config.get("FooFilter"), "Bar");

}

@Test
@@ -176,4 +175,28 @@ void testAddFilter4() {
A6Conf config = cache.getIfPresent(0L);
Assertions.assertEquals(config.getChain().getFilters().size(), 0);
}

@Test
@DisplayName("test fetch conf more times")
void testAddFilter5() {
FlatBufferBuilder builder = new FlatBufferBuilder();
int name = builder.createString("FooFilter");
int value = builder.createString("Bar");
int conf = TextEntry.createTextEntry(builder, name, value);
int confVector = Req.createConfVector(builder, new int[]{conf});
Req.startReq(builder);
Req.addConf(builder, confVector);
builder.finish(Req.endReq(builder));
Req req = Req.getRootAsReq(builder.dataBuffer());

A6ConfigRequest request = new A6ConfigRequest(req);
A6ConfigResponse response = new A6ConfigResponse(0L);
a6ConfigHandler.handle(request, response);

A6Conf a6Conf = cache.getIfPresent(0L);
Assertions.assertTrue(a6Conf.getConfig() instanceof HashMap);
for (int i = 0; i < 100; i++) {
Assertions.assertEquals(a6Conf.get("FooFilter"), "Bar");
}
}
}
@@ -17,36 +17,30 @@

package org.apache.apisix.plugin.runner;

import io.github.api7.A6.PrepareConf.Req;
import io.github.api7.A6.TextEntry;
import org.apache.apisix.plugin.runner.filter.PluginFilterChain;

import java.util.Map;

public class A6Conf {

public Req getReq() {
return req;
public Map<String, String> getConfig() {
return config;
}

private final Req req;
private final Map<String, String> config;

public PluginFilterChain getChain() {
return chain;
}

private final PluginFilterChain chain;

public A6Conf(Req req, PluginFilterChain chain) {
this.req = req;
public A6Conf(Map<String, String> config, PluginFilterChain chain) {
this.config = config;
this.chain = chain;
}

public String get(String key) {
for (int i = 0; i < this.req.confLength(); i++) {
TextEntry conf = this.req.conf(i);
if (conf.name().equals(key)) {
return conf.value();
}
}
return null;
return config.getOrDefault(key, null);
}
}
@@ -33,7 +33,7 @@ public class HttpRequest implements A6Request {

private HttpResponse response;

private io.github.api7.A6.PrepareConf.Req config;
private Map<String, String> config;

private Long requestId;

@@ -58,13 +58,7 @@ public HttpRequest(Req req) {
* @return the config
*/
public String getConfig(PluginFilter filter) {
for (int i = 0; i < config.confLength(); i++) {
TextEntry conf = config.conf(i);
if (conf.name().equals(filter.name())) {
return conf.value();
}
}
return null;
return config.getOrDefault(filter.name(), null);
}

public long getRequestId() {
@@ -274,7 +268,7 @@ public static HttpRequest from(ByteBuffer buffer) {
return new HttpRequest(req);
}

public void initCtx(HttpResponse response, io.github.api7.A6.PrepareConf.Req config) {
public void initCtx(HttpResponse response, Map<String, String> config) {
this.response = response;
this.config = config;
}

0 comments on commit a5ca9ca

Please sign in to comment.