Orphaned DB Connections with refreshing DB credentials from RDS IAM Auth

Viewed 33

Currently testing out RDS IAM Auth in a Golang service.

My service maintains pointer to the DB connection in the struct. So every time connection is refreshed, I will update the pointer to the DB Connection (i.e *sqlx.DB) in my service. However, the problem I'm facing is with race conditions on long-running queries. And i've been testing with/without closing the old connection after connection refresh.

Scenario 1:

  • Query begins with connection A
  • New connection created, refreshes to connection B, close connection A
  • Query ends, but error occurs because connection A is closed.

Scenario 2:

  • Query begins with connection A
  • New connection created, refresh to connection B, DON'T close connection A
  • Query ends, no error because connection still lives.
  • There is no longer a reference to connection A, but since we did not close the connection, I can see an Idle connection lives in the DB.

I was playing with MaxIdleConnections but somehow idle connection would exceed the number mentioned.

Any ideas in general of how to correctly handle these types of race conditions with refreshing credentials?

0 Answers
Related