-
-
Notifications
You must be signed in to change notification settings - Fork 408
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
BeforeConnect Hook #1875
Open
justcompile
wants to merge
6
commits into
go-pg:v10
Choose a base branch
from
justcompile:v10
base: v10
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
BeforeConnect Hook #1875
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
68e8bb8
added new hook to Options for catering for dynamic passwords and the …
7b867af
removed .gitignore
98f3ff4
Merge branch 'go-pg:v10' into v10
justcompile 6186baf
implemented options clone approach
22a4c8d
Merge branch 'v10' of github.com:go-pg/pg into v10
5a91137
Merge branch 'v10' of github.com:justcompile/pg into v10
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 |
---|---|---|
|
@@ -11,11 +11,20 @@ import ( | |
"runtime" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/go-pg/pg/v10/internal/pool" | ||
) | ||
|
||
type BeforeConnectOptions struct { | ||
User string | ||
Password string | ||
|
||
// TLS config for secure connections. | ||
TLSConfig *tls.Config | ||
} | ||
|
||
// Options contains database connection options. | ||
type Options struct { | ||
// Network type, either tcp or unix. | ||
|
@@ -28,6 +37,11 @@ type Options struct { | |
// Network and Addr options. | ||
Dialer func(ctx context.Context, network, addr string) (net.Conn, error) | ||
|
||
// BeforeConnect is a hook which is called before a new connection is | ||
// established. Useful for scenarios where dynamic passwords are used. | ||
// Timeout & Retry values set in this hook are not ignored | ||
BeforeConnect func(ctx context.Context, o *Options) error | ||
|
||
// Hook that is called after new connection is established | ||
// and user is authenticated. | ||
OnConnect func(ctx context.Context, cn *Conn) error | ||
|
@@ -90,6 +104,36 @@ type Options struct { | |
// but idle connections are still discarded by the client | ||
// if IdleTimeout is set. | ||
IdleCheckFrequency time.Duration | ||
|
||
mux sync.Mutex | ||
} | ||
|
||
func (opt *Options) clone() *Options { | ||
return &Options{ | ||
Network: opt.Network, | ||
Addr: opt.Addr, | ||
Dialer: opt.Dialer, | ||
BeforeConnect: opt.BeforeConnect, | ||
OnConnect: opt.OnConnect, | ||
User: opt.User, | ||
Password: opt.Password, | ||
Database: opt.Database, | ||
ApplicationName: opt.ApplicationName, | ||
TLSConfig: opt.TLSConfig.Clone(), | ||
DialTimeout: opt.DialTimeout, | ||
ReadTimeout: opt.ReadTimeout, | ||
WriteTimeout: opt.WriteTimeout, | ||
MaxRetries: opt.MaxRetries, | ||
RetryStatementTimeout: opt.RetryStatementTimeout, | ||
MinRetryBackoff: opt.MinRetryBackoff, | ||
MaxRetryBackoff: opt.MaxRetryBackoff, | ||
PoolSize: opt.PoolSize, | ||
MinIdleConns: opt.MinIdleConns, | ||
MaxConnAge: opt.MaxConnAge, | ||
PoolTimeout: opt.PoolTimeout, | ||
IdleTimeout: opt.IdleTimeout, | ||
IdleCheckFrequency: opt.IdleCheckFrequency, | ||
Comment on lines
+113
to
+135
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. Do these need to be |
||
} | ||
} | ||
|
||
func (opt *Options) init() { | ||
|
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.
I would add another test that tests the before connect with multiple go routines to make sure that a race condition is not triggered by accident. A race condition in a before connection hook for an ORM could cripple an application.
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 think a good way to do this would be to set the options to have a max pool of 1. And then try to send multiple queries concurrently in a few go routines.
This should trigger a concurrent call of the BeforeConnect method; and given the
-race
flag should be a sufficient enough smoke test for most use cases.