All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.khthink.easyapi.kit.communication.EasyRequestKit Maven / Gradle / Ivy

The newest version!
package cn.khthink.easyapi.kit.communication;

import cn.khthink.easyapi.bean.Request;
import cn.khthink.easyapi.config.CoreConfig;
import cn.khthink.easyapi.kit.EasyLogger;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 请求处理组件
 *
 * @author kh
 */
public class EasyRequestKit {
    private static Map map;

    private static class RequestKit {
        private static EasyRequestKit easyRequestKit = new EasyRequestKit();
    }

    public static EasyRequestKit getInstance() {
        return RequestKit.easyRequestKit;
    }

    /**
     * 初始化
     */
    public void init() {
        map = new ConcurrentHashMap<>(10000);
        EasyLogger.info("--->接口限制访问间隔:" + CoreConfig.limit + "ms");
    }

    /**
     * 添加请求
     *
     * @param request 请求
     */
    public static void addRequest(Request request) {
        String req = request.getRemoteip() + request.getMethod() + request.getUriInfo() + request.getDatas();
        map.put(req, request.getRequesttime());
    }

    /**
     * 是否被限制
     *
     * @param request 请求
     * @return boolean
     */
    public static boolean isLimit(Request request) {
        String req = request.getRemoteip() + request.getMethod() + request.getUriInfo() + request.getDatas();
        Long time = null;
        for (Map.Entry entry : map.entrySet()) {
            if (entry.getKey().equals(req)) {
                time = entry.getValue();
                break;
            }
        }
        if (time != null) {
            if (request.getRequesttime() - time < CoreConfig.limit) {
                return false;
            } else {
                map.remove(req);
            }
        }
        return true;
    }

    /**
     * 销毁
     */
    public void destory() {
        map.clear();
        map = null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy