]> git.basschouten.com Git - openhab-addons.git/blob
b8b19f12b41c48f5d2f8dc5086a24315be137f9f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.energidataservice.internal.retry;
14
15 import java.time.Clock;
16 import java.time.Duration;
17 import java.time.LocalTime;
18 import java.time.ZoneId;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jetty.http.HttpStatus;
22 import org.openhab.binding.energidataservice.internal.exception.DataServiceException;
23 import org.openhab.binding.energidataservice.internal.retry.strategy.ExponentialBackoff;
24 import org.openhab.binding.energidataservice.internal.retry.strategy.FixedTime;
25 import org.openhab.binding.energidataservice.internal.retry.strategy.Linear;
26
27 /**
28  * This factory defines policies for determining appropriate {@link RetryStrategy} based
29  * on scenario.
30  * 
31  * @author Jacob Laursen - Initial contribution
32  */
33 @NonNullByDefault
34 public class RetryPolicyFactory {
35
36     /**
37      * Determine {@link RetryStrategy} from {@link Throwable}.
38      *
39      * @param e thrown exception
40      * @return retry strategy
41      */
42     public static RetryStrategy fromThrowable(Throwable e) {
43         if (e instanceof DataServiceException dse) {
44             switch (dse.getHttpStatus()) {
45                 case HttpStatus.TOO_MANY_REQUESTS_429:
46                     return new ExponentialBackoff().withMinimum(Duration.ofMinutes(30));
47                 default:
48                     return new ExponentialBackoff().withMinimum(Duration.ofMinutes(1)).withJitter(0.2);
49             }
50         }
51
52         return new ExponentialBackoff().withMinimum(Duration.ofMinutes(1)).withJitter(0.2);
53     }
54
55     /**
56      * Default {@link RetryStrategy} with one retry per day.
57      * This is intended as a dummy strategy until replaced by a concrete one.
58      *
59      * @return retry strategy
60      */
61     public static RetryStrategy initial() {
62         return new Linear().withMinimum(Duration.ofDays(1));
63     }
64
65     /**
66      * Determine {@link RetryStrategy} for next expected data publishing.
67      *
68      * @param localTime the time of daily data request in local time-zone
69      * @param zoneId the local time-zone
70      * @return retry strategy
71      */
72     public static RetryStrategy atFixedTime(LocalTime localTime, ZoneId zoneId) {
73         return new FixedTime(localTime, Clock.system(zoneId)).withJitter(1);
74     }
75
76     /**
77      * Determine {@link RetryStrategy} when expected spot price data is missing.
78      *
79      * @param localTime the time of daily data request
80      * @param zoneId time-zone
81      * @return retry strategy
82      */
83     public static RetryStrategy whenExpectedSpotPriceDataMissing(LocalTime localTime, ZoneId zoneId) {
84         LocalTime now = LocalTime.now(zoneId);
85         if (now.isAfter(localTime)) {
86             return new ExponentialBackoff().withMinimum(Duration.ofMinutes(10)).withJitter(0.2);
87         }
88         return atFixedTime(localTime, zoneId);
89     }
90 }