Skip to content
Closed
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions cmd/lakectl/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ var (
tokenCache *awsiam.JWTCache
tokenCacheOnce sync.Once
ErrTokenUnavailable = fmt.Errorf("token is not available")

apiClient *apigen.ClientWithResponses
apiClientOnce sync.Once
)

func withRecursiveFlag(cmd *cobra.Command, usage string) {
Expand Down Expand Up @@ -615,7 +618,19 @@ func newAWSIAMAuthProviderConfig() (*awsiam.IAMAuthParams, error) {
return awsiam.NewIAMAuthParams(host, opts...), nil
}

// getClient returns the process-wide API client. All callers — including the
// pre-run usage-stats report — share one client and therefore one underlying
// HTTP connection pool: a lakectl invocation opens a single TCP+TLS connection
// to the server instead of one per client construction, which matters when
// lakectl is invoked at high frequency (e.g. once per file in a pipeline).
func getClient() *apigen.ClientWithResponses {
apiClientOnce.Do(func() {
apiClient = newAPIClient()
})
return apiClient
}

func newAPIClient() *apigen.ClientWithResponses {
httpClient := getHTTPClient(lakectlRetryPolicy)
accessKeyID := cfg.Credentials.AccessKeyID
secretAccessKey := cfg.Credentials.SecretAccessKey
Expand Down
16 changes: 15 additions & 1 deletion cmd/lakectl/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"
"runtime"
"strings"
"sync"
"testing"

"github.com/go-openapi/swag"
Expand Down Expand Up @@ -346,7 +347,10 @@ func TestMaybeWarnEnterprise(t *testing.T) {

originalCfg := cfg
cfg = &Configuration{}
defer func() { cfg = originalCfg }()
defer func() {
cfg = originalCfg
resetAPIClientCache()
}()

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -362,6 +366,9 @@ func TestMaybeWarnEnterprise(t *testing.T) {
}))
defer server.Close()
cfg.Server.EndpointURL = lakefsconfig.OnlyString(server.URL)
// getClient memoizes the API client; clear it so this subtest's
// endpoint takes effect.
resetAPIClientCache()

var buf bytes.Buffer
tt.cmd.SetContext(t.Context())
Expand All @@ -376,3 +383,10 @@ func TestMaybeWarnEnterprise(t *testing.T) {
})
}
}

// resetAPIClientCache clears the memoized API client so a test can point
// getClient at a different server endpoint.
func resetAPIClientCache() {
apiClient = nil
apiClientOnce = sync.Once{}
}
Loading