gorm icon indicating copy to clipboard operation
gorm copied to clipboard

How to update the connection string for new connection in the conn pool?

Open Shubham510 opened this issue 2 years ago • 3 comments

Your Question

How to update the connection string for new connection in the conn pool?

The document you expected this should be explained

I am trying to use AWS IAM authentication for the connection to RDS instance. The auth token provided by i9t is only valid for 15 mins and any attempt to open new connection after that fails. Is there a way to update the connection string for the new connections being added to the connection pool by checking if the token has expired?

Expected answer

I want to be able to update the connection string, if the token has expired.

Shubham510 avatar Jul 20 '22 23:07 Shubham510

You can customize the timer and replace db or db.ConnPool after locking

a631807682 avatar Jul 21 '22 02:07 a631807682

You can customize the timer and replace db or db.ConnPool after locking

How do I put a lock and apply the change in a thread safe way. From what I have read so far, on changing the value for db, the old connections remain in an open state and are not closed.

Shubham510 avatar Jul 21 '22 05:07 Shubham510

https://gorm.io/docs/generic_interface.html#content-inner

a631807682 avatar Jul 21 '22 05:07 a631807682

@Shubham510 wondering if you ever got to a working solution by any chance?

dnanam avatar Dec 16 '22 14:12 dnanam

Ideally a callback function that gets executed whenever a connection is timing out would be awesome, this would be something close to https://github.com/jackc/pgx/blob/master/stdlib/sql.go#L112. I am not sure if there is something that is already there in the library and provides a similar mechanism, at least the docs did not point to me in that direction. Any input would be valuable.

dnanam avatar Dec 16 '22 14:12 dnanam

I also encountered this problem when using vault to dynamically obtain the mysql password in the gorm framework. Is there a solution? @Shubham510

ploynomail avatar Jun 13 '23 15:06 ploynomail

@ploynomail I solved it by creating my own custom driver on top of the existing one by implementing the Connect function in the driver.Connector interface. It refreshes the credentials when they expire and a new connection is required. Also max lifetime for existing connections in the pool is set to 15.

Shubham510 avatar Jun 14 '23 21:06 Shubham510

Sorry if the code is not super clean and debugged. I am writing by hand out of memory. It should work, though. And we have this code in production to handle AWS RDS token generation.

	pgxConfig, err := pgx.ParseConfig(dsn)
	if err != nil {
		panic()
	}

	optBeforeConnect := stdlib.OptionBeforeConnect(func(ctx context.Context, connConfig *pgx.ConnConfig) error {
		var err error
		connConfig.Database = database
		connConfig.Host = host
		connConfig.Port = port
		connConfig.User = user
		connConfig.Password = token
		connConfig.SSLMode = sslMode

		return err
	})

	sqlDB := stdlib.OpenDB(*pgxConfig, optBeforeConnect)

There is no need to write your driver. All components are already there from pgx package...

uded avatar Jun 17 '23 19:06 uded