KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > filters > ThrottleFilter


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.filters;
30
31 import com.caucho.log.Log;
32 import com.caucho.util.IntMap;
33 import com.caucho.util.L10N;
34
35 import javax.servlet.Filter JavaDoc;
36 import javax.servlet.FilterChain JavaDoc;
37 import javax.servlet.FilterConfig JavaDoc;
38 import javax.servlet.ServletException JavaDoc;
39 import javax.servlet.ServletRequest JavaDoc;
40 import javax.servlet.ServletResponse JavaDoc;
41 import javax.servlet.http.HttpServletResponse JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.util.logging.Logger JavaDoc;
44
45 /**
46  * Throttles the filter to only a limited number of requests.
47  */

48 public class ThrottleFilter implements Filter JavaDoc {
49   private static final L10N L = new L10N(ThrottleFilter.class);
50   private static final Logger JavaDoc log = Log.open(ThrottleFilter.class);
51
52   private IntMap _throttleCache = new IntMap();
53
54   private int _maxConcurrentRequests = 2;
55
56   /**
57    * Sets the maximum number of concurrent requests for a single IP.
58    */

59   public void setMaxConcurrentRequests(int max)
60   {
61     _maxConcurrentRequests = max;
62   }
63
64   public void init(FilterConfig JavaDoc config)
65     throws ServletException JavaDoc
66   {
67   }
68
69   public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response,
70                        FilterChain JavaDoc nextFilter)
71     throws ServletException JavaDoc, IOException JavaDoc
72   {
73     String JavaDoc ip = request.getRemoteAddr();
74     boolean isOverflow;
75
76     synchronized (this) {
77       int count = _throttleCache.get(ip);
78
79       if (count <= 0)
80     count = 0;
81
82       if (count < _maxConcurrentRequests) {
83     isOverflow = false;
84     _throttleCache.put(ip, count + 1);
85       }
86       else
87     isOverflow = true;
88     }
89
90     if (isOverflow) {
91       log.info(L.l("'{0}' has too many concurrent requests -- throttling.",
92            ip));
93
94       if (response instanceof HttpServletResponse JavaDoc)
95     ((HttpServletResponse JavaDoc) response).sendError(503);
96       return;
97     }
98
99     try {
100       nextFilter.doFilter(request, response);
101     } finally {
102       synchronized (this) {
103     int count = _throttleCache.get(ip);
104
105     if (count <= 1)
106       _throttleCache.remove(ip);
107     else
108       _throttleCache.put(ip, count - 1);
109       }
110     }
111   }
112
113   /**
114    * Any cleanup for the filter.
115    */

116   public void destroy()
117   {
118   }
119 }
120
Popular Tags