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.

Comment From: a631807682

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

Comment From: Shubham510

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.

Comment From: a631807682

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

Comment From: dnanam

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

Comment From: 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.

Comment From: ploynomail

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

Comment From: Shubham510

@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.

Comment From: uded

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...