-
Notifications
You must be signed in to change notification settings - Fork 26
Create default NGINX API Client with specified Timeout #124
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
Changes from all commits
0d2621b
283a1cc
87d2b4c
d3c71c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,3 +6,6 @@ | |
| .vscode | ||
|
|
||
| dist | ||
|
|
||
| coverage.out | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -494,6 +494,60 @@ type HTTPLimitConnections map[string]LimitConnection | |
| // StreamLimitConnections represents limit connections related stats | ||
| type StreamLimitConnections map[string]LimitConnection | ||
|
|
||
| type option func(*NginxClient) error | ||
|
|
||
| // WithHTTPClient configures NGINX Plus Client to use customized http.Client. | ||
| // | ||
| //nolint:all | ||
| func WithHTTPClient(httpClient *http.Client) option { | ||
| return func(nc *NginxClient) error { | ||
| if httpClient == nil { | ||
| return errors.New("nil http client") | ||
| } | ||
| nc.httpClient = httpClient | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // WithAPIVersion configures NGINX Plus Client to use a specific version of the NGINX Plus API. | ||
| // | ||
| //nolint:all | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I'm struggling to see why these functions are needed and why they can't be "normal" parameters of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Functions in question (functonal options pattern) provide ability to configure created obj by the user of the library. At the same time if user is happy to use a default obj, he is not forced to do not necessary paperwork (creating httpClient to pass it to the func to create the obj, passing explicit api version, etc.). This way the API is cleaner and function signature keep to a minimum. In other words, user can create a minimum usable obj with minimum paperwork involved from his side. If the user wants to pass his own params (in this case http client and version) he can do it with functional options. Commenting on the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the explanation. I understand the functional options pattern, I'm just not sure about adding a new for the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The The additional functionality of the new func that creates an instance of the NGINX client is to decouple the client from a running NGINX server the client tries to connect to.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand what the functionality is about, I just don't agree with the implementation. I don't think Maybe somebody else from the team can weigh in @nginxinc/kic |
||
| func WithAPIVersion(version int) option { | ||
| return func(nc *NginxClient) error { | ||
| if !versionSupported(version) { | ||
| return fmt.Errorf("unsupported API version %v", version) | ||
| } | ||
| nc.version = version | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // NewDefaultNginxClient creates the client for the latest NGINX plus version | ||
| // and configured default HTTP timeout set to 10 seconds. User can configure | ||
| // customized http.Client and supported API version by passing options. | ||
| // | ||
| // NewDefaultNginxClient does not perform external http request to determine | ||
| // what NGINX version it talks to. It is responsibility of the caller to make | ||
| // sure the library is used with supported versions. | ||
| func NewDefaultNginxClient(apiEndpoint string, opts ...option) (*NginxClient, error) { | ||
| if apiEndpoint == "" { | ||
| return nil, errors.New("api endpoint not specified") | ||
| } | ||
| nc := NginxClient{ | ||
| apiEndpoint: apiEndpoint, | ||
| httpClient: &http.Client{ | ||
| Timeout: 10 * time.Second, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is hard-coding a 10 second timeout here really creating a 'default' nginx client? I would have thought leaving the http.Client timeout it its default is more correct. If the user wants to specify 10 seconds then they can do that with WithHTTPClient() |
||
| }, | ||
| version: APIVersion, | ||
| } | ||
| for _, opt := range opts { | ||
| if err := opt(&nc); err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
| return &nc, nil | ||
| } | ||
|
|
||
| // NewNginxClient creates an NginxClient with the latest supported version. | ||
| func NewNginxClient(httpClient *http.Client, apiEndpoint string) (*NginxClient, error) { | ||
| return NewNginxClientWithVersion(httpClient, apiEndpoint, APIVersion) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "github.com/nginxinc/nginx-plus-go-client/client" | ||
| ) | ||
|
|
||
| func main() { | ||
| // Create a custom HTTP Client | ||
| myHTTPClient := &http.Client{ | ||
| Timeout: 60 * time.Second, | ||
| } | ||
|
|
||
| // Create NGINX Plus Client for working with version 8 | ||
| c, err := client.NewDefaultNginxClient( | ||
| "https://demo.nginx.com/api", | ||
| client.WithHTTPClient(myHTTPClient), | ||
| client.WithAPIVersion(8), | ||
| ) | ||
| if err != nil { | ||
| // handle error | ||
| log.Fatal(err) | ||
| } | ||
|
|
||
| // Retrieve info about running NGINX | ||
| info, err := c.GetNginxInfo() | ||
| if err != nil { | ||
| // handle error | ||
| log.Fatal(err) | ||
| } | ||
| fmt.Printf("%+v\n", info) | ||
|
|
||
| // Prints | ||
| // &{Version:1.21.6 Build:nginx-plus-r27 Address:3.125.64.247 Generation:4 LoadTimestamp:2022-09-14T12:45:25.218Z Timestamp:2022-10-10T12:25:16.552Z ProcessID:3640888 ParentProcessID:2945895} | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| module github.com/nginxinc/nginx-plus-go-client | ||
|
|
||
| go 1.19 | ||
|
|
||
| require github.com/google/go-cmp v0.5.8 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= | ||
| github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sometimes it's client.New...() (see line 63 for example), sometimes it's just New...() (as in here). Can we keep this consistent?