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

com.yahoo.bard.webservice.logging.TimeRemainingFunction Maven / Gradle / Ivy

Go to download

Fili web service library provides core capabilities for RESTful aggregation navigation, query planning and metadata

There is a newer version: 1.1.13
Show newest version
// Copyright 2020 Oath Inc.
// Licensed under the terms of the Apache license. Please see LICENSE.md file distributed with this work for terms.
package com.yahoo.bard.webservice.logging;

import com.yahoo.bard.webservice.web.filters.BardLoggingFilter;

import java.util.concurrent.TimeUnit;
import java.util.function.Function;

/**
 * Timeout helper identifies how much time is left given a timeout number.
 */
public class TimeRemainingFunction implements Function {

    public static TimeRemainingFunction INSTANCE = new TimeRemainingFunction();

    /**
     * Calculate timeout remaining based on configured timeout and session start time.
     *
     * @param timeout  The max timeout in milliseconds.
     *
     * @return The time remaining or zero.
     */
    @Override
    public Integer apply(Integer timeout) {
        TimedPhase totalTimer =  RequestLog.fetchTiming(BardLoggingFilter.TOTAL_TIMER);
        long timeSoFarNanos = (totalTimer == null) ? 0 : totalTimer.getActiveDuration();
        return (int) Math.max(timeout - TimeUnit.NANOSECONDS.toMillis(timeSoFarNanos), 0);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy