feat: slow threshold customizable in redis (#1185)

* feat: slow threshold customizable in redis

* chore: improve config robustness
This commit is contained in:
Kevin Wan
2021-10-31 22:14:20 +08:00
committed by GitHub
parent b4d1c6da2c
commit 429f85a9de
8 changed files with 83 additions and 33 deletions

16
core/conf/time.go Normal file
View File

@@ -0,0 +1,16 @@
package conf
import "time"
const minDuration = 100 * time.Microsecond
// CheckedDuration returns the duration that guaranteed to be greater than 100us.
// Why we need this is because users sometimes intend to use 500 to represent 500ms.
// In config, duration less than 100us should always be missing ms etc.
func CheckedDuration(duration time.Duration) time.Duration {
if duration > minDuration {
return duration
}
return duration * time.Millisecond
}

14
core/conf/time_test.go Normal file
View File

@@ -0,0 +1,14 @@
package conf
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestCheckedDuration(t *testing.T) {
assert.Equal(t, time.Second, CheckedDuration(1000))
assert.Equal(t, 2*time.Second, CheckedDuration(2000))
assert.Equal(t, 2*time.Second, CheckedDuration(time.Second*2))
}