Skip to content
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
8 changes: 6 additions & 2 deletions bun/bunconnect/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ type ConnectionOptions struct {
MaxIdleConns int
MaxOpenConns int
ConnMaxIdleTime time.Duration
ConnMaxLifetime time.Duration
Connector func(dsn string) (driver.Connector, error) `json:",omitempty"`
}

func (opts ConnectionOptions) String() string {
return fmt.Sprintf("dsn=%s, max-idle-conns=%d, max-open-conns=%d, conn-max-idle-time=%s",
opts.DatabaseSourceName, opts.MaxIdleConns, opts.MaxOpenConns, opts.ConnMaxIdleTime)
return fmt.Sprintf("dsn=%s, max-idle-conns=%d, max-open-conns=%d, conn-max-idle-time=%s, conn-max-lifetime=%s",
opts.DatabaseSourceName, opts.MaxIdleConns, opts.MaxOpenConns, opts.ConnMaxIdleTime, opts.ConnMaxLifetime)
}

func OpenSQLDB(ctx context.Context, options ConnectionOptions, hooks ...bun.QueryHook) (*bun.DB, error) {
Expand All @@ -51,6 +52,9 @@ func OpenSQLDB(ctx context.Context, options ConnectionOptions, hooks ...bun.Quer
if options.ConnMaxIdleTime != 0 {
sqldb.SetConnMaxIdleTime(options.ConnMaxIdleTime)
}
if options.ConnMaxLifetime != 0 {
sqldb.SetConnMaxLifetime(options.ConnMaxLifetime)
}
if options.MaxOpenConns != 0 {
sqldb.SetMaxOpenConns(options.MaxOpenConns)
}
Expand Down
4 changes: 4 additions & 0 deletions bun/bunconnect/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ const (
PostgresMaxIdleConnsFlag = "postgres-max-idle-conns"
PostgresMaxOpenConnsFlag = "postgres-max-open-conns"
PostgresConnMaxIdleTimeFlag = "postgres-conn-max-idle-time"
PostgresConnMaxLifetimeFlag = "postgres-conn-max-lifetime"
)

func AddFlags(flags *pflag.FlagSet) {
flags.String(PostgresURIFlag, "", "Postgres URI")
flags.Bool(PostgresAWSEnableIAMFlag, false, "Enable AWS IAM authentication")
flags.Int(PostgresMaxIdleConnsFlag, 0, "Max Idle connections")
flags.Duration(PostgresConnMaxIdleTimeFlag, time.Minute, "Max Idle time for connections")
flags.Duration(PostgresConnMaxLifetimeFlag, 0, "Max lifetime for connections")
flags.Int(PostgresMaxOpenConnsFlag, 20, "Max opened connections")
}

Expand Down Expand Up @@ -87,12 +89,14 @@ func ConnectionOptionsFromFlags(cmd *cobra.Command, opts ...Option) (*Connection
}
maxIdleConns, _ := cmd.Flags().GetInt(PostgresMaxIdleConnsFlag)
connMaxIdleConns, _ := cmd.Flags().GetDuration(PostgresConnMaxIdleTimeFlag)
connMaxLifetime, _ := cmd.Flags().GetDuration(PostgresConnMaxLifetimeFlag)
maxOpenConns, _ := cmd.Flags().GetInt(PostgresMaxOpenConnsFlag)

return &ConnectionOptions{
DatabaseSourceName: postgresUri,
MaxIdleConns: maxIdleConns,
ConnMaxIdleTime: connMaxIdleConns,
ConnMaxLifetime: connMaxLifetime,
MaxOpenConns: maxOpenConns,
Connector: connector,
}, nil
Expand Down
1 change: 1 addition & 0 deletions bun/bunconnect/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func Module(connectionOptions ConnectionOptions, debug bool) fx.Option {
"max-idle-conns": connectionOptions.MaxIdleConns,
"max-open-conns": connectionOptions.MaxOpenConns,
"max-conn-max-idle-time": connectionOptions.ConnMaxIdleTime,
"conn-max-lifetime": connectionOptions.ConnMaxLifetime,
}).
Infof("opening database connection")

Expand Down
Loading