package com.atlassian.bitbucket.mesh.throttle;

import javax.annotation.Nonnull;
import java.util.OptionalLong;
import java.util.concurrent.TimeUnit;

/**
 * Strategy for updating a {@link SemaphoreTicketBucket bucket}.
 */
interface ResourceThrottleStrategy {

    @Nonnull
    SemaphoreTicketBucket create();

    /**
     * @param timeUnit the time unit
     * @return the time interval at which the {@link SemaphoreTicketBucket bucket} should be updated. Implementors
     *         should return {@link OptionalLong#empty()} if the bucket does not have to be updated.
     */
    @Nonnull
    OptionalLong getUpdateInterval(@Nonnull TimeUnit timeUnit);

    /**
     * Called when the {@link SemaphoreTicketBucket bucket} should be updated. This method is called at regular
     * {@link #getUpdateInterval(TimeUnit) intervals} on a best effort basis. Implementors should make sure that they
     * can cope with being called slightly earlier/later.
     *
     * @param bucket the bucket that should be updated.
     */
    void update(@Nonnull SemaphoreTicketBucket bucket);
}
