1 /*
2 * Copyright (C) 2013 Square, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package okhttp3.internal.connection;
17
18 import java.util.LinkedHashSet;
19 import java.util.Set;
20 import okhttp3.Route;
21
22 /**
23 * A blacklist of failed routes to avoid when creating a new connection to a target address. This is
24 * used so that OkHttp can learn from its mistakes: if there was a failure attempting to connect to
25 * a specific IP address or proxy server, that failure is remembered and alternate routes are
26 * preferred.
27 */
28 final class RouteDatabase {
29 private final Set<Route> failedRoutes = new LinkedHashSet<>();
30
31 /** Records a failure connecting to {@code failedRoute}. */
32 public synchronized void failed(Route failedRoute) {
33 failedRoutes.add(failedRoute);
34 }
35
36 /** Records success connecting to {@code route}. */
37 public synchronized void connected(Route route) {
38 failedRoutes.remove(route);
39 }
40
41 /** Returns true if {@code route} has failed recently and should be avoided. */
42 public synchronized boolean shouldPostpone(Route route) {
43 return failedRoutes.contains(route);
44 }
45 }
46