siaorg / sia-gateway

微服务路由网关(zuul-plus)
Apache License 2.0
791 stars 345 forks source link

StatisticServiceImpl的统计计数方法线程安全问题 #23

Closed jiangcaijun closed 4 years ago

jiangcaijun commented 5 years ago

com.creditease.gateway.service.impl包下的StatisticServiceImpl类的 increament方法,如下:


    /**
     * 计数操作
     * 
     * */
    @Override
    public void increament(String counterName)
    {
        Integer value = map.get(counterName);

        if(value!=null)
        {
            Integer added = Integer.sum(value.intValue(), 1);

            map.put(counterName, added);

        }else
        {
            map.put(counterName, 1);
        }
    }

该方法会存在线程安全问题:在qps比较高的时候,最后统计出的误差确实比较大。 但是解决方案上,也不能贸然直接 atomicinteger,否则在qps高的时候,cas自旋也会耗时较久。

TeddiWolf commented 5 years ago

已经修正使用LongAddr