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

api: allow configuring http client #5275

Merged
merged 2 commits into from
May 20, 2019
Merged

api: allow configuring http client #5275

merged 2 commits into from
May 20, 2019

Conversation

notnoop
Copy link
Contributor

@notnoop notnoop commented Jan 30, 2019

Allow clients to configure httpClient, e.g. set a pooled/keep-alive
client.

When caller configures HttpClient explicitly, we aim to use as-is; e.g.
we assume it's configured with TLS already. Expose ConfigureTLS to
aid api consumers with configuring their http client.

Also, removes SetTimeout call that I believe is internal only and has
odd side-effects when called on already created config. Also deprecates
config.ConfigureTLS in preference to the new ConfigureTLS.

Copy link
Member

@schmichael schmichael left a comment

Choose a reason for hiding this comment

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

The way we tweak a HttpClient's Transport makes this a little scary to review, but I think overriding the Config.HttpClient before calling NewClient will do the right thing (other than the one spot in ClientConfig I noted).

api/api.go Outdated
@@ -147,7 +147,7 @@ func (c *Config) ClientConfig(region, address string, tlsEnabled bool) *Config {
Address: fmt.Sprintf("%s://%s", scheme, address),
Region: region,
Namespace: c.Namespace,
httpClient: defaultConfig.httpClient,
HttpClient: defaultConfig.HttpClient,
Copy link
Member

Choose a reason for hiding this comment

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

ClientConfig will overwrite any custom http client with the default client. If we support setting a custom client we should propagate it to config copies.

@notnoop notnoop force-pushed the f-api-config-httpclient branch 2 times, most recently from 1d5c294 to f278760 Compare May 17, 2019 20:17
Allow clients to configure httpClient, e.g. set a pooled/keep-alive
client.

When caller configures HttpClient explicitly, we aim to use as-is; e.g.
we assume it's configured with TLS already.  Expose `ConfigureTLS` to
aid api consumers with configuring their http client.

Also, removes `SetTimeout` call that I believe is internal only and has
odd side-effects when called on already created config.  Also deprecates
`config.ConfigureTLS` in preference to the new `ConfigureTLS`.
@notnoop notnoop force-pushed the f-api-config-httpclient branch from f278760 to cbc1555 Compare May 17, 2019 20:27
@notnoop notnoop requested a review from schmichael May 17, 2019 20:27
Copy link
Contributor Author

@notnoop notnoop left a comment

Choose a reason for hiding this comment

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

@schmichael Updated so we don't modify transports or tls config, and be able to handle client connections without modifying the initial state.

Few questions though inlined that I'd love your input on.

func DefaultConfig() *Config {
config := &Config{
Address: "http://127.0.0.1:4646",
httpClient: cleanhttp.DefaultClient(),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously, DefaultConfig initializes httpClient for client to use. This PR changes it so default httpClient creation occurs in NewClient instead and have it be a field of the api client rather than config.

// SetTimeout is used to place a timeout for connecting to Nomad. A negative
// duration is ignored, a duration of zero means no timeout, and any other value
// will add a timeout.
func (c *Config) SetTimeout(t time.Duration) error {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed this function, as I believe we only intend to use internally for creating client connections and it's hard for me to imagine someone using it as-is now for other purposes; and if they do, they can set their http client.

If we don't want to remove a public function here, I can reintroduce it but with adding some additional bookkeeping to discern on applying tls config whether it's to default client with timeout or a custom http client.

}

// copy all public fields, to avoid copying transient state and locks
ntr := &http.Transport{
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here, I want to preserve as much of the http client the user provided (including proxies, etc). Doing copying seems to copy transient state and locks; so opted to copy over all fields explicitly. Is there a better way?

Alternatively, we can have users set a HttpClient constructor rather than a simple HttpClient, so we can modify instances easily. I opted not to, because I felt it complicated the API too much.

api/api.go Outdated
}

// ConfigureTLS applies a set of TLS configurations to the the HTTP client.
//
// Deprecated: This method is called internally. Consider using ConfigureTLS instead.
Copy link
Member

@schmichael schmichael May 20, 2019

Choose a reason for hiding this comment

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

Consider using ConfigureTLS instead.

This method is ConfigureTLS. Should we just unexport it since we're already changing the API?

_Update: Of course 3s later I spot the ConfigureTLS func 😅. I think it's probably worth adding "func" to the comment or something though -- or unexport it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll just remove it then. API consumers can just remove invocation without any change in behavior as it's called in NewClient anyway.

`*Config.ConfigureTLS()` is invoked internally by `NewClient` and API
consumers should not invoke directly.

Now that http client is created in `api.NewClient`,
`*Config.ConfigureTLS` makes no sense.  API consumers that call it
explicitly can remove the invocation and preserve the behavior.
@notnoop notnoop force-pushed the f-api-config-httpclient branch from f4bf14a to 10ab705 Compare May 20, 2019 15:58
@notnoop notnoop merged commit 72f46f0 into master May 20, 2019
@notnoop notnoop deleted the f-api-config-httpclient branch May 21, 2019 15:54
@github-actions
Copy link

github-actions bot commented Feb 9, 2023

I'm going to lock this pull request because it has been closed for 120 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants