Skip to content

Commit e40c20f

Browse files
committed
增加了分层filter,filter之间线程隔离
1 parent 6928c01 commit e40c20f

25 files changed

+696
-137
lines changed

src/main/java/love/wangqi/codec/DefaultHttpRequestBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import io.netty.handler.codec.http.multipart.InterfaceHttpData;
1010
import org.slf4j.Logger;
1111
import org.slf4j.LoggerFactory;
12-
import love.wangqi.exception.NoRouteException;
12+
import love.wangqi.exception.GatewayNoRouteException;
1313
import love.wangqi.route.Route;
1414
import love.wangqi.route.RouteMapper;
1515

@@ -54,7 +54,7 @@ public RequestHolder build(FullHttpRequest originRequest) throws Exception {
5454
httpRequestDecomposer = new HttpRequestDecomposer(originRequest);
5555
Route route = getRoute(originRequest);
5656
if (route == null) {
57-
throw new NoRouteException();
57+
throw new GatewayNoRouteException();
5858
}
5959
URL url = route.getMapUrl();
6060
// logger.info(url.toString());

src/main/java/love/wangqi/config/GatewayConfig.java

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,49 @@
55
import io.netty.channel.ChannelOutboundHandler;
66
import love.wangqi.codec.HttpRequestBuilder;
77
import love.wangqi.exception.handler.ExceptionHandler;
8-
import love.wangqi.filter.HttpRequestFilter;
8+
import love.wangqi.filter.FilterRegistry;
9+
import love.wangqi.filter.GatewayFilter;
910
import love.wangqi.listener.ChannelCloseFutureListener;
1011
import love.wangqi.route.RouteMapper;
1112

12-
import java.util.ArrayList;
13-
import java.util.List;
13+
import java.util.*;
14+
import java.util.concurrent.ConcurrentHashMap;
1415

1516
/**
1617
* @author: wangqi
1718
* @description:
1819
* @date: Created in 2018/6/4 下午6:49
1920
*/
2021
public class GatewayConfig {
22+
23+
final static GatewayConfig INSTANCE = new GatewayConfig();
24+
25+
private final ConcurrentHashMap<String, List<GatewayFilter>> hashFiltersByType = new ConcurrentHashMap<String, List<GatewayFilter>>();
26+
27+
public static GatewayConfig getInstance() {
28+
return INSTANCE;
29+
}
30+
31+
public List<GatewayFilter> getFiltersByType(String filterType) {
32+
List<GatewayFilter> list = hashFiltersByType.get(filterType);
33+
if (list != null) {
34+
return list;
35+
}
36+
37+
list = new ArrayList<>();
38+
39+
Collection<GatewayFilter> filters = FilterRegistry.instance().getAllFilters();
40+
for (Iterator<GatewayFilter> iterator = filters.iterator(); iterator.hasNext(); ) {
41+
GatewayFilter filter = iterator.next();
42+
if (filter.filterType().equals(filterType)) {
43+
list.add(filter);
44+
}
45+
}
46+
Collections.sort(list);
47+
hashFiltersByType.putIfAbsent(filterType, list);
48+
return list;
49+
}
50+
2151
/**
2252
* 读入流量处理器
2353
*/
@@ -46,21 +76,16 @@ public class GatewayConfig {
4676
* channel关闭监听器
4777
*/
4878
private ChannelCloseFutureListener channelCloseFutureListener;
49-
/**
50-
* HttpRequest过滤器
51-
*/
52-
private List<HttpRequestFilter> httpRequestFilterList;
5379
/**
5480
* 异常处理器
5581
*/
5682
private ExceptionHandler exceptionHandler;
5783

5884

59-
public GatewayConfig() {
85+
private GatewayConfig() {
6086
channelInboundHandlerList = new ArrayList<>();
6187
channelOutboundHandlerList = new ArrayList<>();
6288
httpResponseHandlerList = new ArrayList<>();
63-
httpRequestFilterList = new ArrayList<>();
6489
}
6590

6691
public void addChannelInboundHandler(ChannelInboundHandler channelInboundHandler) {
@@ -119,14 +144,6 @@ public void setChannelCloseFutureListener(ChannelCloseFutureListener channelClos
119144
this.channelCloseFutureListener = channelCloseFutureListener;
120145
}
121146

122-
public void addHttpRequestFilter(HttpRequestFilter httpRequestFilter) {
123-
httpRequestFilterList.add(httpRequestFilter);
124-
}
125-
126-
public List<HttpRequestFilter> getHttpRequestFilterList() {
127-
return httpRequestFilterList;
128-
}
129-
130147
public ExceptionHandler getExceptionHandler() {
131148
return exceptionHandler;
132149
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package love.wangqi.context;
2+
3+
import io.netty.channel.Channel;
4+
import io.netty.channel.ChannelHandlerContext;
5+
import io.netty.handler.codec.http.HttpRequest;
6+
7+
import java.util.concurrent.ConcurrentHashMap;
8+
9+
/**
10+
* @author: wangqi
11+
* @description:
12+
* @date: Created in 2018/9/3 下午7:31
13+
*/
14+
public class HttpRequestContext {
15+
/**
16+
* HttpRequest相关联的数据
17+
*/
18+
private static final ConcurrentHashMap<Channel, ConcurrentHashMap<String, Object>> channelContext = new ConcurrentHashMap<>();
19+
/**
20+
* Channel与HttpRequest的对应关系
21+
*/
22+
private static final ConcurrentHashMap<HttpRequest, Channel> requestChannel = new ConcurrentHashMap<>();
23+
24+
private HttpRequestContext() {}
25+
26+
private static final HttpRequestContext INSTANCE = new HttpRequestContext();
27+
28+
public static HttpRequestContext getInstance() {
29+
return INSTANCE;
30+
}
31+
32+
public void setRequestChannel(HttpRequest httpRequest, Channel channel) {
33+
requestChannel.putIfAbsent(httpRequest, channel);
34+
set(channel, RequestConstant.HTTPREQUEST, httpRequest);
35+
}
36+
37+
public ConcurrentHashMap<String, Object> getContext(Channel channel) {
38+
return channelContext.get(channel);
39+
}
40+
41+
public <T> T get(Channel channel, String key) {
42+
ConcurrentHashMap<String, Object> context = getContext(channel);
43+
return context == null ? null : (T) context.get(key);
44+
}
45+
46+
public void set(Channel channel, String key, Object value) {
47+
ConcurrentHashMap<String, Object> context = getContext(channel);
48+
if (context == null) {
49+
context = new ConcurrentHashMap<>();
50+
channelContext.putIfAbsent(channel, context);
51+
}
52+
context.putIfAbsent(key, value);
53+
}
54+
55+
public <T> T get(HttpRequest httpRequest, String key) {
56+
Channel channel = requestChannel.get(httpRequest);
57+
return channel == null ? null : get(channel, key);
58+
}
59+
60+
public void set(HttpRequest httpRequest, String key, Object value) {
61+
Channel channel = requestChannel.get(httpRequest);
62+
set(channel, key, value);
63+
}
64+
65+
public void remove(HttpRequest httpRequest) {
66+
Channel channel = requestChannel.get(httpRequest);
67+
ConcurrentHashMap<String, Object> map = channelContext.remove(channel);
68+
requestChannel.remove(httpRequest);
69+
}
70+
71+
public void remove(Channel channel) {
72+
ConcurrentHashMap<String, Object> map = channelContext.remove(channel);
73+
requestChannel.remove(map.get(RequestConstant.HTTPREQUEST));
74+
}
75+
76+
public HttpRequest getHttpRequest(Channel channel) {
77+
return get(channel, RequestConstant.HTTPREQUEST);
78+
}
79+
80+
public Channel getChannel(HttpRequest httpRequest) {
81+
return requestChannel.get(httpRequest);
82+
}
83+
84+
public ChannelHandlerContext getChannelHandlerContext(HttpRequest httpRequest) {
85+
return (ChannelHandlerContext) get(httpRequest, RequestConstant.CHANNELHANDLERCONTEXT);
86+
}
87+
88+
public void setChannelHandlerContext(HttpRequest httpRequest, ChannelHandlerContext ctx) {
89+
setRequestChannel(httpRequest, ctx.channel());
90+
set(httpRequest, RequestConstant.CHANNELHANDLERCONTEXT, ctx);
91+
}
92+
93+
public Exception getException(HttpRequest httpRequest) {
94+
return (Exception) get(httpRequest, RequestConstant.EXCEPTION);
95+
}
96+
97+
public void setException(HttpRequest httpRequest, Exception exception) {
98+
set(httpRequest, RequestConstant.EXCEPTION, exception);
99+
}
100+
101+
public void setException(Channel channel, Exception exception) {
102+
set(channel, RequestConstant.EXCEPTION, exception);
103+
}
104+
105+
public void setResponse(Channel channel, Object response) {
106+
set(channel, RequestConstant.RESPONSE, response);
107+
}
108+
109+
public Object getResponse(Channel channel) {
110+
return get(channel, RequestConstant.RESPONSE);
111+
}
112+
113+
public Object getResponse(HttpRequest httpRequest) {
114+
return get(httpRequest, RequestConstant.RESPONSE);
115+
}
116+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package love.wangqi.context;
2+
3+
/**
4+
* @author: wangqi
5+
* @description:
6+
* @date: Created in 2018/9/5 上午11:02
7+
*/
8+
public class RequestConstant {
9+
public static final String CHANNELHANDLERCONTEXT = "channelHandlerContext";
10+
public static final String HTTPREQUEST = "httpRequest";
11+
public static final String EXCEPTION = "exception";
12+
public static final String STATUSCODE = "statusCode";
13+
public static final String RESPONSE = "response";
14+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package love.wangqi.exception;
2+
3+
import io.netty.handler.codec.http.HttpResponseStatus;
4+
5+
/**
6+
* @author: wangqi
7+
* @description:
8+
* @date: Created in 2018/9/3 下午6:39
9+
*/
10+
public class GatewayException extends RuntimeException {
11+
private HttpResponseStatus status;
12+
private String message;
13+
14+
public GatewayException(HttpResponseStatus status, String message) {
15+
super(message);
16+
this.status = status;
17+
this.message = message;
18+
}
19+
20+
public HttpResponseStatus getStatus() {
21+
return status;
22+
}
23+
24+
@Override
25+
public String getMessage() {
26+
return message;
27+
}
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package love.wangqi.exception;
2+
3+
import io.netty.handler.codec.http.HttpResponseStatus;
4+
5+
/**
6+
* @author: wangqi
7+
* @description:
8+
* @date: Created in 2018/6/5 下午7:26
9+
*/
10+
public class GatewayNoRouteException extends GatewayException {
11+
12+
public GatewayNoRouteException() {
13+
super(HttpResponseStatus.NOT_FOUND, "no route found");
14+
}
15+
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package love.wangqi.exception;
2+
3+
import io.netty.handler.codec.http.HttpResponseStatus;
4+
5+
/**
6+
* @author: wangqi
7+
* @description:
8+
* @date: Created in 2018/7/28 09:37
9+
*/
10+
public class GatewayTimeoutException extends GatewayException {
11+
12+
public GatewayTimeoutException() {
13+
super(HttpResponseStatus.REQUEST_TIMEOUT, "timeout");
14+
}
15+
}

src/main/java/love/wangqi/exception/NoRouteException.java

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

src/main/java/love/wangqi/exception/TimeoutException.java

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

src/main/java/love/wangqi/exception/handler/DefaultExceptionHandler.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
import io.netty.handler.codec.http.FullHttpResponse;
77
import io.netty.handler.codec.http.HttpResponseStatus;
88
import io.netty.handler.codec.http.HttpVersion;
9-
import love.wangqi.exception.NoRouteException;
10-
import love.wangqi.exception.TimeoutException;
9+
import love.wangqi.exception.GatewayException;
1110

1211
import java.net.ConnectException;
1312
import java.util.concurrent.RejectedExecutionException;
@@ -21,18 +20,15 @@ public class DefaultExceptionHandler extends AbstractExceptionHandler {
2120
@Override
2221
public ExceptionResponse getExceptionResponse(Exception exception) {
2322
ExceptionResponse exceptionResponse = new ExceptionResponse();
24-
if (exception instanceof NoRouteException) {
25-
exceptionResponse.setStatus(HttpResponseStatus.NOT_FOUND);
23+
if (exception instanceof GatewayException) {
24+
GatewayException gatewayException = (GatewayException) exception;
25+
exceptionResponse.setStatus(gatewayException.getStatus());
2626
exceptionResponse.setContentType("text/plain");
27-
exceptionResponse.setContent(exception.getMessage());
27+
exceptionResponse.setContent(gatewayException.getMessage());
2828
} else if (exception instanceof ConnectException) {
2929
exceptionResponse.setStatus(HttpResponseStatus.NOT_FOUND);
3030
exceptionResponse.setContentType("text/plain");
3131
exceptionResponse.setContent("connect server refused");
32-
} else if (exception instanceof TimeoutException) {
33-
exceptionResponse.setStatus(HttpResponseStatus.REQUEST_TIMEOUT);
34-
exceptionResponse.setContentType("text/plain");
35-
exceptionResponse.setContent("request timeout");
3632
} else if (exception instanceof RejectedExecutionException) {
3733
exceptionResponse.setStatus(HttpResponseStatus.TOO_MANY_REQUESTS);
3834
exceptionResponse.setContentType("text/plain");

0 commit comments

Comments
 (0)