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

Add several configuration options to ConsulFactory #23

Closed
sleberknight opened this issue May 4, 2023 · 1 comment · Fixed by #78
Closed

Add several configuration options to ConsulFactory #23

sleberknight opened this issue May 4, 2023 · 1 comment · Fixed by #78
Assignees
Labels
enhancement A request for change or improvement to an existing feature
Milestone

Comments

@sleberknight
Copy link
Member

Add several configuration properties to ConsulFactory to permit changing the read and write timeouts for the underlying OkHttp client, as well as allowing you to set the client configuration (com.orbitz.consul.config.ClientConfig).

Add the following fields are to ConsulFactory, right below the healthCheckPath:

private Optional<Long> networkWriteTimeoutMillis = Optional.empty();

private Optional<Long> networkReadTimeoutMillis = Optional.empty();

private Optional<ClientConfig> clientConfig = Optional.empty();

Replace all the Optional.ofNullable with calls to a private helper method toOptional:

private static <T> Optional<T> toOptional(T propertyValue) {
    return Optional.ofNullable(propertyValue);
}

After the setHealthCheckPath method, add "getters" and setters for the three new properties. The getter returns Optional while the setter accepts the "raw" value. e.g.

public Optional<Long> getNetworkWriteTimeoutMillis() {
  return networkWriteTimeoutMillis ;
}

public void setNetworkWriteTimeoutMillis(Long networkTimeout) {
    this.networkWriteTimeoutMillis  = toOptional(networkTimeout);
}

// Use the same pattern for networkReadTimeoutMillis and clientConfig...

The last production code change in ConsulFactory is in in the build method: after the aclToken.ifPresent, add the following three lines:

networkWriteTimeoutMillis.ifPresent(builder::withWriteTimeoutMillis);
networkReadTimeoutMillis.ifPresent(builder::withReadTimeoutMillis);
clientConfig.ifPresent(builder::withClientConfiguration);

Next, add the following (JUnit 4) test to ConsulBundleTest:

@Test
public void testPopulatedNetworkWriteTimeout() throws Exception {
    CacheConfig cacheConfig =
        CacheConfig .builder().withWatchDuration(Duration.of(20, ChronoUnit.SECONDS)).build();
    ClientConfig clientConfig = new ClientConfig(cacheConfig);
    factory.setNetworkWriteTimeoutMillis(5_000L);
    factory.setClientConfig(clientConfig);
    factory.setNetworkReadTimeoutMillis(10_001L);
    bundle.run(config, environment);

    assertThat(factory.getNetworkWriteTimeoutMillis()).isPresent().contains(5_000L);
    assertThat(factory.getNetworkReadTimeoutMillis()).isPresent().contains(10_001L);
    assertThat(factory.getClientConfig())
        .isPresent()
        .is(new Condition<Optional<ClientConfig>>() {
            @Override
            public boolean matches(Optional<ClientConfig> value) {
                return value.isPresent() &&
                    value.get().getCacheConfig().getWatchDuration().equals(Duration.of(20, ChronoUnit.SECONDS));
            }
        });
}

We will convert to JUnit 5 in a separate issue.

And last, change the last line in the testAclToken test to:

assertThat(factory.getAclToken()).isPresent().contains(token);

In the above, the isPresent() calls are redundant and can be removed. Asserting that an Optional contains a value also asserts that a value is present.

@sleberknight sleberknight added the enhancement A request for change or improvement to an existing feature label May 4, 2023
@sleberknight sleberknight added this to the 0.5.0 milestone May 4, 2023
@sleberknight sleberknight self-assigned this May 4, 2023
@sleberknight
Copy link
Member Author

Note that the ConsulFactory implementation has equals and hashCode even though it doesn't seem like you would ever need to compare two ConsulFactory objects. I do not plan to add these new properties to the existing equals and hashCode methods, and after seeing these methods am considering several future changes:

  1. Split ConsulFactory into separate classes, one that contains only configuration, and one for taking that configuration and building the Consul instance.
  2. Remove equals and hashCode from the factory class
  3. Maybe make the configuration class a "value" or "data" class, where it would (maybe) make more sense to have equals and hashCode
  4. Change all the Optional handling across the library, i.e. don't declare fields as Optional and don't pass Optional to methods, etc.

sleberknight added a commit that referenced this issue May 11, 2023
* networkWriteTimeoutMillis
* networkReadTimeoutMillis
* ClientConfig

Closes #23
dsingley pushed a commit that referenced this issue May 12, 2023
* networkWriteTimeoutMillis
* networkReadTimeoutMillis
* ClientConfig

Closes #23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A request for change or improvement to an existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant