I have a simple filter defined in web.xml like this. 

    <filter>

    <filter-name>ThrottlingFilter</filter-name>

        <filter-class>com.xyz.filters.ThrottlingFilter</filter-class>

    </filter>

    <filter-mapping>

        <filter-name>ThrottlingFilter</filter-name>

        <url-pattern>/*</url-pattern>

    </filter-mapping>

and my code looks like below. In my test below, i wanted to allow only 2 
simultaneous connections to my page. I put an artificial sleep in my 
servlets and made 5 concurrent requests but the filter logic doesn't kick 
in. The connectionCount seems to be 0 for all the servlet requests. Can 
anyone suggest what's wrong? 


public class ThrottlingFilter implements Filter

{

   private static final String 
CONNECTION_NOT_ALLOWED_REACHED_MAXIMUM_NUMBER_OF_CONNECTIONS =

   "Connection not allowed: reached maximum number of connections";


   private static final Logger log = Logger.getLogger(ThrottlingFilter.class
.getName());

   private int connectionCount = 0;

   private int max_connection_count = 2;


   public void destroy()

   {}


   public void init(FilterConfig config)

   {}

   

   @Override

   public void doFilter(ServletRequest request, ServletResponse response, 
FilterChain chain)

   throws IOException, ServletException

   {

      final boolean allowed;

      final HttpServletRequest httpRequest = (HttpServletRequest) request;

      final HttpServletResponse httpResponse = (HttpServletResponse) 
response;

      

      synchronized (this)

      {

         allowed = connectionCount < max_connection_count;

         log.info("connectionCount = " + connectionCount + "; 
max_connection_count = " + max_connection_count);


         if (allowed)

         {

            ++connectionCount;

            log.info("connectionCount = " + connectionCount + "; in allowed 
block");            

         }

      }

      if (!allowed) {

      log.info(CONNECTION_NOT_ALLOWED_REACHED_MAXIMUM_NUMBER_OF_CONNECTIONS
);

      httpResponse.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, 
CONNECTION_NOT_ALLOWED_REACHED_MAXIMUM_NUMBER_OF_CONNECTIONS);

      return;

      }


      try

      {

         chain.doFilter(httpRequest, httpResponse);

      }

      finally

      {

         synchronized (this)

         {

            if (--connectionCount < 0)

            {

               connectionCount = 0;

            }

            log.info("connectionCount = " + connectionCount + "; in finally 
block");

         }

      }

   }

}

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/google-appengine-java/-/ogFIUgw2wlEJ.
To post to this group, send email to google-appengine-java@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to