Skip to content

Commit

Permalink
Expose methods to allow user to add headers in Thrift request (#255)
Browse files Browse the repository at this point in the history
* Expose methods in TChannel service to allow user to add headers in Thrift request

* Update changelog
  • Loading branch information
meiliang86 authored Mar 5, 2019
1 parent b83f643 commit 7f85e92
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v2.3.1
- Added support for SignalWithStart Service API
- Expose methods in TChannel service to allow user to add headers in Thrift request

## v2.3.0
- Added cron schedule support.
- Fix infinite retryer in activity and workflow worker due to non-retryable error.
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ googleJavaFormat {
}

group = 'com.uber.cadence'
version = '2.3.0'
version = '2.3.1'

description = """Uber Cadence Java Client"""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ public static class ClientOptions {
/** Optional TChannel transport headers */
private final Map<String, String> transportHeaders;

/** Optional TChannel headers */
private final Map<String, String> headers;

private ClientOptions(Builder builder) {
this.rpcTimeoutMillis = builder.rpcTimeoutMillis;
if (builder.clientAppName == null) {
Expand All @@ -169,6 +172,12 @@ private ClientOptions(Builder builder) {
} else {
this.transportHeaders = ImmutableMap.of();
}

if (builder.headers != null) {
this.headers = ImmutableMap.copyOf(builder.headers);
} else {
this.headers = ImmutableMap.of();
}
}

/** @return Returns the rpc timeout value in millis. */
Expand Down Expand Up @@ -203,6 +212,10 @@ public Map<String, String> getTransportHeaders() {
return transportHeaders;
}

public Map<String, String> getHeaders() {
return headers;
}

/**
* Builder is the builder for ClientOptions.
*
Expand All @@ -218,6 +231,7 @@ public static class Builder {
public String serviceName;
private Scope metricsScope;
private Map<String, String> transportHeaders;
private Map<String, String> headers;

/**
* Sets the rpc timeout value for non query and non long poll calls. Default is 1000.
Expand Down Expand Up @@ -298,6 +312,11 @@ public Builder setTransportHeaders(Map<String, String> transportHeaders) {
return this;
}

public Builder setHeaders(Map<String, String> headers) {
this.headers = headers;
return this;
}

/**
* Builds and returns a ClientOptions object.
*
Expand Down Expand Up @@ -349,7 +368,7 @@ public WorkflowServiceTChannel(String host, int port, ClientOptions options) {
throw new IllegalArgumentException("0 or negative port");
}
this.options = options;
this.thriftHeaders = getThriftHeaders();
this.thriftHeaders = getThriftHeaders(options);
// this.metricsReporter = new MetricsReporter(options.getMetricsClient());
// Need to create tChannel last in order to prevent leaking when an exception is thrown
this.tChannel = new TChannel.Builder(options.getClientAppName()).build();
Expand Down Expand Up @@ -380,13 +399,13 @@ public WorkflowServiceTChannel(String host, int port, ClientOptions options) {
*/
public WorkflowServiceTChannel(SubChannel subChannel, ClientOptions options) {
this.options = options;
this.thriftHeaders = getThriftHeaders();
this.thriftHeaders = getThriftHeaders(options);
// this.metricsReporter = new MetricsReporter(options.getMetricsClient());
this.tChannel = null;
this.subChannel = subChannel;
}

private static Map<String, String> getThriftHeaders() {
private static Map<String, String> getThriftHeaders(ClientOptions options) {
String envUserName = System.getenv("USER");
String envHostname;
try {
Expand All @@ -395,12 +414,20 @@ private static Map<String, String> getThriftHeaders() {
envHostname = "localhost";
}

return ImmutableMap.<String, String>builder()
.put("user-name", envUserName)
.put("host-name", envHostname)
.put("cadence-client-library-version", Version.LIBRARY_VERSION)
.put("cadence-client-feature-version", Version.FEATURE_VERSION)
.build();
ImmutableMap.Builder<String, String> builder =
ImmutableMap.<String, String>builder()
.put("user-name", envUserName)
.put("host-name", envHostname)
.put("cadence-client-library-version", Version.LIBRARY_VERSION)
.put("cadence-client-feature-version", Version.FEATURE_VERSION);

if (options.headers != null) {
for (Map.Entry<String, String> entry : options.headers.entrySet()) {
builder.put(entry.getKey(), entry.getValue());
}
}

return builder.build();
}

/** Returns the endpoint in the format service::method" */
Expand Down

0 comments on commit 7f85e92

Please sign in to comment.