Skip to content

Commit

Permalink
add comment and also refactoring for dbConn.Close()
Browse files Browse the repository at this point in the history
  • Loading branch information
jiahui-grabtaxi authored and sanketplus committed Apr 1, 2024
1 parent 63c0f28 commit db609c2
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions locker_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ func (l MysqlLocker) ObtainTimeoutContext(ctx context.Context, key string, timeo

var res sql.NullInt32
err = row.Scan(&res)
if err != nil || !res.Valid || res.Int32 != int32(1) {
if err != nil {
//Close database connection whenever failed to acquire lock
defer dbConn.Close()

// mysql error does not tell if it was due to context closing, checking it manually
select {
case <-ctx.Done():
Expand All @@ -80,11 +80,15 @@ func (l MysqlLocker) ObtainTimeoutContext(ctx context.Context, key string, timeo
cancelFunc()
return nil, fmt.Errorf("could not read mysql response: %w", err)
} else if !res.Valid {
//Close database connection whenever failed to acquire lock
defer dbConn.Close()
// Internal MySQL error occurred, such as out-of-memory, thread killed or others (the doc is not clear)
// Note: some MySQL/MariaDB versions (like MariaDB 10.1) does not support -1 as timeout parameters
cancelFunc()
return nil, ErrMySQLInternalError
} else if res.Int32 == 0 {
//Close database connection whenever failed to acquire lock
defer dbConn.Close()
// MySQL Timeout
cancelFunc()
return nil, ErrMySQLTimeout
Expand Down

0 comments on commit db609c2

Please sign in to comment.