001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.core.recovery; 015 016public class RecoveryCoordinator { 017 018 public final static long BACKOFF_COEFFICIENT_MIN = 20; 019 public final static long BACKOFF_MULTIPLIER = 4; 020 static long BACKOFF_COEFFICIENT_MAX = 327680; // BACKOFF_COEFFICIENT_MIN * 4^7 021 022 private long backOffCoefficient = BACKOFF_COEFFICIENT_MIN; 023 024 private static long UNSET = -1; 025 // tests can set the time directly independently of system clock 026 private long currentTime = UNSET; 027 private long next; 028 029 public RecoveryCoordinator() { 030 next = getCurrentTime() + getBackoffCoefficient(); 031 } 032 033 public RecoveryCoordinator(long currentTime) { 034 this.currentTime = currentTime; 035 next = getCurrentTime() + getBackoffCoefficient(); 036 } 037 038 public boolean isTooSoon() { 039 long now = getCurrentTime(); 040 if (now > next) { 041 next = now + getBackoffCoefficient(); 042 return false; 043 } else { 044 return true; 045 } 046 } 047 048 void setCurrentTime(long forcedTime) { 049 currentTime = forcedTime; 050 } 051 052 private long getCurrentTime() { 053 if (currentTime != UNSET) { 054 return currentTime; 055 } 056 return System.currentTimeMillis(); 057 } 058 059 private long getBackoffCoefficient() { 060 long currentCoeff = backOffCoefficient; 061 if (backOffCoefficient < BACKOFF_COEFFICIENT_MAX) { 062 backOffCoefficient *= BACKOFF_MULTIPLIER; 063 } 064 return currentCoeff; 065 } 066}