Skip to content
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

cmd/atlas/internal/docker: set client conn as ephemeral and limit its lifetime #3361

Merged
merged 1 commit into from
Feb 3, 2025
Merged
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
32 changes: 32 additions & 0 deletions cmd/atlas/internal/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type (
Database string
// Out is a custom writer to send docker cli output to.
Out io.Writer
// ConnOptions allows configuring the underlying connection pool.
ConnOptions *ConnOptions
}
// A Container is an instance of a created container.
Container struct {
Expand All @@ -56,6 +58,13 @@ type (
// Port on the host this containers service is bound to.
Port string
}
// ConnOptions allows configuring the underlying connection pool.
ConnOptions struct {
MaxOpen int
MaxIdle int
MaxLifetime time.Duration
MaxIdleTime time.Duration
}
// ConfigOption allows configuring Config with functional arguments.
ConfigOption func(*Config) error
)
Expand Down Expand Up @@ -369,6 +378,14 @@ func Setup(s ...string) ConfigOption {
}
}

// Conn sets the config connection configuration.
func Conn(s *ConnOptions) ConfigOption {
return func(c *Config) error {
c.ConnOptions = s
return nil
}
}

// Run pulls and starts a new docker container from the Config.
func (c *Config) Run(ctx context.Context) (*Container, error) {
// Make sure the configuration is not missing critical values.
Expand Down Expand Up @@ -586,6 +603,21 @@ func OpenWithOpts(ctx context.Context, u *url.URL, opts ...ConfigOption) (client
if client, err = sqlclient.Open(ctx, u1.String()); err != nil {
return nil, err
}
client.Ephemeral = true
if s := c.ConnOptions; s != nil {
if s.MaxOpen > 0 {
client.DB.SetMaxOpenConns(s.MaxOpen)
}
if s.MaxIdle > 0 {
client.DB.SetMaxIdleConns(s.MaxIdle)
}
if s.MaxLifetime > 0 {
client.DB.SetConnMaxLifetime(s.MaxLifetime)
}
if s.MaxIdleTime > 0 {
client.DB.SetConnMaxIdleTime(s.MaxIdleTime)
}
}
client.AddClosers(c)
return client, nil
}
6 changes: 6 additions & 0 deletions sql/sqlclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ type (
schemahcl.Marshaler
schemahcl.Evaluator

// Ephemeral indicates that the database we connect to is "ephemeral"
// (e.g., a temporary running container). This can be set by the driver
// that opens the client to signal to its consumers that there is no need
// to guard against race conditions with other Atlas clients.
Ephemeral bool

// Functions registered by the drivers and used for opening transactions and their clients.
openDriver func(schema.ExecQuerier) (migrate.Driver, error)
openTx TxOpener
Expand Down
Loading