diff --git a/.golangci.yml b/.golangci.yml index bb7d6eda5..e558827c0 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -56,15 +56,12 @@ linters: - staticcheck - ineffassign - misspell - # - errcheck - asciicheck - bodyclose - rowserrcheck - #- makezero + - gofmt - durationcheck - # - prealloc - # - predeclared - + - sqlclosecheck run: diff --git a/pkg/datasource/sql/datasource/mysql/trigger.go b/pkg/datasource/sql/datasource/mysql/trigger.go index d9339a6d1..689ad2c73 100644 --- a/pkg/datasource/sql/datasource/mysql/trigger.go +++ b/pkg/datasource/sql/datasource/mysql/trigger.go @@ -93,6 +93,7 @@ func (m *mysqlTrigger) getColumnMetas(ctx context.Context, dbName string, table if err != nil { return nil, err } + defer stmt.Close() rows, err := stmt.Query(dbName, table) if err != nil { @@ -164,7 +165,7 @@ func (m *mysqlTrigger) getIndexes(ctx context.Context, dbName string, tableName if err != nil { return nil, err } - + defer stmt.Close() rows, err := stmt.Query(dbName, tableName) if err != nil { return nil, err diff --git a/pkg/datasource/sql/exec/at/escape.go b/pkg/datasource/sql/exec/at/escape.go index 3133b40d6..bd8d1c6ab 100644 --- a/pkg/datasource/sql/exec/at/escape.go +++ b/pkg/datasource/sql/exec/at/escape.go @@ -142,7 +142,7 @@ func addEscape(colName string, dbType types.DBType, escape string) string { buf := make([]byte, len(colName)+2) buf[0], buf[len(buf)-1] = escape[0], escape[0] - for key, _ := range colName { + for key := range colName { buf[key+1] = colName[key] } diff --git a/pkg/datasource/sql/tx.go b/pkg/datasource/sql/tx.go index a6c5f70ff..db55e97d1 100644 --- a/pkg/datasource/sql/tx.go +++ b/pkg/datasource/sql/tx.go @@ -166,7 +166,7 @@ func (tx *Tx) register(ctx *types.TransactionContext) error { if !ctx.HasUndoLog() || !ctx.HasLockKey() { return nil } - for k, _ := range ctx.LockKeys { + for k := range ctx.LockKeys { lockKey += k + ";" } request.LockKeys = lockKey diff --git a/pkg/datasource/sql/types/image.go b/pkg/datasource/sql/types/image.go index f755d9c91..0290b3660 100644 --- a/pkg/datasource/sql/types/image.go +++ b/pkg/datasource/sql/types/image.go @@ -146,7 +146,7 @@ func (r *RowImage) GetColumnMap() map[string]*ColumnImage { // PrimaryKeys Primary keys list. func (r *RowImage) PrimaryKeys(cols []ColumnImage) []ColumnImage { var pkFields []ColumnImage - for key, _ := range cols { + for key := range cols { if cols[key].KeyType == PrimaryKey.Number() { pkFields = append(pkFields, cols[key]) } @@ -158,7 +158,7 @@ func (r *RowImage) PrimaryKeys(cols []ColumnImage) []ColumnImage { // NonPrimaryKeys get non-primary keys func (r *RowImage) NonPrimaryKeys(cols []ColumnImage) []ColumnImage { var nonPkFields []ColumnImage - for key, _ := range cols { + for key := range cols { if cols[key].KeyType != PrimaryKey.Number() { nonPkFields = append(nonPkFields, cols[key]) } diff --git a/pkg/datasource/sql/undo/base/undo.go b/pkg/datasource/sql/undo/base/undo.go index 394cacbd6..752597fbf 100644 --- a/pkg/datasource/sql/undo/base/undo.go +++ b/pkg/datasource/sql/undo/base/undo.go @@ -106,6 +106,8 @@ func (m *BaseUndoLogManager) InsertUndoLogWithSqlConn(ctx context.Context, recor if err != nil { return err } + defer stmt.Close() + _, err = stmt.Exec(record.BranchID, record.XID, record.Context, record.RollbackInfo, int64(record.LogStatus)) if err != nil { return err @@ -120,7 +122,7 @@ func (m *BaseUndoLogManager) DeleteUndoLog(ctx context.Context, xid string, bran log.Errorf("[DeleteUndoLog] prepare sql fail, err: %v", err) return err } - + defer stmt.Close() if _, err = stmt.Exec(branchID, xid); err != nil { log.Errorf("[DeleteUndoLog] exec delete undo log fail, err: %v", err) return err @@ -146,6 +148,7 @@ func (m *BaseUndoLogManager) BatchDeleteUndoLog(xid []string, branchID []int64, log.Errorf("prepare sql fail, err: %v", err) return err } + defer stmt.Close() branchIDStr, err := Int64Slice2Str(branchID, ",") if err != nil { @@ -413,7 +416,7 @@ func (m *BaseUndoLogManager) DBType() types.DBType { // HasUndoLogTable check undo log table if exist func (m *BaseUndoLogManager) HasUndoLogTable(ctx context.Context, conn *sql.Conn) (res bool, err error) { - if _, err = conn.QueryContext(ctx, getCheckUndoLogTableExistSql()); err != nil { //nolint:rowserrcheck + if _, err = conn.QueryContext(ctx, getCheckUndoLogTableExistSql()); err != nil { //nolint:rowserrcheck,sqlclosecheck // 1146 mysql table not exist fault code if e, ok := err.(*mysql.SQLError); ok && e.Code == mysql.ErrNoSuchTable { return false, nil diff --git a/pkg/datasource/sql/undo/executor/executor.go b/pkg/datasource/sql/undo/executor/executor.go index f4c9aed46..f5b573a6a 100644 --- a/pkg/datasource/sql/undo/executor/executor.go +++ b/pkg/datasource/sql/undo/executor/executor.go @@ -124,7 +124,7 @@ func (b *BaseExecutor) queryCurrentRecords(ctx context.Context, conn *sql.Conn) if err != nil { return nil, err } - + defer rows.Close() image := types.RecordImage{ TableName: b.undoImage.TableName, TableMeta: tableMeta, diff --git a/pkg/datasource/sql/undo/executor/mysql_undo_delete_executor.go b/pkg/datasource/sql/undo/executor/mysql_undo_delete_executor.go index 7a392fc98..02045c3df 100644 --- a/pkg/datasource/sql/undo/executor/mysql_undo_delete_executor.go +++ b/pkg/datasource/sql/undo/executor/mysql_undo_delete_executor.go @@ -48,7 +48,7 @@ func (m *mySQLUndoDeleteExecutor) ExecuteOn(ctx context.Context, dbType types.DB if err != nil { return err } - + defer stmt.Close() beforeImage := m.sqlUndoLog.BeforeImage for _, row := range beforeImage.Rows { @@ -97,7 +97,7 @@ func (m *mySQLUndoDeleteExecutor) buildUndoSQL(dbType types.DBType) (string, err insertColumnSlice, insertValueSlice []string ) - for key, _ := range fields { + for key := range fields { insertColumnSlice = append(insertColumnSlice, AddEscape(fields[key].ColumnName, dbType)) insertValueSlice = append(insertValueSlice, "?") } diff --git a/pkg/datasource/sql/undo/executor/mysql_undo_insert_executor.go b/pkg/datasource/sql/undo/executor/mysql_undo_insert_executor.go index 108a62e64..bd80804d5 100644 --- a/pkg/datasource/sql/undo/executor/mysql_undo_insert_executor.go +++ b/pkg/datasource/sql/undo/executor/mysql_undo_insert_executor.go @@ -50,7 +50,7 @@ func (m *mySQLUndoInsertExecutor) ExecuteOn(ctx context.Context, dbType types.DB if err != nil { return err } - + defer stmt.Close() afterImage := m.sqlUndoLog.AfterImage for _, row := range afterImage.Rows { pkValueList := make([]interface{}, 0) @@ -96,7 +96,7 @@ func (m *mySQLUndoInsertExecutor) generateDeleteSql( } var pkList []string - for key, _ := range colImages { + for key := range colImages { pkList = append(pkList, colImages[key].ColumnName) } diff --git a/pkg/datasource/sql/undo/executor/mysql_undo_update_executor.go b/pkg/datasource/sql/undo/executor/mysql_undo_update_executor.go index d3561c449..65e132188 100644 --- a/pkg/datasource/sql/undo/executor/mysql_undo_update_executor.go +++ b/pkg/datasource/sql/undo/executor/mysql_undo_update_executor.go @@ -54,6 +54,7 @@ func (m *mySQLUndoUpdateExecutor) ExecuteOn(ctx context.Context, dbType types.DB if err != nil { return err } + defer stmt.Close() beforeImage := m.sqlUndoLog.BeforeImage for _, row := range beforeImage.Rows { @@ -93,7 +94,7 @@ func (m *mySQLUndoUpdateExecutor) buildUndoSQL(dbType types.DBType) (string, err ) nonPkFields := row.NonPrimaryKeys(row.Columns) - for key, _ := range nonPkFields { + for key := range nonPkFields { updateColumnSlice = append(updateColumnSlice, AddEscape(nonPkFields[key].ColumnName, dbType)+" = ? ") } @@ -103,7 +104,7 @@ func (m *mySQLUndoUpdateExecutor) buildUndoSQL(dbType types.DBType) (string, err return "", err } - for key, _ := range pkList { + for key := range pkList { pkNameList = append(pkNameList, pkList[key].ColumnName) } diff --git a/pkg/datasource/sql/undo/executor/sql.go b/pkg/datasource/sql/undo/executor/sql.go index 104baced0..5594a4436 100644 --- a/pkg/datasource/sql/undo/executor/sql.go +++ b/pkg/datasource/sql/undo/executor/sql.go @@ -142,7 +142,7 @@ func addEscape(colName string, dbType types.DBType, escape string) string { buf := make([]byte, len(colName)+2) buf[0], buf[len(buf)-1] = escape[0], escape[0] - for key, _ := range colName { + for key := range colName { buf[key+1] = colName[key] } diff --git a/pkg/remoting/getty/readwriter.go b/pkg/remoting/getty/readwriter.go index 5232315f8..321648969 100644 --- a/pkg/remoting/getty/readwriter.go +++ b/pkg/remoting/getty/readwriter.go @@ -58,9 +58,9 @@ var ( var ( ErrNotEnoughStream = errors.New("packet stream is not enough") - ErrTooLargePackage = errors.New("package length is exceed the getty package's legal maximum length.") + ErrTooLargePackage = errors.New("package length is exceed the getty package's legal maximum length") ErrInvalidPackage = errors.New("invalid rpc package") - ErrIllegalMagic = errors.New("package magic is not right.") + ErrIllegalMagic = errors.New("package magic is not right") ) type RpcPackageHandler struct{} @@ -141,7 +141,7 @@ func (p *RpcPackageHandler) Write(ss getty.Session, pkg interface{}) ([]byte, er headLength := message.V1HeadLength var headMapBytes []byte - if msg.HeadMap != nil && len(msg.HeadMap) > 0 { + if len(msg.HeadMap) > 0 { hb, headMapLength := encodeHeapMap(msg.HeadMap) headMapBytes = hb headLength += headMapLength diff --git a/pkg/remoting/loadbalance/loadbalance.go b/pkg/remoting/loadbalance/loadbalance.go index 5704eb39a..abfb6b6f7 100644 --- a/pkg/remoting/loadbalance/loadbalance.go +++ b/pkg/remoting/loadbalance/loadbalance.go @@ -40,7 +40,7 @@ func Select(loadBalanceType string, sessions *sync.Map, xid string) getty.Sessio case consistentHashLoadBalance: return ConsistentHashLoadBalance(sessions, xid) case leastActiveLoadBalance: - return LeastActiveLoadBalance(sessions, xid) + return LeastActiveLoadBalance(sessions, xid) case roundRobinLoadBalance: return RoundRobinLoadBalance(sessions, xid) default: