001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.filters;
030:
031: import com.caucho.log.Log;
032: import com.caucho.util.IntMap;
033: import com.caucho.util.L10N;
034:
035: import javax.servlet.Filter;
036: import javax.servlet.FilterChain;
037: import javax.servlet.FilterConfig;
038: import javax.servlet.ServletException;
039: import javax.servlet.ServletRequest;
040: import javax.servlet.ServletResponse;
041: import javax.servlet.http.HttpServletResponse;
042: import java.io.IOException;
043: import java.util.logging.Logger;
044:
045: /**
046: * Throttles the filter to only a limited number of requests.
047: */
048: public class ThrottleFilter implements Filter {
049: private static final L10N L = new L10N(ThrottleFilter.class);
050: private static final Logger log = Log.open(ThrottleFilter.class);
051:
052: private IntMap _throttleCache = new IntMap();
053:
054: private int _maxConcurrentRequests = 2;
055:
056: /**
057: * Sets the maximum number of concurrent requests for a single IP.
058: */
059: public void setMaxConcurrentRequests(int max) {
060: _maxConcurrentRequests = max;
061: }
062:
063: public void init(FilterConfig config) throws ServletException {
064: }
065:
066: public void doFilter(ServletRequest request,
067: ServletResponse response, FilterChain nextFilter)
068: throws ServletException, IOException {
069: String ip = request.getRemoteAddr();
070: boolean isOverflow;
071:
072: synchronized (this ) {
073: int count = _throttleCache.get(ip);
074:
075: if (count <= 0)
076: count = 0;
077:
078: if (count < _maxConcurrentRequests) {
079: isOverflow = false;
080: _throttleCache.put(ip, count + 1);
081: } else
082: isOverflow = true;
083: }
084:
085: if (isOverflow) {
086: log
087: .info(L
088: .l(
089: "'{0}' has too many concurrent requests -- throttling.",
090: ip));
091:
092: if (response instanceof HttpServletResponse)
093: ((HttpServletResponse) response).sendError(503);
094: return;
095: }
096:
097: try {
098: nextFilter.doFilter(request, response);
099: } finally {
100: synchronized (this ) {
101: int count = _throttleCache.get(ip);
102:
103: if (count <= 1)
104: _throttleCache.remove(ip);
105: else
106: _throttleCache.put(ip, count - 1);
107: }
108: }
109: }
110:
111: /**
112: * Any cleanup for the filter.
113: */
114: public void destroy() {
115: }
116: }
|