All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.azure.cosmos.implementation.directconnectivity.TimeoutHelper Maven / Gradle / Ivy

Go to download

This Package contains Microsoft Azure Cosmos SDK (with Reactive Extension Reactor support) for Azure Cosmos DB SQL API

There is a newer version: 4.61.1
Show newest version
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.cosmos.implementation.directconnectivity;

import com.azure.cosmos.implementation.GoneException;
import com.azure.cosmos.implementation.RequestTimeoutException;

import java.time.Duration;
import java.time.Instant;

public class TimeoutHelper {
    private final Instant startTime;
    private final Duration timeOut;

    public TimeoutHelper(Duration timeOut) {
        this.startTime = Instant.now();
        this.timeOut = timeOut;
    }

    public boolean isElapsed() {
        Duration elapsed = Duration.ofMillis(Instant.now().toEpochMilli() - startTime.toEpochMilli());
        return elapsed.compareTo(this.timeOut) >= 0;
    }

    public Duration getRemainingTime() {
        Duration elapsed = Duration.ofMillis(Instant.now().toEpochMilli() - startTime.toEpochMilli());
        return this.timeOut.minus(elapsed);
    }

    public void throwTimeoutIfElapsed() throws RequestTimeoutException {
        if (this.isElapsed()) {
            throw new RequestTimeoutException();
        }
    }

    public void throwGoneIfElapsed() throws GoneException {
        if (this.isElapsed()) {
            throw new GoneException();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy