-
Notifications
You must be signed in to change notification settings - Fork 6
Configurable http client timeout for Azure Blob Storage #162
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
Open
jochenehret
wants to merge
2
commits into
main
Choose a base branch
from
azure_http_timeouts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import ( | |
| "fmt" | ||
| "io" | ||
| "log/slog" | ||
| "net/http" | ||
| "os" | ||
| "strconv" | ||
| "strings" | ||
|
|
@@ -104,9 +105,45 @@ func createContext(dsc DefaultStorageClient) (context.Context, context.CancelFun | |
| } | ||
|
|
||
| type DefaultStorageClient struct { | ||
| credential *azblob.SharedKeyCredential | ||
| serviceURL string | ||
| storageConfig config.AZStorageConfig | ||
| credential *azblob.SharedKeyCredential | ||
| serviceURL string | ||
| storageConfig config.AZStorageConfig | ||
| httpRequestTimeout time.Duration | ||
| } | ||
|
|
||
| // clientOptions returns azblob.ClientOptions with a timeout-configured http.Client, | ||
| // or nil when no http_request_timeout is set (use SDK defaults). | ||
| func (dsc DefaultStorageClient) blockblobClientOptions() *blockblob.ClientOptions { | ||
| if dsc.httpRequestTimeout == 0 { | ||
| return nil | ||
| } | ||
| return &blockblob.ClientOptions{ | ||
| ClientOptions: azcore.ClientOptions{ | ||
| Transport: &http.Client{Timeout: dsc.httpRequestTimeout}, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (dsc DefaultStorageClient) blobClientOptions() *azBlob.ClientOptions { | ||
| if dsc.httpRequestTimeout == 0 { | ||
| return nil | ||
| } | ||
| return &azBlob.ClientOptions{ | ||
| ClientOptions: azcore.ClientOptions{ | ||
| Transport: &http.Client{Timeout: dsc.httpRequestTimeout}, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (dsc DefaultStorageClient) containerClientOptions() *azContainer.ClientOptions { | ||
|
Member
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. All 3 xxxClientOptions() methods have the same body. Could probably be simplified. |
||
| if dsc.httpRequestTimeout == 0 { | ||
| return nil | ||
| } | ||
| return &azContainer.ClientOptions{ | ||
| ClientOptions: azcore.ClientOptions{ | ||
| Transport: &http.Client{Timeout: dsc.httpRequestTimeout}, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func NewStorageClient(storageConfig config.AZStorageConfig) (StorageClient, error) { | ||
|
|
@@ -115,9 +152,19 @@ func NewStorageClient(storageConfig config.AZStorageConfig) (StorageClient, erro | |
| return nil, err | ||
| } | ||
|
|
||
| httpRequestTimeout, err := storageConfig.HTTPRequestTimeoutValue() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| serviceURL := fmt.Sprintf("https://%s.%s/%s", storageConfig.AccountName, storageConfig.StorageEndpoint(), storageConfig.ContainerName) | ||
|
|
||
| return DefaultStorageClient{credential: credential, serviceURL: serviceURL, storageConfig: storageConfig}, nil | ||
| return DefaultStorageClient{ | ||
| credential: credential, | ||
| serviceURL: serviceURL, | ||
| storageConfig: storageConfig, | ||
| httpRequestTimeout: httpRequestTimeout, | ||
| }, nil | ||
| } | ||
|
|
||
| func (dsc DefaultStorageClient) Upload( | ||
|
|
@@ -138,7 +185,7 @@ func (dsc DefaultStorageClient) Upload( | |
| } | ||
| defer cancel() | ||
|
|
||
| client, err := blockblob.NewClientWithSharedKeyCredential(blobURL, dsc.credential, nil) | ||
| client, err := blockblob.NewClientWithSharedKeyCredential(blobURL, dsc.credential, dsc.blockblobClientOptions()) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
@@ -173,7 +220,7 @@ func (dsc DefaultStorageClient) UploadStream( | |
| } | ||
| defer cancel() | ||
|
|
||
| client, err := blockblob.NewClientWithSharedKeyCredential(blobURL, dsc.credential, nil) | ||
| client, err := blockblob.NewClientWithSharedKeyCredential(blobURL, dsc.credential, dsc.blockblobClientOptions()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
@@ -196,7 +243,7 @@ func (dsc DefaultStorageClient) Download( | |
| ) error { | ||
| blobURL := fmt.Sprintf("%s/%s", dsc.serviceURL, source) | ||
| slog.Info("Downloading blob from container", "container", dsc.storageConfig.ContainerName, "blob", source, "local_file", dest.Name()) | ||
| client, err := blockblob.NewClientWithSharedKeyCredential(blobURL, dsc.credential, nil) | ||
| client, err := blockblob.NewClientWithSharedKeyCredential(blobURL, dsc.credential, dsc.blockblobClientOptions()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
@@ -226,7 +273,7 @@ func (dsc DefaultStorageClient) Copy( | |
| srcURL := fmt.Sprintf("%s/%s", dsc.serviceURL, srcBlob) | ||
| destURL := fmt.Sprintf("%s/%s", dsc.serviceURL, destBlob) | ||
|
|
||
| destClient, err := blockblob.NewClientWithSharedKeyCredential(destURL, dsc.credential, nil) | ||
| destClient, err := blockblob.NewClientWithSharedKeyCredential(destURL, dsc.credential, dsc.blockblobClientOptions()) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create destination client: %w", err) | ||
| } | ||
|
|
@@ -268,7 +315,7 @@ func (dsc DefaultStorageClient) Delete( | |
| blobURL := fmt.Sprintf("%s/%s", dsc.serviceURL, dest) | ||
|
|
||
| slog.Info("Deleting blob from container", "container", dsc.storageConfig.ContainerName, "blob", dest, "url", blobURL) | ||
| client, err := blockblob.NewClientWithSharedKeyCredential(blobURL, dsc.credential, nil) | ||
| client, err := blockblob.NewClientWithSharedKeyCredential(blobURL, dsc.credential, dsc.blockblobClientOptions()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
@@ -295,7 +342,7 @@ func (dsc DefaultStorageClient) DeleteRecursive( | |
| slog.Info("Deleting all blobs in container", "container", dsc.storageConfig.ContainerName) | ||
| } | ||
|
|
||
| containerClient, err := azContainer.NewClientWithSharedKeyCredential(dsc.serviceURL, dsc.credential, nil) | ||
| containerClient, err := azContainer.NewClientWithSharedKeyCredential(dsc.serviceURL, dsc.credential, dsc.containerClientOptions()) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create container client: %w", err) | ||
| } | ||
|
|
@@ -315,7 +362,7 @@ func (dsc DefaultStorageClient) DeleteRecursive( | |
|
|
||
| for _, blob := range resp.Segment.BlobItems { | ||
| blobURL := fmt.Sprintf("%s/%s", dsc.serviceURL, *blob.Name) | ||
| blobClient, err := blockblob.NewClientWithSharedKeyCredential(blobURL, dsc.credential, nil) | ||
| blobClient, err := blockblob.NewClientWithSharedKeyCredential(blobURL, dsc.credential, dsc.blockblobClientOptions()) | ||
| if err != nil { | ||
| slog.Error("Failed to create blob client", "blob", *blob.Name, "error", err) | ||
| continue | ||
|
|
@@ -338,7 +385,7 @@ func (dsc DefaultStorageClient) Exists( | |
| blobURL := fmt.Sprintf("%s/%s", dsc.serviceURL, dest) | ||
|
|
||
| slog.Info("Checking if blob exists", "container", dsc.storageConfig.ContainerName, "blob", dest, "url", blobURL) | ||
| client, err := blockblob.NewClientWithSharedKeyCredential(blobURL, dsc.credential, nil) | ||
| client, err := blockblob.NewClientWithSharedKeyCredential(blobURL, dsc.credential, dsc.blockblobClientOptions()) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
|
|
@@ -365,7 +412,7 @@ func (dsc DefaultStorageClient) SignedUrl( | |
| blobURL := fmt.Sprintf("%s/%s", dsc.serviceURL, dest) | ||
|
|
||
| slog.Info("Generating SAS URL for blob", "container", dsc.storageConfig.ContainerName, "blob", dest, "request_type", requestType, "expiration", expiration) | ||
| client, err := azBlob.NewClientWithSharedKeyCredential(blobURL, dsc.credential, nil) | ||
| client, err := azBlob.NewClientWithSharedKeyCredential(blobURL, dsc.credential, dsc.blobClientOptions()) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
@@ -398,7 +445,7 @@ func (dsc DefaultStorageClient) List( | |
| slog.Info("Listing blobs in container", "container", dsc.storageConfig.ContainerName) | ||
| } | ||
|
|
||
| client, err := azContainer.NewClientWithSharedKeyCredential(dsc.serviceURL, dsc.credential, nil) | ||
| client, err := azContainer.NewClientWithSharedKeyCredential(dsc.serviceURL, dsc.credential, dsc.containerClientOptions()) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create container client: %w", err) | ||
| } | ||
|
|
@@ -437,7 +484,7 @@ func (dsc DefaultStorageClient) Properties( | |
| blobURL := fmt.Sprintf("%s/%s", dsc.serviceURL, dest) | ||
|
|
||
| slog.Info("Getting properties for blob", "container", dsc.storageConfig.ContainerName, "blob", dest, "url", blobURL) | ||
| client, err := blockblob.NewClientWithSharedKeyCredential(blobURL, dsc.credential, nil) | ||
| client, err := blockblob.NewClientWithSharedKeyCredential(blobURL, dsc.credential, dsc.blockblobClientOptions()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
@@ -469,7 +516,7 @@ func (dsc DefaultStorageClient) Properties( | |
| func (dsc DefaultStorageClient) EnsureContainerExists() error { | ||
| slog.Info("Ensuring container exists", "container", dsc.storageConfig.ContainerName) | ||
|
|
||
| containerClient, err := azContainer.NewClientWithSharedKeyCredential(dsc.serviceURL, dsc.credential, nil) | ||
| containerClient, err := azContainer.NewClientWithSharedKeyCredential(dsc.serviceURL, dsc.credential, dsc.containerClientOptions()) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create container client: %w", err) | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
There is no transport set in the &http.Client{} which means that http.Client uses http.DefaultTransport instead of the default transport of the azure SDK.
http.DefaultTransport uses RenegotiateNever and has no HTTP/2 health-check pings. azcore's default transport uses RenegotiateFreelyAsClient, TLS 1.2 minimum, and ReadIdleTimeout: 10s for HTTP/2 pings. In environments with corporate proxies or Azure sovereign-cloud endpoints that request TLS renegotiation, all connections silently fail. The fix is to build a transport derived from azcore's defaults (via azidentity.DefaultTransportFromEnvironment or azcorehttp.NewDefaultTransport), set only the Timeout, and keep the RenegotiateFreelyAsClient config intact.