001/*******************************************************************************
002 * Copyright (c) 2006-2014 Nuxeo SA (http://nuxeo.com/) and others.
003 * 
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 ******************************************************************************/
009package org.nuxeo.ecm.core.redis.retry;
010
011import java.util.concurrent.TimeUnit;
012
013import org.apache.commons.logging.LogFactory;
014
015public class SimpleDelay implements Retry.Policy {
016
017    protected final int base;
018
019    protected final long delay;
020
021    protected int elapsed;
022
023    public SimpleDelay(int base, int delay) {
024        this.base = base;
025        this.delay = TimeUnit.MILLISECONDS.convert(delay, TimeUnit.SECONDS);
026    }
027
028    @Override
029    public boolean allow() {
030        return elapsed < delay;
031    }
032
033    @Override
034    public void pause() {
035        long computed = computeDelay();
036        LogFactory.getLog(SimpleDelay.class).warn("pausing for " + computed + " ms");
037        try {
038            Thread.sleep(computed);
039        } catch (InterruptedException e) {
040            Thread.currentThread().interrupt();
041        }
042        elapsed += computed;
043    }
044
045    protected long computeDelay() {
046        return base;
047    }
048
049}