From 7a1faf26a37cc198f33257002827acdf376b8616 Mon Sep 17 00:00:00 2001 From: cuiweixie Date: Fri, 22 Aug 2025 09:56:13 +0800 Subject: [PATCH] refactor(audit): using atomic type atomic.Uint32 --- audit/audit.go | 6 +++--- audit/interceptor.go | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/audit/audit.go b/audit/audit.go index 759bf3e18f8..24029b5c5af 100644 --- a/audit/audit.go +++ b/audit/audit.go @@ -24,7 +24,7 @@ const ( NodeTypeZero = "zero" ) -var auditEnabled uint32 +var auditEnabled atomic.Uint32 type AuditEvent struct { User string @@ -107,7 +107,7 @@ func InitAuditor(conf *x.LoggerConf, gId, nId uint64) error { if auditor.log, err = x.InitLogger(conf, filename); err != nil { return err } - atomic.StoreUint32(&auditEnabled, 1) + auditEnabled.Store(1) glog.Infoln("audit logs are enabled") return nil } @@ -116,7 +116,7 @@ func InitAuditor(conf *x.LoggerConf, gId, nId uint64) error { // It also sets the log to nil, because its being called by zero when license expires. // If license added, InitLogger will take care of the file. func Close() { - if atomic.LoadUint32(&auditEnabled) == 0 { + if auditEnabled.Load() == 0 { return } auditor.log.Sync() diff --git a/audit/interceptor.go b/audit/interceptor.go index c0399eb923a..694f3c632e5 100644 --- a/audit/interceptor.go +++ b/audit/interceptor.go @@ -68,7 +68,7 @@ func AuditRequestGRPC(ctx context.Context, req interface{}, return skipApis[info.FullMethod[strings.LastIndex(info.FullMethod, "/")+1:]] } - if atomic.LoadUint32(&auditEnabled) == 0 || skip(info.FullMethod) { + if auditEnabled.Load() == 0 || skip(info.FullMethod) { return handler(ctx, req) } response, err := handler(ctx, req) @@ -82,7 +82,7 @@ func AuditRequestHttp(next http.Handler) http.Handler { return skipEPs[r.URL.Path] } - if atomic.LoadUint32(&auditEnabled) == 0 || skip(r.URL.Path) { + if auditEnabled.Load() == 0 || skip(r.URL.Path) { next.ServeHTTP(w, r) return } @@ -110,7 +110,7 @@ func AuditRequestHttp(next http.Handler) http.Handler { } func AuditWebSockets(ctx context.Context, req *schema.Request) { - if atomic.LoadUint32(&auditEnabled) == 0 { + if auditEnabled.Load() == 0 { return }