Created
July 20, 2016 01:59
-
-
Save chowey/bce83aeec640cfc013f3a69086a87c2b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "sync" | |
type ExitGroup struct { | |
wg sync.WaitGroup | |
cl []func() | |
lk sync.Mutex | |
} | |
func (e *ExitGroup) OnClose(fn func()) { | |
e.lk.Lock() | |
e.wg.Add(1) | |
e.cl = append(e.cl, fn) | |
e.lk.Unlock() | |
} | |
func (e *ExitGroup) OnCloseErr(fn func() error) { | |
e.OnClose(func() { | |
if err := fn(); err != nil { | |
log.Println(err) | |
} | |
}) | |
} | |
func (e *ExitGroup) Exit() { | |
e.lk.Lock() | |
for _, fn := range e.cl { | |
fn() | |
e.wg.Done() | |
} | |
e.lk.Unlock() | |
} | |
func (e *ExitGroup) Wait() { | |
e.wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment