fix: 完成商务迁移复核与接口优化并清理历史资料
This commit is contained in:
@@ -10,9 +10,11 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
dataScopeAuditQueueSize = 1024
|
||||
dataScopeAuditBatchSize = 100
|
||||
dataScopeAuditInterval = 2 * time.Second
|
||||
dataScopeAuditQueueSize = 1024
|
||||
dataScopeAuditBatchSize = 100
|
||||
dataScopeAuditInterval = 2 * time.Second
|
||||
dataScopeAuditWriteTimeout = 2 * time.Second
|
||||
dataScopeAuditCloseTimeout = 5 * time.Second
|
||||
)
|
||||
|
||||
// dataScopeAuditWriter keeps row-scope auditing best-effort: callbacks only
|
||||
@@ -26,6 +28,8 @@ type dataScopeAuditWriter struct {
|
||||
stop chan struct{}
|
||||
done chan struct{}
|
||||
closeOnce sync.Once
|
||||
enqueueMu sync.RWMutex
|
||||
closed bool
|
||||
}
|
||||
|
||||
func newDataScopeAuditWriter(data *Data, logger *slog.Logger) *dataScopeAuditWriter {
|
||||
@@ -57,6 +61,11 @@ func (w *dataScopeAuditWriter) Enqueue(record dataAccessLogPO) {
|
||||
if w == nil {
|
||||
return
|
||||
}
|
||||
w.enqueueMu.RLock()
|
||||
defer w.enqueueMu.RUnlock()
|
||||
if w.closed {
|
||||
return
|
||||
}
|
||||
select {
|
||||
case w.queue <- record:
|
||||
default:
|
||||
@@ -68,8 +77,19 @@ func (w *dataScopeAuditWriter) Close() {
|
||||
if w == nil {
|
||||
return
|
||||
}
|
||||
w.closeOnce.Do(func() { close(w.stop) })
|
||||
<-w.done
|
||||
w.closeOnce.Do(func() {
|
||||
w.enqueueMu.Lock()
|
||||
w.closed = true
|
||||
close(w.stop)
|
||||
w.enqueueMu.Unlock()
|
||||
})
|
||||
timer := time.NewTimer(dataScopeAuditCloseTimeout)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-w.done:
|
||||
case <-timer.C:
|
||||
w.log().Warn("等待数据权限审计写入超时", "mod", "data-scope")
|
||||
}
|
||||
}
|
||||
|
||||
func (w *dataScopeAuditWriter) run() {
|
||||
@@ -78,6 +98,7 @@ func (w *dataScopeAuditWriter) run() {
|
||||
defer ticker.Stop()
|
||||
batch := make([]dataAccessLogPO, 0, w.batchSize)
|
||||
flush := func() {
|
||||
defer func() { batch = batch[:0] }()
|
||||
if len(batch) == 0 || w.data == nil || w.data.gormDB == nil {
|
||||
return
|
||||
}
|
||||
@@ -85,10 +106,11 @@ func (w *dataScopeAuditWriter) run() {
|
||||
if db == nil {
|
||||
return
|
||||
}
|
||||
if err := db.WithContext(context.Background()).Session(&gorm.Session{NewDB: true, SkipHooks: true}).Create(&batch).Error; err != nil {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), dataScopeAuditWriteTimeout)
|
||||
defer cancel()
|
||||
if err := db.WithContext(ctx).Session(&gorm.Session{NewDB: true, SkipHooks: true}).Create(&batch).Error; err != nil {
|
||||
w.log().Warn("数据权限审计批量写入失败", "mod", "data-scope", "error", err)
|
||||
}
|
||||
batch = batch[:0]
|
||||
}
|
||||
for {
|
||||
select {
|
||||
|
||||
Reference in New Issue
Block a user