chore: refactor set in collection package (#5016)

This commit is contained in:
Kevin Wan
2025-07-18 21:25:40 +08:00
committed by GitHub
parent bf6ef5f033
commit 3c9b6335fb
5 changed files with 74 additions and 441 deletions

View File

@@ -43,8 +43,8 @@ func SetSlowThreshold(threshold time.Duration) {
// UnaryStatInterceptor returns a func that uses given metrics to report stats.
func UnaryStatInterceptor(metrics *stat.Metrics, conf StatConf) grpc.UnaryServerInterceptor {
staticNotLoggingContentMethods := collection.NewSet()
staticNotLoggingContentMethods.AddStr(conf.IgnoreContentMethods...)
staticNotLoggingContentMethods := collection.NewSet[string]()
staticNotLoggingContentMethods.Add(conf.IgnoreContentMethods...)
return func(ctx context.Context, req any, info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (resp any, err error) {
@@ -68,7 +68,7 @@ func isSlow(duration, durationThreshold time.Duration) bool {
}
func logDuration(ctx context.Context, method string, req any, duration time.Duration,
ignoreMethods *collection.Set, durationThreshold time.Duration) {
ignoreMethods *collection.Set[string], durationThreshold time.Duration) {
var addr string
client, ok := peer.FromContext(ctx)
if ok {
@@ -92,7 +92,7 @@ func logDuration(ctx context.Context, method string, req any, duration time.Dura
}
}
func shouldLogContent(method string, ignoreMethods *collection.Set) bool {
func shouldLogContent(method string, ignoreMethods *collection.Set[string]) bool {
_, ok := ignoreContentMethods.Load(method)
return !ok && !ignoreMethods.Contains(method)
}

View File

@@ -88,7 +88,7 @@ func TestLogDuration(t *testing.T) {
assert.NotPanics(t, func() {
logDuration(test.ctx, "foo", test.req, test.duration,
collection.NewSet(), test.durationThreshold)
collection.NewSet[string](), test.durationThreshold)
})
})
}
@@ -150,7 +150,7 @@ func TestLogDurationWithoutContent(t *testing.T) {
assert.NotPanics(t, func() {
logDuration(test.ctx, "foo", test.req, test.duration,
collection.NewSet(), test.durationThreshold)
collection.NewSet[string](), test.durationThreshold)
})
})
}
@@ -206,9 +206,10 @@ func Test_shouldLogContent(t *testing.T) {
t.Cleanup(func() {
ignoreContentMethods = sync.Map{}
})
set := collection.NewSet()
set.AddStr(tt.args.staticNotLoggingContentMethods...)
assert.Equalf(t, tt.want, shouldLogContent(tt.args.method, set), "shouldLogContent(%v, %v)", tt.args.method, tt.args.staticNotLoggingContentMethods)
set := collection.NewSet[string]()
set.Add(tt.args.staticNotLoggingContentMethods...)
assert.Equalf(t, tt.want, shouldLogContent(tt.args.method, set),
"shouldLogContent(%v, %v)", tt.args.method, tt.args.staticNotLoggingContentMethods)
})
}
}