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

fix: Enable http transport connection reuse #207

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 4 additions & 39 deletions netbox/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package netbox

import (
"context"
"crypto/tls"
"fmt"
"net/http"

Expand Down Expand Up @@ -214,48 +213,14 @@ func configureProvider(ctx context.Context, d *schema.ResourceData) (interface{}
scheme := d.Get("scheme").(string)
insecure := d.Get("insecure").(bool)

var options runtimeclient.TLSClientOptions
options.InsecureSkipVerify = insecure
tlsConfig, _ := runtimeclient.TLSClientAuth(options)

headers := make(map[string]string)

// Create a custom client
// Override the default transport with a RoundTripper to inject dynamic headers
// Add TLSOptions
cli := &http.Client{
Transport: &transport{
headers: headers,
TLSClientConfig: tlsConfig,
},
}

defaultScheme := []string{scheme}

t := runtimeclient.NewWithClient(url, basepath, defaultScheme, cli)
t := runtimeclient.New(url, basepath, defaultScheme)
if insecure {
t.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify = insecure
}
t.DefaultAuthentication = runtimeclient.APIKeyAuth(authHeaderName, "header",
fmt.Sprintf(authHeaderFormat, token))

return client.New(t, strfmt.Default), nil
}

type transport struct {
headers map[string]string
base http.RoundTripper
TLSClientConfig *tls.Config
}

func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
// Add headers to request
for k, v := range t.headers {
req.Header.Add(k, v)
}
base := t.base
if base == nil {
// init an http.Transport with TLSOptions
customTransport := http.DefaultTransport.(*http.Transport).Clone()
customTransport.TLSClientConfig = t.TLSClientConfig
base = customTransport
}
return base.RoundTrip(req)
}