Skip to content

Commit db609c2

Browse files
jiahui-grabtaxisanketplus
authored andcommitted
add comment and also refactoring for dbConn.Close()
1 parent 63c0f28 commit db609c2

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

locker_client.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ func (l MysqlLocker) ObtainTimeoutContext(ctx context.Context, key string, timeo
6666

6767
var res sql.NullInt32
6868
err = row.Scan(&res)
69-
if err != nil || !res.Valid || res.Int32 != int32(1) {
69+
if err != nil {
70+
//Close database connection whenever failed to acquire lock
7071
defer dbConn.Close()
71-
7272
// mysql error does not tell if it was due to context closing, checking it manually
7373
select {
7474
case <-ctx.Done():
@@ -80,11 +80,15 @@ func (l MysqlLocker) ObtainTimeoutContext(ctx context.Context, key string, timeo
8080
cancelFunc()
8181
return nil, fmt.Errorf("could not read mysql response: %w", err)
8282
} else if !res.Valid {
83+
//Close database connection whenever failed to acquire lock
84+
defer dbConn.Close()
8385
// Internal MySQL error occurred, such as out-of-memory, thread killed or others (the doc is not clear)
8486
// Note: some MySQL/MariaDB versions (like MariaDB 10.1) does not support -1 as timeout parameters
8587
cancelFunc()
8688
return nil, ErrMySQLInternalError
8789
} else if res.Int32 == 0 {
90+
//Close database connection whenever failed to acquire lock
91+
defer dbConn.Close()
8892
// MySQL Timeout
8993
cancelFunc()
9094
return nil, ErrMySQLTimeout

0 commit comments

Comments
 (0)