Repository: zeromicro/go-zero
Pull request #1188 (2021-11-01 00:23:14)

feat: slow threshold customizable in sqlx


Patch:
diff --git a/core/stores/mongo/options.go b/core/stores/mongo/options.go
index f90c77ea..50313e85 100644
--- a/core/stores/mongo/options.go
+++ b/core/stores/mongo/options.go
@@ -17,6 +17,7 @@ type (
 	Option func(opts *options)
 )
 
+// SetSlowThreshold sets the slow threshold.
 func SetSlowThreshold(threshold time.Duration) {
 	slowThreshold.Set(threshold)
 }
diff --git a/core/stores/redis/conf.go b/core/stores/redis/conf.go
index 2d93723b..f8ee90c6 100644
--- a/core/stores/redis/conf.go
+++ b/core/stores/redis/conf.go
@@ -17,7 +17,7 @@ type (
 		Host string
 		Type string `json:",default=node,options=node|cluster"`
 		Pass string `json:",optional"`
-		Tls  bool   `json:",default=false,options=true|false"`
+		Tls  bool   `json:",optional"`
 	}
 
 	// A RedisKeyConf is a redis config with key.
diff --git a/core/stores/sqlx/stmt.go b/core/stores/sqlx/stmt.go
index ebefa1f4..87fc8c22 100644
--- a/core/stores/sqlx/stmt.go
+++ b/core/stores/sqlx/stmt.go
@@ -5,10 +5,18 @@ import (
 	"time"
 
 	"github.com/tal-tech/go-zero/core/logx"
+	"github.com/tal-tech/go-zero/core/syncx"
 	"github.com/tal-tech/go-zero/core/timex"
 )
 
-const slowThreshold = time.Millisecond * 500
+const defaultSlowThreshold = time.Millisecond * 500
+
+var slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
+
+// SetSlowThreshold sets the slow threshold.
+func SetSlowThreshold(threshold time.Duration) {
+	slowThreshold.Set(threshold)
+}
 
 func exec(conn sessionConn, q string, args ...interface{}) (sql.Result, error) {
 	stmt, err := format(q, args...)
@@ -19,7 +27,7 @@ func exec(conn sessionConn, q string, args ...interface{}) (sql.Result, error) {
 	startTime := timex.Now()
 	result, err := conn.Exec(q, args...)
 	duration := timex.Since(startTime)
-	if duration > slowThreshold {
+	if duration > slowThreshold.Load() {
 		logx.WithDuration(duration).Slowf("[SQL] exec: slowcall - %s", stmt)
 	} else {
 		logx.WithDuration(duration).Infof("sql exec: %s", stmt)
@@ -40,7 +48,7 @@ func execStmt(conn stmtConn, q string, args ...interface{}) (sql.Result, error)
 	startTime := timex.Now()
 	result, err := conn.Exec(args...)
 	duration := timex.Since(startTime)
-	if duration > slowThreshold {
+	if duration > slowThreshold.Load() {
 		logx.WithDuration(duration).Slowf("[SQL] execStmt: slowcall - %s", stmt)
 	} else {
 		logx.WithDuration(duration).Infof("sql execStmt: %s", stmt)
@@ -61,7 +69,7 @@ func query(conn sessionConn, scanner func(*sql.Rows) error, q string, args ...in
 	startTime := timex.Now()
 	rows, err := conn.Query(q, args...)
 	duration := timex.Since(startTime)
-	if duration > slowThreshold {
+	if duration > slowThreshold.Load() {
 		logx.WithDuration(duration).Slowf("[SQL] query: slowcall - %s", stmt)
 	} else {
 		logx.WithDuration(duration).Infof("sql query: %s", stmt)
@@ -84,7 +92,7 @@ func queryStmt(conn stmtConn, scanner func(*sql.Rows) error, q string, args ...i
 	startTime := timex.Now()
 	rows, err := conn.Query(args...)
 	duration := timex.Since(startTime)
-	if duration > slowThreshold {
+	if duration > slowThreshold.Load() {
 		logx.WithDuration(duration).Slowf("[SQL] queryStmt: slowcall - %s", stmt)
 	} else {
 		logx.WithDuration(duration).Infof("sql queryStmt: %s", stmt)


Test patch:
diff --git a/core/stores/sqlx/stmt_test.go b/core/stores/sqlx/stmt_test.go
index bff970a6..9a252afa 100644
--- a/core/stores/sqlx/stmt_test.go
+++ b/core/stores/sqlx/stmt_test.go
@@ -171,6 +171,12 @@ func TestStmt_query(t *testing.T) {
 	}
 }
 
+func TestSetSlowThreshold(t *testing.T) {
+	assert.Equal(t, defaultSlowThreshold, slowThreshold.Load())
+	SetSlowThreshold(time.Second)
+	assert.Equal(t, time.Second, slowThreshold.Load())
+}
+
 type mockedSessionConn struct {
 	lastInsertId int64
 	rowsAffected int64
@@ -180,7 +186,7 @@ type mockedSessionConn struct {
 
 func (m *mockedSessionConn) Exec(query string, args ...interface{}) (sql.Result, error) {
 	if m.delay {
-		time.Sleep(slowThreshold + time.Millisecond)
+		time.Sleep(defaultSlowThreshold + time.Millisecond)
 	}
 	return mockedResult{
 		lastInsertId: m.lastInsertId,
@@ -190,7 +196,7 @@ func (m *mockedSessionConn) Exec(query string, args ...interface{}) (sql.Result,
 
 func (m *mockedSessionConn) Query(query string, args ...interface{}) (*sql.Rows, error) {
 	if m.delay {
-		time.Sleep(slowThreshold + time.Millisecond)
+		time.Sleep(defaultSlowThreshold + time.Millisecond)
 	}
 
 	err := errMockedPlaceholder
@@ -209,7 +215,7 @@ type mockedStmtConn struct {
 
 func (m *mockedStmtConn) Exec(args ...interface{}) (sql.Result, error) {
 	if m.delay {
-		time.Sleep(slowThreshold + time.Millisecond)
+		time.Sleep(defaultSlowThreshold + time.Millisecond)
 	}
 	return mockedResult{
 		lastInsertId: m.lastInsertId,
@@ -219,7 +225,7 @@ func (m *mockedStmtConn) Exec(args ...interface{}) (sql.Result, error) {
 
 func (m *mockedStmtConn) Query(args ...interface{}) (*sql.Rows, error) {
 	if m.delay {
-		time.Sleep(slowThreshold + time.Millisecond)
+		time.Sleep(defaultSlowThreshold + time.Millisecond)
 	}
 
 	err := errMockedPlaceholder