mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
feat(server): implement management endpoints and redis sync
Enable full resource management via API and support data plane synchronization. - Add CRUD handlers for Providers, Models, and Keys using DTOs - Implement LogWriter service for asynchronous, batched audit logging - Update SyncService to snapshot full configuration state to Redis - Register new API routes and initialize background services - Add configuration options for logging performance tuning
This commit is contained in:
79
internal/service/logger.go
Normal file
79
internal/service/logger.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/ez-api/ez-api/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// LogWriter batches log records to reduce IO overhead.
|
||||
type LogWriter struct {
|
||||
ch chan model.LogRecord
|
||||
batchSize int
|
||||
flushInterval time.Duration
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewLogWriter(db *gorm.DB, queueCapacity, batchSize int, flushInterval time.Duration) *LogWriter {
|
||||
if batchSize <= 0 {
|
||||
batchSize = 10
|
||||
}
|
||||
if queueCapacity <= 0 {
|
||||
queueCapacity = 1000
|
||||
}
|
||||
if flushInterval <= 0 {
|
||||
flushInterval = time.Second
|
||||
}
|
||||
return &LogWriter{
|
||||
ch: make(chan model.LogRecord, queueCapacity),
|
||||
batchSize: batchSize,
|
||||
flushInterval: flushInterval,
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
// Start begins a background writer. Should be called once at startup.
|
||||
func (w *LogWriter) Start(ctx context.Context) {
|
||||
go func() {
|
||||
ticker := time.NewTicker(w.flushInterval)
|
||||
defer ticker.Stop()
|
||||
|
||||
buf := make([]model.LogRecord, 0, w.batchSize)
|
||||
flush := func() {
|
||||
if len(buf) == 0 {
|
||||
return
|
||||
}
|
||||
if err := w.db.Create(&buf).Error; err != nil {
|
||||
log.Printf("log batch insert failed: %v", err)
|
||||
}
|
||||
buf = buf[:0]
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
flush()
|
||||
return
|
||||
case rec := <-w.ch:
|
||||
buf = append(buf, rec)
|
||||
if len(buf) >= w.batchSize {
|
||||
flush()
|
||||
}
|
||||
case <-ticker.C:
|
||||
flush()
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Write queues a log record; drops silently if buffer is full to protect performance.
|
||||
func (w *LogWriter) Write(rec model.LogRecord) {
|
||||
select {
|
||||
case w.ch <- rec:
|
||||
default:
|
||||
// drop to avoid blocking hot path
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user