@Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler) throws ExecutionException {
        // tenant-level rate limit
        if (trafficConfiguration.isTenantSwitch()) {
            String token = request.getHeader("token");
            if (!StringUtils.isEmpty(token)) {
                RateLimiter tenantRateLimiter = tenantRateLimiterCache.get(token);
                if (!tenantRateLimiter.tryAcquire()) {
                    response.setStatus(HttpStatus.TOO_MANY_REQUESTS.value());
                    log.warn("Too many request, reach tenant rate limit, current tenant:{} qps is {}", token,
                            tenantRateLimiter.getRate());
                    return false;
                }
            }
        }
        // global rate limit
        if (trafficConfiguration.isGlobalSwitch()) {
            if (!globalRateLimiter.tryAcquire()) {
                response.setStatus(HttpStatus.TOO_MANY_REQUESTS.value());
                log.warn("Too many request, reach global rate limit, current qps is {}",
                        globalRateLimiter.getRate());
                return false;
            }
        }
        return true;
    }
@Test
    public void testPreHandleWithTenantLevenControl() throws ExecutionException {
        ApiConfig.TrafficConfiguration trafficConfiguration = new ApiConfig.TrafficConfiguration();
        trafficConfiguration.setTenantSwitch(true);
        Map<String, Integer> map = new HashMap<>();
        map.put("tenant1", 2);
        map.put("tenant2", 2);
        trafficConfiguration.setCustomizeTenantQpsRate(map);
        trafficConfiguration.setDefaultTenantQpsRate(4);
        RateLimitInterceptor rateLimitInterceptor = new RateLimitInterceptor(trafficConfiguration);

        HttpServletRequest tenant1Request = Mockito.mock(HttpServletRequest.class);
        HttpServletRequest tenant2Request = Mockito.mock(HttpServletRequest.class);
        Mockito.when(tenant1Request.getHeader(Mockito.any())).thenReturn("tenant1");
        Mockito.when(tenant2Request.getHeader(Mockito.any())).thenReturn("tenant2");
        HttpServletResponse response = Mockito.mock(HttpServletResponse.class);

        for (int i = 0; i < 2; i++) {
            rateLimitInterceptor.preHandle(tenant1Request, response, null);
        }
        Assertions.assertFalse(rateLimitInterceptor.preHandle(tenant1Request, response, null));
        Assertions.assertTrue(rateLimitInterceptor.preHandle(tenant2Request, response, null));
    }