1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.client5.http.impl.classic;
29
30 import java.io.IOException;
31
32 import org.apache.hc.client5.http.HttpRoute;
33 import org.apache.hc.client5.http.classic.BackoffManager;
34 import org.apache.hc.client5.http.classic.ConnectionBackoffStrategy;
35 import org.apache.hc.client5.http.classic.ExecChain;
36 import org.apache.hc.client5.http.classic.ExecChainHandler;
37 import org.apache.hc.core5.annotation.Contract;
38 import org.apache.hc.core5.annotation.Experimental;
39 import org.apache.hc.core5.annotation.ThreadingBehavior;
40 import org.apache.hc.core5.http.ClassicHttpRequest;
41 import org.apache.hc.core5.http.ClassicHttpResponse;
42 import org.apache.hc.core5.http.HttpException;
43 import org.apache.hc.core5.util.Args;
44
45
46
47
48
49
50
51
52
53
54
55
56 @Contract(threading = ThreadingBehavior.STATELESS)
57 @Experimental
58 public final class BackoffStrategyExec implements ExecChainHandler {
59
60 private final ConnectionBackoffStrategy connectionBackoffStrategy;
61 private final BackoffManager backoffManager;
62
63 public BackoffStrategyExec(
64 final ConnectionBackoffStrategy connectionBackoffStrategy,
65 final BackoffManager backoffManager) {
66 super();
67 Args.notNull(connectionBackoffStrategy, "Connection backoff strategy");
68 Args.notNull(backoffManager, "Backoff manager");
69 this.connectionBackoffStrategy = connectionBackoffStrategy;
70 this.backoffManager = backoffManager;
71 }
72
73 @Override
74 public ClassicHttpResponse execute(
75 final ClassicHttpRequest request,
76 final ExecChain.Scope scope,
77 final ExecChain chain) throws IOException, HttpException {
78 Args.notNull(request, "HTTP request");
79 Args.notNull(scope, "Scope");
80 final HttpRoute route = scope.route;
81
82 final ClassicHttpResponse response;
83 try {
84 response = chain.proceed(request, scope);
85 } catch (final IOException | HttpException ex) {
86 if (this.connectionBackoffStrategy.shouldBackoff(ex)) {
87 this.backoffManager.backOff(route);
88 }
89 throw ex;
90 }
91 if (this.connectionBackoffStrategy.shouldBackoff(response)) {
92 this.backoffManager.backOff(route);
93 } else {
94 this.backoffManager.probe(route);
95 }
96 return response;
97 }
98
99 }