chore: refresh gramsrv public release
This commit is contained in:
parent
75cebe8dbf
commit
70b6820474
1274 changed files with 378751 additions and 59919 deletions
67
internal/app/files/upload_gc.go
Normal file
67
internal/app/files/upload_gc.go
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
package files
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// UploadPartGCWorker 周期性清理未组装的过期上传分片。
|
||||
type UploadPartGCWorker struct {
|
||||
files *Service
|
||||
logger *zap.Logger
|
||||
ttl time.Duration
|
||||
interval time.Duration
|
||||
batch int
|
||||
}
|
||||
|
||||
func NewUploadPartGCWorker(files *Service, logger *zap.Logger, ttl, interval time.Duration, batch int) *UploadPartGCWorker {
|
||||
if logger == nil {
|
||||
logger = zap.NewNop()
|
||||
}
|
||||
if ttl <= 0 {
|
||||
ttl = DefaultUploadPartTTL
|
||||
}
|
||||
if interval <= 0 {
|
||||
interval = DefaultUploadPartGCInterval
|
||||
}
|
||||
if batch <= 0 {
|
||||
batch = DefaultUploadPartGCBatch
|
||||
}
|
||||
return &UploadPartGCWorker{
|
||||
files: files,
|
||||
logger: logger,
|
||||
ttl: ttl,
|
||||
interval: interval,
|
||||
batch: batch,
|
||||
}
|
||||
}
|
||||
|
||||
func (w *UploadPartGCWorker) Run(ctx context.Context) {
|
||||
w.runOnce(ctx)
|
||||
ticker := time.NewTicker(w.interval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
w.runOnce(ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *UploadPartGCWorker) runOnce(ctx context.Context) {
|
||||
if w.files == nil {
|
||||
return
|
||||
}
|
||||
deleted, err := w.files.DeleteExpiredUploadParts(ctx, time.Now().Add(-w.ttl), w.batch)
|
||||
if err != nil {
|
||||
w.logger.Warn("清理过期 upload_parts 失败", zap.Error(err))
|
||||
return
|
||||
}
|
||||
if deleted > 0 {
|
||||
w.logger.Info("清理过期 upload_parts 完成", zap.Int64("deleted", deleted))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue