// Path: src/main/java/com/stackify/api/common/http/HttpException.java
// public class HttpException extends Exception {
// 
// 	/**
// 	 * Serial version UID
// 	 */
// 	private static final long serialVersionUID = -6349485370575426368L;
// 	
// 	/**
// 	 * HTTP status code
// 	 */
// 	private final int statusCode;
// 	
// 	/**
// 	 * Constructor
// 	 * @param statusCode HTTP status code
// 	 */
// 	public HttpException(int statusCode) {
// 		this.statusCode = statusCode;
// 	}
// 
// 	/**
// 	 * @return the statusCode
// 	 */
// 	public int getStatusCode() {
// 		return statusCode;
// 	}
// 	
// 	/**
// 	 * @return True if 4xx status code
// 	 */
// 	public boolean isClientError() {
// 		return (HttpURLConnection.HTTP_BAD_REQUEST <= statusCode) && (statusCode < HttpURLConnection.HTTP_INTERNAL_ERROR);
// 	}
// }


import java.net.HttpURLConnection;
import com.stackify.api.common.http.HttpException;
/*
 * Copyright 2014 Stackify
 *
 * 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.stackify.api.common.log;



/**
 * LogBackgroundServiceScheduler
 *
 * @author Eric Martin
 */
public class LogBackgroundServiceScheduler {

	/**
	 * One second (milliseconds)
	 */
	private static final long ONE_SECOND = 1000;

	/**
	 * Five seconds (milliseconds)
	 */
	private static final long FIVE_SECONDS = 5000;

	/**
	 * One minute (milliseconds)
	 */
	private static final long ONE_MINUTE = 60000;

	/**
	 * Five minutes (milliseconds)
	 */
	private static final long FIVE_MINUTES = 300000;

	/**
	 * Schedule delay (in milliseconds)
	 */
	private long scheduleDelay = ONE_SECOND;

	/**
	 * UTC timestamp of the HTTP error
	 */
	private long lastHttpError = 0;

	/**
	 * Sets the next scheduled delay based on the number of messages sent
	 * @param numSent The number of log messages sent
	 */
	public void update(final int numSent) {

		// Reset the last HTTP error

		lastHttpError = 0;

		// adjust the schedule delay based on the number of messages sent in the last iteration

		if (100 <= numSent) {

			// messages are coming in quickly so decrease our delay
			// minimum delay is 1 second

			scheduleDelay = Math.max(Math.round(scheduleDelay / 2.0), ONE_SECOND);

		} else if (numSent < 10) {

			// messages are coming in rather slowly so increase our delay
			// maximum delay is 5 seconds

			scheduleDelay = Math.min(Math.round(1.25 * scheduleDelay), FIVE_SECONDS);

		}
	}

	/**
	 * Sets the next scheduled delay based on the HTTP transmission status
	 * @param t The exception
	 */
	public void update(final Throwable t) {

		// see if the exception indicates an authorization problem

		boolean isAuthorized = true;

		if (t instanceof HttpException) {
// Path: src/main/java/com/stackify/api/common/http/HttpException.java
// public class HttpException extends Exception {
// 
// 	/**
// 	 * Serial version UID
// 	 */
// 	private static final long serialVersionUID = -6349485370575426368L;
// 	
// 	/**
// 	 * HTTP status code
// 	 */
// 	private final int statusCode;
// 	
// 	/**
// 	 * Constructor
// 	 * @param statusCode HTTP status code
// 	 */
// 	public HttpException(int statusCode) {
// 		this.statusCode = statusCode;
// 	}
// 
// 	/**
// 	 * @return the statusCode
// 	 */
// 	public int getStatusCode() {
// 		return statusCode;
// 	}
// 	
// 	/**
// 	 * @return True if 4xx status code
// 	 */
// 	public boolean isClientError() {
// 		return (HttpURLConnection.HTTP_BAD_REQUEST <= statusCode) && (statusCode < HttpURLConnection.HTTP_INTERNAL_ERROR);
// 	}
// }


// Path: src/main/java/com/stackify/api/common/log/LogBackgroundServiceScheduler.java
import java.net.HttpURLConnection;
import com.stackify.api.common.http.HttpException;

/*
 * Copyright 2014 Stackify
 *
 * 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.stackify.api.common.log;



/**
 * LogBackgroundServiceScheduler
 *
 * @author Eric Martin
 */
public class LogBackgroundServiceScheduler {

	/**
	 * One second (milliseconds)
	 */
	private static final long ONE_SECOND = 1000;

	/**
	 * Five seconds (milliseconds)
	 */
	private static final long FIVE_SECONDS = 5000;

	/**
	 * One minute (milliseconds)
	 */
	private static final long ONE_MINUTE = 60000;

	/**
	 * Five minutes (milliseconds)
	 */
	private static final long FIVE_MINUTES = 300000;

	/**
	 * Schedule delay (in milliseconds)
	 */
	private long scheduleDelay = ONE_SECOND;

	/**
	 * UTC timestamp of the HTTP error
	 */
	private long lastHttpError = 0;

	/**
	 * Sets the next scheduled delay based on the number of messages sent
	 * @param numSent The number of log messages sent
	 */
	public void update(final int numSent) {

		// Reset the last HTTP error

		lastHttpError = 0;

		// adjust the schedule delay based on the number of messages sent in the last iteration

		if (100 <= numSent) {

			// messages are coming in quickly so decrease our delay
			// minimum delay is 1 second

			scheduleDelay = Math.max(Math.round(scheduleDelay / 2.0), ONE_SECOND);

		} else if (numSent < 10) {

			// messages are coming in rather slowly so increase our delay
			// maximum delay is 5 seconds

			scheduleDelay = Math.min(Math.round(1.25 * scheduleDelay), FIVE_SECONDS);

		}
	}

	/**
	 * Sets the next scheduled delay based on the HTTP transmission status
	 * @param t The exception
	 */
	public void update(final Throwable t) {

		// see if the exception indicates an authorization problem

		boolean isAuthorized = true;

		if (t instanceof HttpException) {
			HttpException httpException = (HttpException) t;