Skip to content

Commit

Permalink
Merge pull request #766 from raj-prince/add_storage_handle_file
Browse files Browse the repository at this point in the history
Adding API to Create Storage Client Handle
  • Loading branch information
vadlakondaswetha authored Aug 23, 2022
2 parents f7ce917 + c945861 commit 64b65e2
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
70 changes: 70 additions & 0 deletions internal/storage/storage_handle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package storage

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

"cloud.google.com/go/storage"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"google.golang.org/api/option"
)

type storageClient struct {
client *storage.Client
}

type storageClientConfig struct {
disableHTTP2 bool
maxConnsPerHost int
maxIdleConnsPerHost int
tokenSrc oauth2.TokenSource
timeOut time.Duration
}

// NewStorageHandle returns the handle of Go storage client containing
// customized http client. We can configure the http client using the
// storageClientConfig parameter.
func NewStorageHandle(ctx context.Context,
clientConfig storageClientConfig) (sh *storageClient, err error) {
var transport *http.Transport
// Disabling the http2 makes the client more performant.
if clientConfig.disableHTTP2 {
transport = &http.Transport{
MaxConnsPerHost: clientConfig.maxConnsPerHost,
MaxIdleConnsPerHost: clientConfig.maxIdleConnsPerHost,
// This disables HTTP/2 in transport.
TLSNextProto: make(
map[string]func(string, *tls.Conn) http.RoundTripper,
),
}
} else {
// For http2, change in MaxConnsPerHost doesn't affect the performance.
transport = &http.Transport{
DisableKeepAlives: true,
MaxConnsPerHost: clientConfig.maxConnsPerHost,
ForceAttemptHTTP2: true,
}
}

// Custom http client for Go Client.
httpClient := &http.Client{
Transport: &oauth2.Transport{
Base: transport,
Source: clientConfig.tokenSrc,
},
Timeout: clientConfig.timeOut,
}

var sc *storage.Client
sc, err = storage.NewClient(ctx, option.WithHTTPClient(httpClient))
if err != nil {
err = fmt.Errorf("go storage client creation failed: %w", err)
return
}

sh = &storageClient{sc}
return
}
53 changes: 53 additions & 0 deletions internal/storage/storage_handle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package storage

import (
"context"
"testing"
"time"

"golang.org/x/oauth2"
)

func invokeAndVerifyStorageHandle(t *testing.T, sc storageClientConfig) {
handleCreated, err := NewStorageHandle(context.Background(), sc)

if err != nil {
t.Errorf("Handle creation failure")
}
if nil == handleCreated {
t.Fatalf("Storage handle is null")
}
if nil == handleCreated.client {
t.Fatalf("Storage client handle is null")
}
}

func TestNewStorageHandleHttp2Disabled(t *testing.T) {
sc := storageClientConfig{disableHTTP2: true,
maxConnsPerHost: 10,
maxIdleConnsPerHost: 100,
tokenSrc: oauth2.StaticTokenSource(&oauth2.Token{}),
timeOut: 800 * time.Millisecond}

invokeAndVerifyStorageHandle(t, sc)
}

func TestNewStorageHandleHttp2Enabled(t *testing.T) {
sc := storageClientConfig{disableHTTP2: false,
maxConnsPerHost: 10,
maxIdleConnsPerHost: 100,
tokenSrc: oauth2.StaticTokenSource(&oauth2.Token{}),
timeOut: 800 * time.Millisecond}

invokeAndVerifyStorageHandle(t, sc)
}

func TestNewStorageHandleWithZeroMaxConnsPerHost(t *testing.T) {
sc := storageClientConfig{disableHTTP2: true,
maxConnsPerHost: 0,
maxIdleConnsPerHost: 100,
tokenSrc: oauth2.StaticTokenSource(&oauth2.Token{}),
timeOut: 800 * time.Millisecond}

invokeAndVerifyStorageHandle(t, sc)
}

0 comments on commit 64b65e2

Please sign in to comment.