-
Notifications
You must be signed in to change notification settings - Fork 217
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
Handle Client.Close on cloned clients #893
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3d211c4
Use counter on close for reused-connection clients
cretz 7ed26b8
Merge branch 'master' into cloned-client-close
cretz 82071cd
Properly close client when done with it
cretz e109a53
Merge branch 'master' into cloned-client-close
cretz 0c29894
Merge branch 'master' into cloned-client-close
cretz 2098f45
Minor PR fix
cretz 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 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 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 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 |
---|---|---|
|
@@ -29,8 +29,10 @@ import ( | |
"errors" | ||
"fmt" | ||
"io" | ||
"math" | ||
"reflect" | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/pborman/uuid" | ||
|
@@ -83,6 +85,10 @@ type ( | |
excludeInternalFromRetry *uberatomic.Bool | ||
capabilities *workflowservice.GetSystemInfoResponse_Capabilities | ||
capabilitiesLock sync.RWMutex | ||
|
||
// The pointer value is shared across multiple clients. If non-nil, only | ||
// access/mutate atomically. | ||
unclosedClients *int32 | ||
} | ||
|
||
// namespaceClient is the client for managing namespaces. | ||
|
@@ -942,11 +948,25 @@ func (wc *WorkflowClient) ensureInitialized() error { | |
|
||
// Close client and clean up underlying resources. | ||
func (wc *WorkflowClient) Close() { | ||
if wc.conn == nil { | ||
return | ||
// If there's a set of unclosed clients, we have to decrement it and then | ||
// set it to a new pointer of max to prevent decrementing on repeated Close | ||
// calls to this client. If the count has not reached zero, this close call is | ||
// ignored. | ||
if wc.unclosedClients != nil { | ||
remainingUnclosedClients := atomic.AddInt32(wc.unclosedClients, -1) | ||
// Set the unclosed clients to max value so we never try this again | ||
var maxUnclosedClients int32 = math.MaxInt32 | ||
wc.unclosedClients = &maxUnclosedClients | ||
// If there are any remaining, do not close | ||
if remainingUnclosedClients > 0 { | ||
return | ||
} | ||
} | ||
if err := wc.conn.Close(); err != nil { | ||
wc.logger.Warn("unable to close connection", tagError, err) | ||
|
||
if wc.conn != nil { | ||
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. when is this nil? 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. Test cases w/ mocks. This logic has not changed, I just moved the condition from the top of the function to down here. |
||
if err := wc.conn.Close(); err != nil { | ||
wc.logger.Warn("unable to close connection", tagError, err) | ||
} | ||
} | ||
} | ||
|
||
|
This file contains 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 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
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.
this shouldn't ever be nil, right?
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.
I cannot trust all places that create a client will set this. That includes tests and other code paths.