private Optional<Integer> indexFor(HttpUrl url) { HttpUrl canonicalUrl = canonicalize(url); for (int i = 0; i < baseUrls.size(); ++i) { if (isBaseUrlFor(baseUrls.get(i), canonicalUrl)) { return Optional.of(i); } } return Optional.empty(); }
private Optional<Integer> indexFor(HttpUrl url) { HttpUrl canonicalUrl = canonicalize(url); for (int i = 0; i < baseUrls.size(); ++i) { if (isBaseUrlFor(baseUrls.get(i), canonicalUrl)) { return Optional.of(i); } } return Optional.empty(); }
/** * Creates a new {@link UrlSelector} with the supplied URLs. The order of the URLs may be randomized by setting * {@code randomizeOrder} to true. If a {@code failedUrlCooldown} is specified, URLs that are marked as failed * using {@link #markAsFailed(HttpUrl)} will be removed from the pool of prioritized, healthy URLs for that period * of time. */ static UrlSelectorImpl createWithFailedUrlCooldown(Collection<String> baseUrls, boolean randomizeOrder, Duration failedUrlCooldown) { List<String> orderedUrls = new ArrayList<>(baseUrls); if (randomizeOrder) { Collections.shuffle(orderedUrls); } ImmutableSet.Builder<HttpUrl> canonicalUrls = ImmutableSet.builder(); // ImmutableSet maintains insert order orderedUrls.forEach(url -> { HttpUrl httpUrl = HttpUrl.parse(switchWsToHttp(url)); Preconditions.checkArgument(httpUrl != null, "Not a valid URL: %s", url); HttpUrl canonicalUrl = canonicalize(httpUrl); Preconditions.checkArgument(canonicalUrl.equals(httpUrl), "Base URLs must be 'canonical' and consist of schema, host, port, and path only: %s", url); canonicalUrls.add(canonicalUrl); }); return new UrlSelectorImpl(ImmutableList.copyOf(canonicalUrls.build()), failedUrlCooldown); }
/** * Creates a new {@link UrlSelector} with the supplied URLs. The order of the URLs may be randomized by setting * {@code randomizeOrder} to true. If a {@code failedUrlCooldown} is specified, URLs that are marked as failed * using {@link #markAsFailed(HttpUrl)} will be removed from the pool of prioritized, healthy URLs for that period * of time. */ static UrlSelectorImpl createWithFailedUrlCooldown(Collection<String> baseUrls, boolean randomizeOrder, Duration failedUrlCooldown) { List<String> orderedUrls = new ArrayList<>(baseUrls); if (randomizeOrder) { Collections.shuffle(orderedUrls); } ImmutableSet.Builder<HttpUrl> canonicalUrls = ImmutableSet.builder(); // ImmutableSet maintains insert order orderedUrls.forEach(url -> { HttpUrl httpUrl = HttpUrl.parse(switchWsToHttp(url)); Preconditions.checkArgument(httpUrl != null, "Not a valid URL: %s", url); HttpUrl canonicalUrl = canonicalize(httpUrl); Preconditions.checkArgument(canonicalUrl.equals(httpUrl), "Base URLs must be 'canonical' and consist of schema, host, port, and path only: %s", url); canonicalUrls.add(canonicalUrl); }); return new UrlSelectorImpl(ImmutableList.copyOf(canonicalUrls.build()), failedUrlCooldown); }