标签:hashmap except javascrip prot 技术分享 image pil protected 如何
<Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" connectionTimeout="8000" enableLookups="false" acceptorThreadCount="1" URIEncoding="utf-8" redirectPort="443" compression="on" compressionMinSize="1024" compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain,application/json,application/xml" /> <Executor className="com.weibo.api.tomcat.StandardThreadExecutor" name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="500" minSpareThreads="100"/>
/** * 简单的实现基于URL的流控 */ public class TrafficControl { //一个url请求的最大访问数量为300 private final static int ONE_URI_MAX_CONCURRENT = 300; //所有url请求的最大访问数量为500 private final static int ALL_URI_MAX_CONCURRENT = 500; private final static AtomicInteger all_url_concurrent = new AtomicInteger(0); private final static ConcurrentMap<String, AtomicInteger> url_concurrent_map = new ConcurrentHashMap<String, AtomicInteger>(); private final static SwitcherManager switcherManager = SwitcherManagerFactoryLoader.getSwitcherManagerFactory().getSwitcherManager(); private final static Switcher tcEnabled = switcherManager.registerSwitcher("feature.trackurl.traffic_control.enable", true); public static boolean isDisabled() { return tcEnabled.isClose(); } public static boolean isOverflow(String uri) { if (all_url_concurrent.get() > ALL_URI_MAX_CONCURRENT) { return true; } AtomicInteger one_url_concurrent = url_concurrent_map.get(uri); if (one_url_concurrent != null && one_url_concurrent.get() > ONE_URI_MAX_CONCURRENT) { return true; } return false; } public static void startAccess(String uri) { all_url_concurrent.incrementAndGet(); AtomicInteger one_url_concurrent = url_concurrent_map.get(uri); if (one_url_concurrent != null) { one_url_concurrent.incrementAndGet(); } else { url_concurrent_map.putIfAbsent(uri, new AtomicInteger(1)); } } public static void endAccess(String uri) { all_url_concurrent.decrementAndGet(); AtomicInteger one_url_concurrent = url_concurrent_map.get(uri); if (one_url_concurrent != null) { one_url_concurrent.decrementAndGet(); } } public static void dumpWarnLog() { String lineSeparator = System.getProperty("line.separator"); // 估算每一个URL和其计数占用32个字符 StringBuilder sb = new StringBuilder((1 + url_concurrent_map.size()) * 32); sb.append("all_url_concurrent : ").append(all_url_concurrent); for (Map.Entry<String, AtomicInteger> entry : url_concurrent_map.entrySet()) { sb.append(lineSeparator).append(‘\t‘).append(entry.getKey()).append(" : ").append(entry.getValue()); } ApiLogger.warn(sb); } }
/** * 带有流量控制的Servlet */ public class TrafficControlServlet extends HttpServlet { @Override protected final void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (TrafficControl.isDisabled()) { super.service(req, resp); return; } String uri = req.getRequestURI(); if (TrafficControl.isOverflow(uri)) { TrafficControl.dumpWarnLog(); resp.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE); return; } try { TrafficControl.startAccess(uri); super.service(req, resp); } finally { TrafficControl.endAccess(uri); } } }
public class TestServlet extends TrafficControlServlet { private static final long serialVersionUID = 2895590140869067830L; @Override protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException { //................. } @Override protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws IOException { doGet(request, response); }
标签:hashmap except javascrip prot 技术分享 image pil protected 如何
原文地址:http://www.cnblogs.com/ficohu/p/6819292.html