Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UrlBuilder supports adding multiple path segments at once #2016

Merged
merged 2 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changelog/@unreleased/pr-2016.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type: improvement
improvement:
description: |-
UrlBuilder supports adding multiple path segments at once.
Deprecation metric is only created when a deprecated endpoint is invoked.
links:
- https://github.com/palantir/dialogue/pull/2016
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.base.CharMatcher;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.common.collect.Collections2;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
Expand All @@ -36,6 +37,7 @@
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;

Expand Down Expand Up @@ -154,6 +156,12 @@ public DefaultUrlBuilder pathSegment(String thePath) {
return this;
}

@Override
public DefaultUrlBuilder pathSegments(Collection<String> paths) {
this.pathSegments.addAll(Collections2.transform(paths, BaseUrl.UrlEncoder::encodePathSegment));
return this;
}

/**
* URL-encodes the given query parameter name and value and adds them to the list of query parameters. Note that
* no guarantee is made regarding the ordering of query parameters in the resulting URL.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.codahale.metrics.Meter;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.common.base.Suppliers;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.ListenableFuture;
import com.palantir.dialogue.Endpoint;
Expand All @@ -32,6 +33,7 @@
import com.palantir.tritium.metrics.registry.TaggedMetricRegistry;
import java.time.Duration;
import java.util.Optional;
import java.util.function.Supplier;
import org.immutables.value.Value;

/**
Expand Down Expand Up @@ -73,19 +75,19 @@ public ListenableFuture<Response> execute(Request request) {
}

private FutureCallback<Response> createCallback(String channelName, Endpoint endpoint) {
Meter meter = metrics.deprecations(endpoint.serviceName());

// lazily create meter metric name only if deprecated endpoint is accessed
Supplier<Meter> meterSupplier = Suppliers.memoize(() -> metrics.deprecations(endpoint.serviceName()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine this came up with something that used sticky sessions? iirc each sticky session client re-creates endpointchannel instances, which is why we memoize some other metrics.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, I saw this on some of the major clients that use sticky sessions

return DialogueFutures.onSuccess(response -> {
if (response == null) {
return;
}

Optional<String> maybeHeader = response.getFirstHeader("deprecation");
if (!maybeHeader.isPresent()) {
if (maybeHeader.isEmpty()) {
return;
}

meter.mark();
meterSupplier.get().mark();
if (log.isWarnEnabled() && tryAcquire(channelName, endpoint)) {
log.warn(
"Using a deprecated endpoint when connecting to service",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.palantir.dialogue.TestEndpoint;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.UUID;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -65,13 +66,21 @@ public void encodesPaths() throws Exception {
assertThat(minimalUrl().pathSegment("foo").build().toString()).isEqualTo("http://host:80/foo");
assertThat(minimalUrl().pathSegment("foo").pathSegment("bar").build().toString())
.isEqualTo("http://host:80/foo/bar");
assertThat(minimalUrl().pathSegments(List.of("foo", "bar")).build().toString())
.isEqualTo("http://host:80/foo/bar");
assertThat(minimalUrl().pathSegment("foo/bar").build().toString()).isEqualTo("http://host:80/foo%2Fbar");
assertThat(minimalUrl()
.pathSegment("!@#$%^&*()_+{}[]|\\|\"':;/?.>,<~`")
.build()
.toString())
.isEqualTo("http://host:80/%21%40%23%24%25%5E%26%2A%28%29_%2B%7B%7D"
+ "%5B%5D%7C%5C%7C%22%27%3A%3B%2F%3F.%3E%2C%3C~%60");
assertThat(minimalUrl()
.pathSegments(List.of("!@#$%^&*()_+{}", "[]|\\|\"':;/?.>,<~`"))
.build()
.toString())
.isEqualTo("http://host:80/%21%40%23%24%25%5E%26%2A%28%29_%2B%7B%7D"
+ "/%5B%5D%7C%5C%7C%22%27%3A%3B%2F%3F.%3E%2C%3C~%60");
}

@Test
Expand All @@ -83,6 +92,12 @@ public void handlesEmptyPathSegments() throws Exception {
.build()
.toString())
.isEqualTo("http://host:80///bar");
assertThat(minimalUrl()
.pathSegments(List.of("", ""))
.pathSegment("bar")
.build()
.toString())
.isEqualTo("http://host:80///bar");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void fill(ListMultimap<String, String> parameters, UrlBuilder url) {
if (segment.fixed != null) {
url.pathSegment(segment.fixed);
} else {
parameters.get(segment.variable).forEach(url::pathSegment);
url.pathSegments(parameters.get(segment.variable));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@

package com.palantir.dialogue;

import java.util.Collection;

public interface UrlBuilder {

/** URL-encodes the given path segment and adds it to the list of segments. */
UrlBuilder pathSegment(String thePath);

default UrlBuilder pathSegments(Collection<String> paths) {
paths.forEach(this::pathSegment);
return this;
}

/**
* URL-encodes the given query parameter name and value and adds them to the list of query parameters. Note that
* no guarantee is made regarding the ordering of query parameters in the resulting URL.
Expand Down
Loading