Skip to content

Commit

Permalink
Simplified (1 to 1) server-to-routes mapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Mar 19, 2017
1 parent 55dfd9c commit 5319733
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 183 deletions.
1 change: 0 additions & 1 deletion rapidoid-commons/src/main/resources/rapidoid-classes.txt
Expand Up @@ -439,7 +439,6 @@ org.rapidoid.http.HttpReq
org.rapidoid.http.HttpResp
org.rapidoid.http.HttpResponseCodes
org.rapidoid.http.HttpRoutes
org.rapidoid.http.HttpRoutesGroup
org.rapidoid.http.HttpStatus
org.rapidoid.http.HttpUtils
org.rapidoid.http.HttpVerb
Expand Down
104 changes: 43 additions & 61 deletions rapidoid-http-fast/src/main/java/org/rapidoid/http/FastHttp.java
Expand Up @@ -58,17 +58,17 @@ public class FastHttp extends AbstractHttpProcessor {

private static final byte[] BUILT_IN_RES_PATH = "/_rapidoid/".getBytes();

private final HttpRoutesGroup routesGroup;
private final HttpRoutesImpl routes;

private final Map<String, Object> attributes = Coll.synchronizedMap();

public FastHttp(HttpRoutesImpl... routeGroups) {
this(new HttpRoutesGroup(routeGroups), new ConfigImpl());
public FastHttp(HttpRoutesImpl routes) {
this(routes, new ConfigImpl());
}

public FastHttp(HttpRoutesGroup routesGroup, Config serverConfig) {
public FastHttp(HttpRoutesImpl routes, @SuppressWarnings("unused") Config serverConfig) {
super(null);
this.routesGroup = routesGroup;
this.routes = routes;
}

@Override
Expand All @@ -94,39 +94,26 @@ public void onRequest(Channel channel, RapidoidHelper data) {

HttpStatus status = HttpStatus.NOT_FOUND;

HttpRoutesImpl matchingRoutes = null;
Route matchingRoute = null;
HandlerMatch match = null;
HandlerMatch match;

if (isGet && shouldServeBuiltInResources(buf, path)) {
for (HttpRoutesImpl r : routesGroup.all()) {
match = r.builtInResourcesHandler();
if (match != null) {
matchingRoutes = r;
matchingRoute = match.getRoute();
break;
}
match = routes.builtInResourcesHandler();
if (match != null) {
matchingRoute = match.getRoute();
}

} else {

for (HttpRoutesImpl r : routesGroup.all()) {
match = r.findHandler(buf, isGet, verb, path);
if (match != null) {
matchingRoutes = r;
matchingRoute = match.getRoute();
break;
}
match = routes.findHandler(buf, isGet, verb, path);
if (match != null) {
matchingRoute = match.getRoute();
}

if (match == null && isGet) {
for (HttpRoutesImpl r : routesGroup.all()) {
match = r.staticResourcesHandler();
if (match != null) {
matchingRoutes = r;
matchingRoute = match.getRoute();
break;
}
match = routes.staticResourcesHandler();
if (match != null) {
matchingRoute = match.getRoute();
}
}
}
Expand All @@ -137,7 +124,7 @@ public void onRequest(Channel channel, RapidoidHelper data) {
ReqImpl req = null;

if (!noReq) {
req = createReq(channel, isGet, isKeepAlive, data, buf, matchingRoutes, matchingRoute, match, handler);
req = createReq(channel, isGet, isKeepAlive, data, buf, matchingRoute, match, handler);

if (serveFromCache(req)) return;
}
Expand Down Expand Up @@ -173,7 +160,7 @@ private boolean shouldServeBuiltInResources(Buf buf, BufRange path) {

@Override
public void waitToInitialize() {
routesGroup.waitToInitialize();
routes.waitToStabilize();
}

private boolean serveFromCache(ReqImpl req) {
Expand Down Expand Up @@ -217,7 +204,7 @@ private void serveCached(ReqImpl req, CachedResp resp) {

@SuppressWarnings("unchecked")
public ReqImpl createReq(Channel channel, boolean isGet, boolean isKeepAlive,
RapidoidHelper helper, Buf buf, HttpRoutesImpl matchingRoutes,
RapidoidHelper helper, Buf buf,
Route matchingRoute, HandlerMatch match, HttpHandler handler) {

ReqImpl req;
Expand Down Expand Up @@ -281,7 +268,7 @@ public ReqImpl createReq(Channel channel, boolean isGet, boolean isKeepAlive,
cookies = Collections.synchronizedMap(cookies);

req = new ReqImpl(this, channel, isKeepAlive, verb, uri, path, query, body, params, headers, cookies,
posted, files, pendingBodyParsing, contentType, zone, matchingRoutes, matchingRoute);
posted, files, pendingBodyParsing, contentType, zone, matchingRoute);

if (!attributes.isEmpty()) {
req.attrs().putAll(attributes);
Expand Down Expand Up @@ -333,7 +320,7 @@ private void handleNotFound(Channel channel, boolean isKeepAlive, Req req) {
}

public Customization custom() {
return routesGroup.customization();
return routes.custom();
}

private String validateRequest(Buf input, BufRange verb, BufRange uri) {
Expand All @@ -349,17 +336,15 @@ private String validateRequest(Buf input, BufRange verb, BufRange uri) {
}

private HttpStatus tryGenericHandlers(Channel channel, boolean isKeepAlive, ReqImpl req) {
for (HttpRoutesImpl routes : routesGroup.all()) {

// trying with different routes
req.routes(routes);
// trying with the routes
req.routes(routes);

for (HttpHandler handler : routes.genericHandlers()) {
HttpStatus status = handleIfFound(channel, isKeepAlive, handler, req);
for (HttpHandler handler : routes.genericHandlers()) {
HttpStatus status = handleIfFound(channel, isKeepAlive, handler, req);

if (status != HttpStatus.NOT_FOUND) {
return status;
}
if (status != HttpStatus.NOT_FOUND) {
return status;
}
}

Expand All @@ -369,31 +354,28 @@ private HttpStatus tryGenericHandlers(Channel channel, boolean isKeepAlive, ReqI
}

public synchronized void resetConfig() {
routesGroup.reset();
routes.reset();
}

public void notFound(Channel ctx, boolean isKeepAlive, MediaType contentType, HttpHandler fromHandler, Req req) {
HttpStatus status = HttpStatus.NOT_FOUND;

tryRoutes:
for (HttpRoutesImpl route : routesGroup.all()) {
List<HttpHandler> genericHandlers = route.genericHandlers();
int count = genericHandlers.size();
List<HttpHandler> genericHandlers = routes.genericHandlers();
int count = genericHandlers.size();

for (int i = 0; i < count; i++) {
HttpHandler handler = genericHandlers.get(i);
if (handler == fromHandler) {
// a generic handler returned "not found" -> go to the next one
if (i < count - 1) {
// trying with different routes
ReqImpl reqi = (ReqImpl) req;
reqi.routes(route);
for (int i = 0; i < count; i++) {
HttpHandler handler = genericHandlers.get(i);
if (handler == fromHandler) {
// a generic handler returned "not found" -> go to the next one
if (i < count - 1) {
// trying with different routes
ReqImpl reqi = (ReqImpl) req;
reqi.routes(routes);

HttpHandler nextHandler = genericHandlers.get(i + 1);
status = handleIfFound(ctx, isKeepAlive, nextHandler, reqi);
HttpHandler nextHandler = genericHandlers.get(i + 1);
status = handleIfFound(ctx, isKeepAlive, nextHandler, reqi);

break tryRoutes;
}
break;
}
}
}
Expand All @@ -407,12 +389,12 @@ public Map<String, Object> attributes() {
return attributes;
}

public HttpRoutesGroup routes() {
return routesGroup;
public HttpRoutesImpl routes() {
return routes;
}

public boolean hasRouteOrResource(HttpVerb verb, String uri) {
return routesGroup.hasRouteOrResource(verb, uri);
return routes.hasRouteOrResource(verb, uri);
}

}

This file was deleted.

Expand Up @@ -13,6 +13,7 @@
import org.rapidoid.commons.Err;
import org.rapidoid.commons.Str;
import org.rapidoid.data.BufRange;
import org.rapidoid.env.Env;
import org.rapidoid.http.*;
import org.rapidoid.http.customize.Customization;
import org.rapidoid.http.handler.HttpHandler;
Expand All @@ -24,6 +25,7 @@
import org.rapidoid.u.U;
import org.rapidoid.util.AnsiColor;
import org.rapidoid.util.Constants;
import org.rapidoid.util.Msc;

import java.util.*;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -52,6 +54,8 @@
@Since("5.1.0")
public class HttpRoutesImpl extends RapidoidThing implements HttpRoutes {

private static final int ROUTE_SETUP_WAITING_TIME_MS = Env.test() ? 300 : 500;

private static final Pattern PATTERN_PATTERN = Pattern.compile("[^\\w/-]");

private static final byte[] _POST = Constants.POST.getBytes();
Expand Down Expand Up @@ -98,6 +102,7 @@ public class HttpRoutesImpl extends RapidoidThing implements HttpRoutes {
private volatile boolean initialized;
private volatile Runnable onInit;

private volatile boolean stable;
private volatile Date lastChangedAt = new Date();

public HttpRoutesImpl(Customization customization) {
Expand Down Expand Up @@ -544,6 +549,10 @@ public synchronized void reset() {
initialized = false;
onInit = null;

customization.reset();
stable = false;
lastChangedAt = null;

notifyChanged();
}

Expand Down Expand Up @@ -656,4 +665,24 @@ public Date lastChangedAt() {
private void notifyChanged() {
lastChangedAt = new Date();
}

public boolean ready() {
long lastChangedAt = lastChangedAt().getTime();
return !isEmpty() && Msc.timedOut(lastChangedAt, ROUTE_SETUP_WAITING_TIME_MS);
}

public void waitToStabilize() {
while (!stable) {
U.sleep(1);
if (ready()) {
synchronized (this) {
if (!stable) {
stable = true;
Log.debug("Stabilized HTTP routes");
}
}
}
}
}

}

0 comments on commit 5319733

Please sign in to comment.