/*
 * Copyright 2012-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.repository;

import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.Policy;
import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.Rate;
import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.RateLimiter;

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

/**
 * In memory rate limiter configuration for dev environment.
 *
 * @author Marcos Barbero
 * @since 2017-06-23
 */
public class InMemoryRateLimiter implements RateLimiter {

    private Map<String, Rate> repository = new ConcurrentHashMap<>();

    @Override
    public synchronized Rate consume(final Policy policy, final String key) {
        Rate rate = this.create(policy, key);
        this.update(rate);
        this.repository.put(key, rate);
        return rate;
    }

    private void update(Rate rate) {
        if (rate.getReset() > 0) {
            Long reset = rate.getExpiration().getTime() - System.currentTimeMillis();
            rate.setReset(reset);
        }
        rate.setRemaining(rate.getRemaining() - 1);
    }

    private Rate create(Policy policy, String key) {
        Rate rate = this.repository.get(key);
        if (isExpired(rate)) {
            rate = new Rate();

            final Long limit = policy.getLimit();
            final Long refreshInterval = policy.getRefreshInterval();
            final Date expiration = new Date(System.currentTimeMillis() + (refreshInterval * 1000L));

            rate.setExpiration(expiration);
            rate.setLimit(limit);
            rate.setRemaining(limit);
            rate.setReset(refreshInterval);
        }
        return rate;
    }

    private boolean isExpired(Rate rate) {
        return rate == null || (rate.getExpiration().getTime() < System.currentTimeMillis());
    }
}

@@ -1018,16 +1018,70 @@
 shMap;%0A%0A
+import static java.util.concurrent.TimeUnit.SECONDS;%0A%0A
 /**%0A * I
@@ -1782,16 +1782,29 @@
 maining(
+Math.max(-1, 
 rate.get
@@ -1819,16 +1819,17 @@
 g() - 1)
+)
 ;%0A    %7D%0A
@@ -2082,16 +2082,33 @@
 erval = 
+SECONDS.toMillis(
 policy.g
@@ -2126,16 +2126,17 @@
 terval()
+)
 ;%0A      
@@ -2203,17 +2203,16 @@
 lis() + 
-(
 refreshI
@@ -2222,17 +2222,8 @@
 rval
- * 1000L)
 );%0A%0A

Fix use millis instead of seconds