-
Notifications
You must be signed in to change notification settings - Fork 972
/
RequestLimitingHandler.java
139 lines (116 loc) · 4.52 KB
/
RequestLimitingHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.undertow.server.handlers;
import static org.wildfly.common.Assert.checkNotNullParam;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import io.undertow.server.HandlerWrapper;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.handlers.builder.HandlerBuilder;
/**
* A handler which limits the maximum number of concurrent requests. Requests beyond the limit will
* block until the previous request is complete.
*
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
*/
public final class RequestLimitingHandler implements HttpHandler {
private final HttpHandler nextHandler;
private final RequestLimit requestLimit;
/**
* Construct a new instance. The maximum number of concurrent requests must be at least one. The next handler
* must not be {@code null}.
*
* @param maximumConcurrentRequests the maximum concurrent requests
* @param nextHandler the next handler
*/
public RequestLimitingHandler(int maximumConcurrentRequests, HttpHandler nextHandler) {
this(maximumConcurrentRequests, -1, nextHandler);
}
/**
* Construct a new instance. The maximum number of concurrent requests must be at least one. The next handler
* must not be {@code null}.
*
* @param maximumConcurrentRequests the maximum concurrent requests
* @param queueSize the maximum number of requests to queue
* @param nextHandler the next handler
*/
public RequestLimitingHandler(int maximumConcurrentRequests, int queueSize, HttpHandler nextHandler) {
checkNotNullParam("nextHandler", nextHandler);
if (maximumConcurrentRequests < 1) {
throw new IllegalArgumentException("Maximum concurrent requests must be at least 1");
}
this.requestLimit = new RequestLimit(maximumConcurrentRequests, queueSize);
this.nextHandler = nextHandler;
}
/**
* Construct a new instance. This version takes a {@link RequestLimit} directly which may be shared with other
* handlers.
*
* @param requestLimit the request limit information.
* @param nextHandler the next handler
*/
public RequestLimitingHandler(RequestLimit requestLimit, HttpHandler nextHandler) {
checkNotNullParam("nextHandler", nextHandler);
this.requestLimit = requestLimit;
this.nextHandler = nextHandler;
}
public void handleRequest(final HttpServerExchange exchange) throws Exception {
requestLimit.handleRequest(exchange, nextHandler);
}
public RequestLimit getRequestLimit() {
return requestLimit;
}
@Override
public String toString() {
return "request-limit( " + requestLimit.getMaximumConcurrentRequests() + " )";
}
public static class Builder implements HandlerBuilder {
@Override
public String name() {
return "request-limit";
}
@Override
public Map<String, Class<?>> parameters() {
return Collections.<String, Class<?>>singletonMap("requests", int.class);
}
@Override
public Set<String> requiredParameters() {
return Collections.singleton("requests");
}
@Override
public String defaultParameter() {
return "requests";
}
@Override
public HandlerWrapper build(Map<String, Object> config) {
return new Wrapper((Integer) config.get("requests"));
}
}
private static class Wrapper implements HandlerWrapper {
private final int requests;
private Wrapper(int requests) {
this.requests = requests;
}
@Override
public HttpHandler wrap(HttpHandler handler) {
return new RequestLimitingHandler(requests, handler);
}
}
}