Skip to content

Commit

Permalink
cmd/atlas/internal/docker: set client conn as ephemeral and limit its…
Browse files Browse the repository at this point in the history
… lifetime
  • Loading branch information
a8m committed Feb 3, 2025
1 parent 6e3b402 commit ac2fb0f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
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

0 comments on commit ac2fb0f

Please sign in to comment.