处理返回的 cancel 函数, 避免 context 泄露

不处理 cancel 相关返回函数,会造成 context 一直停留在 background.
the cancel function returned by context.WithTimeout should be called, not discarded, to avoid a context leak
This commit is contained in:
huiyifyj 2024-12-16 22:25:16 +08:00
parent fe6830b77a
commit 14014e9af4
No known key found for this signature in database
GPG Key ID: 67D4F264AF9D2C4C
1 changed files with 3 additions and 2 deletions

View File

@ -67,7 +67,6 @@ func (m *Minio) UploadFile(file *multipart.FileHeader) (filePathres, key string,
}
f.Close() // 创建文件 defer 关闭
// 对文件名进行加密存储
ext := filepath.Ext(file.Filename)
filename := utils.MD5V([]byte(strings.TrimSuffix(file.Filename, ext))) + ext
@ -91,8 +90,10 @@ func (m *Minio) UploadFile(file *multipart.FileHeader) (filePathres, key string,
}
func (m *Minio) DeleteFile(key string) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
// Delete the object from MinIO
ctx, _ := context.WithTimeout(context.Background(), time.Second*5)
err := m.Client.RemoveObject(ctx, m.bucket, key, minio.RemoveObjectOptions{})
return err
}