Skip to content

Commit

Permalink
User supplied http client in RPC provider:
Browse files Browse the repository at this point in the history
This plumbs through either the default or user supplied
http client to the RPC provider.

Signed-off-by: Jacob Weinstock <[email protected]>
  • Loading branch information
jacobweinstock committed Oct 1, 2023
1 parent d2aac70 commit 8c54ad5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
3 changes: 3 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ func (c *Client) defaultTimeout(ctx context.Context) time.Duration {
func (c *Client) registerRPCProvider() error {
driverRPC := rpc.New(c.providerConfig.rpc.ConsumerURL, c.Auth.Host, c.providerConfig.rpc.Opts.HMAC.Secrets)
c.providerConfig.rpc.Logger = c.Logger
httpClient := *c.httpClient
httpClient.Transport = c.httpClient.Transport.(*http.Transport).Clone()
c.providerConfig.rpc.HTTPClient = &httpClient

Check warning on line 142 in client.go

View check run for this annotation

Codecov / codecov/patch

client.go#L140-L142

Added lines #L140 - L142 were not covered by tests
if err := mergo.Merge(driverRPC, c.providerConfig.rpc, mergo.WithOverride, mergo.WithTransformers(&rpc.Provider{})); err != nil {
return fmt.Errorf("failed to merge user specified rpc config with the config defaults, rpc provider not available: %w", err)
}
Expand Down
9 changes: 5 additions & 4 deletions providers/rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"
"time"

"github.com/bmc-toolbox/bmclib/v2/internal/httpclient"
"github.com/bmc-toolbox/bmclib/v2/providers"
"github.com/go-logr/logr"
"github.com/jacobweinstock/registrar"
Expand Down Expand Up @@ -64,8 +65,8 @@ type Provider struct {
ConsumerURL string
// Host is the BMC ip address or hostname or identifier.
Host string
// Client is the http client used for all HTTP calls.
Client *http.Client
// HTTPClient is the http client used for all HTTP calls.
HTTPClient *http.Client
// Logger is the logger to use for logging.
Logger logr.Logger
// LogNotificationsDisabled determines whether responses from rpc consumer/listeners will be logged or not.
Expand Down Expand Up @@ -136,7 +137,7 @@ func New(consumerURL string, host string, secrets Secrets) *Provider {
c := &Provider{
Host: host,
ConsumerURL: consumerURL,
Client: http.DefaultClient,
HTTPClient: httpclient.Build(),
Logger: logr.Discard(),
Opts: Opts{
Request: RequestOpts{
Expand Down Expand Up @@ -325,7 +326,7 @@ func (p *Provider) process(ctx context.Context, rp RequestPayload) (ResponsePayl
kvs = append(kvs, []interface{}{"params", rp.Params}...)
}

resp, err := p.Client.Do(req)
resp, err := p.HTTPClient.Do(req)
if err != nil {
p.Logger.Error(err, "failed to send rpc notification", kvs...)
return ResponsePayload{}, err
Expand Down

0 comments on commit 8c54ad5

Please sign in to comment.