Browse Source

feat: slow threshold customizable in sqlx (#1188)

Kevin Wan 3 years ago
parent
commit
785d100be9
4 changed files with 25 additions and 10 deletions
  1. 1 0
      core/stores/mongo/options.go
  2. 1 1
      core/stores/redis/conf.go
  3. 13 5
      core/stores/sqlx/stmt.go
  4. 10 4
      core/stores/sqlx/stmt_test.go

+ 1 - 0
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)
 }

+ 1 - 1
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.

+ 13 - 5
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)

+ 10 - 4
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