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

Implement Functional Options #33

Merged
merged 15 commits into from
Jul 16, 2018
Merged

Implement Functional Options #33

merged 15 commits into from
Jul 16, 2018

Conversation

italolelis
Copy link
Contributor

Hello,

As proposed in here #31, the following implements functional options into the library. This is a breaking change and should probably increase the major version. A few things to consider:

New package names

Why?

I started to have name conflicts with the options that were common to both clients since both of them are in the root folder of the project. Here is an example of the issue:

httpClient := heimdall.NewHTTPClient(
	heimdall.WithHTTPTimeout(10), 
	heimdall.WithRetryCount(4),
)

hystrixClient := heimdall.NewHystrixHTTPClient(
	heimdall.WithHTTPTimeout(10), // This name cannot be the same
	heimdall.WithRetryCount(4),// This name cannot be the same
	heimdall.WithHystrixTimeout(1100),
)

For this reason, I thought it would be more elegant to have the client implementations in their own package. I called them httpclient (to avoid conflict with the http package) and hystrix

Now the usage looks like:

	httpClient := httpclient.NewClient(
		httpclient.WithHTTPTimeout(timeout),
		httpclient.WithRetryCount(2),
	)

	hystrixClient := hystrix.NewClient(
		hystrix.WithHTTPTimeout(timeout),
		hystrix.WithCommandName("MyCommand"),
		hystrix.WithHystrixTimeout(1100),
		hystrix.WithMaxConcurrentRequests(100),
		hystrix.WithErrorPercentThreshold(25),
		hystrix.WithSleepWindow(10),
		hystrix.WithRequestVolumeThreshold(10),
	)

Returning concrete types

Now I started to return concrete types on the constructors of the clients and to make sure the compiler can identify if we are implementing the interface I added this line.

var _ heimdall.Client = (*Client)(nil)

If you don't like the packaging strategy please let me know and we can try to find a solution.

Thanks!

@rShetty
Copy link
Contributor

rShetty commented May 9, 2018

@italolelis Thanks for the PR. will Check it this week.

@rShetty
Copy link
Contributor

rShetty commented Jun 7, 2018

@italolelis There are merge conflicts, can you help resolve these plz.

@italolelis
Copy link
Contributor Author

@rShetty done

@rafaeljesus
Copy link

looking forward to this merge, good job @italolelis

@rShetty
Copy link
Contributor

rShetty commented Jun 22, 2018

@italolelis Skimmed through the PR. Overall looks solid and Thank you for this. Have added few comments.

@italolelis
Copy link
Contributor Author

@rShetty Thanks for reviewing, I can't see the comments that you left though. Do I need to fix something?

Copy link
Contributor

@rShetty rShetty left a comment

Choose a reason for hiding this comment

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

Changes

README.md Outdated
@@ -259,15 +268,19 @@ func (c *myHTTPClient) Do(request *http.Request) (*http.Response, error) {
}
```

And set this with `httpClient.SetCustomHTTPClient(&myHTTPClient{client: http.DefaultClient})`
And set this with `httpclient.NewClient(httpclient.NewClient&myHTTPClient{client: http.DefaultClient}))`
Copy link
Contributor

Choose a reason for hiding this comment

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

Brackets seem off, can we fix this

// WithHTTPTimeout sets hystrix timeout
func WithHTTPTimeout(timeout time.Duration) Option {
return func(c *Client) {
c.timeout = timeout
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add some sane defaults to all of the functional options ?

// WithHTTPTimeout sets hystrix timeout
func WithHTTPTimeout(timeout time.Duration) Option {
return func(c *Client) {
c.timeout = timeout
Copy link
Contributor

Choose a reason for hiding this comment

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

Defaults in these functional options too

// WithRetryCount sets the retry count for the Client
func WithRetryCount(retryCount int) Option {
return func(c *Client) {
c.retryCount = retryCount
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe also test cases for this file ?

@rShetty
Copy link
Contributor

rShetty commented Jun 25, 2018

@italolelis Have commented

@italolelis
Copy link
Contributor Author

@rShetty I've added the defaults and tests. Please, check if it makes sense.

@rShetty rShetty merged commit 2f4870e into gojek:master Jul 16, 2018
@rShetty
Copy link
Contributor

rShetty commented Jul 16, 2018

Thank you very much for the PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants