go-mysql-lock icon indicating copy to clipboard operation
go-mysql-lock copied to clipboard

DB connection not returned to connection pool if failed to acquire the lock

Open vanhuycntt opened this issue 3 years ago • 5 comments

I got my application hung after doing a load test. I dug into the library code and see that the database connection is not closed (return connection to the connection pool) if unable to obtain the lock.

vanhuycntt avatar Nov 03 '21 05:11 vanhuycntt

I think you are right. Connection obtained here https://github.com/sanketplus/go-mysql-lock/blob/master/locker_client.go#L59 is not closed if there is any issue with query execution or return code is unexpected. More than happy to review a PR if you are interested :)

sanketplus avatar Nov 03 '21 09:11 sanketplus

Slightly unrelated but we are not using connection pool here, rather we fetch/create an explicit connection and close it manually https://pkg.go.dev/database/sql#Conn.

Since a lock is held by a db connection, we do not want the connection to get back to the pool and still (unintentionally) hold the lock.

sanketplus avatar Nov 03 '21 09:11 sanketplus

Thank you for your explanation. I think we can fix it by returning the connection to the pool when got any issue with query execution or return code is unexpected like :

 dbConn, err := l.db.Conn(ctx)
 row := dbConn.QueryRowContext(ctx, "SELECT COALESCE(GET_LOCK(?, ?), 2)", key, timeout)

 var res int
 err = row.Scan(&res)
 if err != nil || res != 1 {
    defer func() {
        dbConn.Close()
    }
    // handle error or unexpected code return from MySQL as normal
    ....
 }
...	
	

Is it fine?

vanhuycntt avatar Nov 03 '21 14:11 vanhuycntt

@sanketplus We actually get the connection from the connection pool, https://pkg.go.dev/database/sql#DB.Conn, we can add defer dbConn.Close() soon after successfully getting a connection should help to manage all the places from where we return execution.

ankur-a avatar Nov 29 '21 12:11 ankur-a

apologies for the delayed response, I got busy with certain life events :)

@vanhuycntt doing just defer dbConn.Close() in case of error should be okay.

@ankur-a since a lock is tied to a connection, we cannot release the connection until lock is no longer needed.

sanketplus avatar Dec 05 '21 22:12 sanketplus

@sanketplus hello, we are using this awesome library in our application but we are facing this issue as we use this library in one of our long running tasks. created a PR here.. do you mind to spend some time to review and give feedback? thanks in advance. 🙏

jiahui-grabtaxi avatar Mar 15 '24 09:03 jiahui-grabtaxi

closed in #13

sanketplus avatar Apr 01 '24 03:04 sanketplus

@sanketplus sorry to disturb you again... do you mind to do a release say 0.0.7 for this fixes? it would help on importing to our go module 😄

jiahui-grabtaxi avatar May 13 '24 09:05 jiahui-grabtaxi

@jiahui911 done now. apologies I missed that :)

sanketplus avatar May 13 '24 18:05 sanketplus