ru.tinkoff.kora.resilient.timeout.TimeoutConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of resilient-kora Show documentation
Show all versions of resilient-kora Show documentation
Kora resilient-kora module
The newest version!
package ru.tinkoff.kora.resilient.timeout;
import jakarta.annotation.Nonnull;
import ru.tinkoff.kora.config.common.annotation.ConfigValueExtractor;
import java.time.Duration;
import java.util.Map;
import java.util.Objects;
@ConfigValueExtractor
public interface TimeoutConfig {
String DEFAULT = "default";
default Map timeout() {
return Map.of();
}
default NamedConfig getNamedConfig(@Nonnull String name) {
Objects.requireNonNull(name);
if (timeout() == null)
throw new IllegalStateException("Timeout no configuration is provided, but either '" + name + "' or '" + DEFAULT + "' config is required");
final NamedConfig defaultConfig = timeout().get(DEFAULT);
final NamedConfig namedConfig = timeout().getOrDefault(name, defaultConfig);
if (namedConfig == null)
throw new IllegalStateException("Timeout no configuration is provided, but either '" + name + "' or '" + DEFAULT + "' config is required");
final NamedConfig mergedConfig = merge(namedConfig, defaultConfig);
if (mergedConfig.duration() == null)
throw new IllegalStateException("Timeout 'duration' is not configured in either '" + name + "' or '" + DEFAULT + "' config");
return mergedConfig;
}
private static NamedConfig merge(NamedConfig namedConfig, NamedConfig defaultConfig) {
if (defaultConfig == null) {
return namedConfig;
}
return new NamedConfig(namedConfig.duration() == null ? defaultConfig.duration() : namedConfig.duration());
}
/**
* {@link #duration} Configures maximum interval for timeout.
*/
@ConfigValueExtractor
record NamedConfig(Duration duration) {}
}