From 5e30299dc7cb8d8853391f3caf41861a4c13ccae Mon Sep 17 00:00:00 2001 From: Jan Brennenstuhl Date: Mon, 18 Jan 2021 09:23:56 +0100 Subject: [PATCH] #1040 introduce call timeout --- .../DefaultRiptideRegistrar.java | 25 +++++++++++++++---- .../riptide/autoconfigure/Defaulting.java | 3 ++- .../autoconfigure/FailsafePluginFactory.java | 14 ++++++++++- .../autoconfigure/RiptideProperties.java | 3 ++- 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/riptide-spring-boot-autoconfigure/src/main/java/org/zalando/riptide/autoconfigure/DefaultRiptideRegistrar.java b/riptide-spring-boot-autoconfigure/src/main/java/org/zalando/riptide/autoconfigure/DefaultRiptideRegistrar.java index 888bf8970..1e5e4d4d2 100644 --- a/riptide-spring-boot-autoconfigure/src/main/java/org/zalando/riptide/autoconfigure/DefaultRiptideRegistrar.java +++ b/riptide-spring-boot-autoconfigure/src/main/java/org/zalando/riptide/autoconfigure/DefaultRiptideRegistrar.java @@ -248,6 +248,7 @@ private String findObjectMapper(final String id) { private List registerPlugins(final String id, final Client client) { final Stream> plugins = Stream.of( registerChaosPlugin(id, client), + registerCallTimeoutFailsafePlugin(id, client), registerMicrometerPlugin(id, client), registerRequestCompressionPlugin(id, client), registerLogbookPlugin(id, client), @@ -256,7 +257,7 @@ private List registerPlugins(final String id, final Client client registerRetryPolicyFailsafePlugin(id, client), registerAuthorizationPlugin(id, client), registerBackupRequestFailsafePlugin(id, client), - registerTimeoutFailsafePlugin(id, client), + registerGlobalTimeoutFailsafePlugin(id, client), registerOriginalStackTracePlugin(id, client), registerCustomPlugin(id)); @@ -453,12 +454,26 @@ private Optional registerBackupRequestFailsafePlugin(final String id, fi return Optional.empty(); } - private Optional registerTimeoutFailsafePlugin(final String id, final Client client) { - if (client.getTimeouts().getEnabled()) { + private Optional registerCallTimeoutFailsafePlugin(final String id, final Client client) { + if (client.getTimeouts().getEnabled() && client.getTimeouts().getCall() != null) { final String pluginId = registry.registerIfAbsent(name(id, Timeout.class, FailsafePlugin.class), () -> { - log.debug("Client [{}]: Registering [TimeoutFailsafePlugin]", id); + log.debug("Client [{}]: Registering [CallTimeoutFailsafePlugin]", id); return genericBeanDefinition(FailsafePluginFactory.class) - .setFactoryMethod("createTimeoutPlugin") + .setFactoryMethod("createCallTimeoutPlugin") + .addConstructorArgValue(client) + .addConstructorArgValue(createTaskDecorator(id, client)); + }); + return Optional.of(pluginId); + } + return Optional.empty(); + } + + private Optional registerGlobalTimeoutFailsafePlugin(final String id, final Client client) { + if (client.getTimeouts().getEnabled() && client.getTimeouts().getGlobal() != null) { + final String pluginId = registry.registerIfAbsent(name(id, Timeout.class, FailsafePlugin.class), () -> { + log.debug("Client [{}]: Registering [GlobalTimeoutFailsafePlugin]", id); + return genericBeanDefinition(FailsafePluginFactory.class) + .setFactoryMethod("createGlobalTimeoutPlugin") .addConstructorArgValue(client) .addConstructorArgValue(createTaskDecorator(id, client)); }); diff --git a/riptide-spring-boot-autoconfigure/src/main/java/org/zalando/riptide/autoconfigure/Defaulting.java b/riptide-spring-boot-autoconfigure/src/main/java/org/zalando/riptide/autoconfigure/Defaulting.java index aa8fa96ff..a8d2f3bc9 100644 --- a/riptide-spring-boot-autoconfigure/src/main/java/org/zalando/riptide/autoconfigure/Defaulting.java +++ b/riptide-spring-boot-autoconfigure/src/main/java/org/zalando/riptide/autoconfigure/Defaulting.java @@ -230,7 +230,8 @@ private static BackupRequest merge(final BackupRequest base, final BackupRequest private static Timeouts merge(final Timeouts base, final Timeouts defaults) { return new Timeouts( either(base.getEnabled(), defaults.getEnabled()), - either(base.getGlobal(), defaults.getGlobal()) + either(base.getGlobal(), defaults.getGlobal()), + either(base.getCall(), defaults.getCall()) ); } diff --git a/riptide-spring-boot-autoconfigure/src/main/java/org/zalando/riptide/autoconfigure/FailsafePluginFactory.java b/riptide-spring-boot-autoconfigure/src/main/java/org/zalando/riptide/autoconfigure/FailsafePluginFactory.java index 4f687e5b6..3e2dc5206 100644 --- a/riptide-spring-boot-autoconfigure/src/main/java/org/zalando/riptide/autoconfigure/FailsafePluginFactory.java +++ b/riptide-spring-boot-autoconfigure/src/main/java/org/zalando/riptide/autoconfigure/FailsafePluginFactory.java @@ -145,7 +145,7 @@ public static Plugin createBackupRequestPlugin( .withDecorator(decorator); } - public static Plugin createTimeoutPlugin( + public static Plugin createGlobalTimeoutPlugin( final Client client, final TaskDecorator decorator) { final Duration timeout = client.getTimeouts().getGlobal().toDuration(); @@ -157,6 +157,18 @@ public static Plugin createTimeoutPlugin( .withDecorator(decorator); } + public static Plugin createCallTimeoutPlugin( + final Client client, final TaskDecorator decorator) { + + final Duration timeout = client.getTimeouts().getCall().toDuration(); + + return new FailsafePlugin() + .withPolicy( + Timeout.of(timeout) + .withCancel(true)) + .withDecorator(decorator); + } + private static DelayFunction delayFunction() { return new CompositeDelayFunction<>(Arrays.asList( new RetryAfterDelayFunction(systemUTC()), diff --git a/riptide-spring-boot-autoconfigure/src/main/java/org/zalando/riptide/autoconfigure/RiptideProperties.java b/riptide-spring-boot-autoconfigure/src/main/java/org/zalando/riptide/autoconfigure/RiptideProperties.java index 96d9a7e06..162351438 100644 --- a/riptide-spring-boot-autoconfigure/src/main/java/org/zalando/riptide/autoconfigure/RiptideProperties.java +++ b/riptide-spring-boot-autoconfigure/src/main/java/org/zalando/riptide/autoconfigure/RiptideProperties.java @@ -99,7 +99,7 @@ public static final class Defaults { private BackupRequest backupRequest = new BackupRequest(false, null); @NestedConfigurationProperty - private Timeouts timeouts = new Timeouts(false, null); + private Timeouts timeouts = new Timeouts(false, null, null); @NestedConfigurationProperty private RequestCompression requestCompression = new RequestCompression(false); @@ -327,6 +327,7 @@ public static final class BackupRequest { public static final class Timeouts { private Boolean enabled; private TimeSpan global; + private TimeSpan call; } @Getter