Skip to content

Commit 790302b

Browse files
authored
fix: should not ignore slowThreshold (#4655)
1 parent 6a0672b commit 790302b

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

zrpc/internal/serverinterceptors/statinterceptor.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var (
2525

2626
// StatConf defines the static configuration for stat interceptor.
2727
type StatConf struct {
28-
SlowThreshold time.Duration `json:",optional"`
28+
SlowThreshold time.Duration `json:",default=500ms"`
2929
IgnoreContentMethods []string `json:",optional"`
3030
}
3131

@@ -63,7 +63,8 @@ func UnaryStatInterceptor(metrics *stat.Metrics, conf StatConf) grpc.UnaryServer
6363
}
6464

6565
func isSlow(duration, durationThreshold time.Duration) bool {
66-
return durationThreshold > 0 && duration > durationThreshold
66+
return duration > slowThreshold.Load() ||
67+
(durationThreshold > 0 && duration > durationThreshold)
6768
}
6869

6970
func logDuration(ctx context.Context, method string, req any, duration time.Duration,

zrpc/internal/serverinterceptors/statinterceptor_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ func Test_isSlow(t *testing.T) {
229229
args{
230230
duration: time.Millisecond * 501,
231231
},
232-
false,
232+
true,
233233
nil,
234234
},
235235
{
@@ -251,7 +251,8 @@ func Test_isSlow(t *testing.T) {
251251
t.Cleanup(func() {
252252
slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
253253
})
254-
assert.Equalf(t, tt.want, isSlow(tt.args.duration, tt.args.staticSlowThreshold), "isSlow(%v, %v)", tt.args.duration, tt.args.staticSlowThreshold)
254+
assert.Equalf(t, tt.want, isSlow(tt.args.duration, tt.args.staticSlowThreshold),
255+
"isSlow(%v, %v)", tt.args.duration, tt.args.staticSlowThreshold)
255256
})
256257
}
257258
}

0 commit comments

Comments
 (0)