mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-17 16:12:42 +08:00
cleanup guard
This commit is contained in:
24
cleanup_guard/cleanup_guard.go
Normal file
24
cleanup_guard/cleanup_guard.go
Normal 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()
|
||||||
|
}
|
Reference in New Issue
Block a user