forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly shut down EEBUS mdns entry (evcc-io#2493)
- Loading branch information
1 parent
518f937
commit 10ac20f
Showing
9 changed files
with
119 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package shutdown | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
var ( | ||
mu sync.Mutex | ||
handlers = make([]func(), 0) | ||
exitC = make(chan struct{}) | ||
) | ||
|
||
func Register(cb func()) { | ||
mu.Lock() | ||
handlers = append(handlers, cb) | ||
mu.Unlock() | ||
} | ||
|
||
func Run(stopC <-chan struct{}) { | ||
<-stopC | ||
wg := new(sync.WaitGroup) | ||
|
||
mu.Lock() | ||
for _, cb := range handlers { | ||
wg.Add(1) | ||
|
||
go func(cb func()) { | ||
cb() | ||
wg.Done() | ||
}(cb) | ||
} | ||
mu.Unlock() | ||
|
||
wg.Wait() | ||
close(exitC) | ||
} | ||
|
||
func Done(timeout ...time.Duration) <-chan struct{} { | ||
to := time.Second | ||
if len(timeout) == 1 { | ||
to = timeout[0] | ||
} | ||
|
||
select { | ||
case <-exitC: | ||
return exitC | ||
case <-time.After(to): | ||
exitC := make(chan struct{}) | ||
close(exitC) | ||
return exitC | ||
} | ||
} |
This file contains 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