1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-17 08:02:40 +08:00

cleanup guard

This commit is contained in:
2022-07-26 14:15:13 +08:00
parent 9c86470fa1
commit 93352f03c1

View File

@ -0,0 +1,24 @@
package cleanupguard
import "sync"
type CleanupGuard struct {
enable bool
fn func()
mutex sync.Mutex
}
// 新建清理
func NewCleanupGuard(fn func()) CleanupGuard {
return CleanupGuard{fn: fn, enable: true}
}
func (g *CleanupGuard) Enable() {
g.mutex.Lock()
defer g.mutex.Unlock()
g.enable = true
}
func (g *CleanupGuard) Run() {
g.fn()
}