Close listeners which are no longer used

This commit is contained in:
Matthew Holt
2019-04-02 15:31:02 -06:00
parent f976aa7443
commit 59a5d0db28
2 changed files with 40 additions and 12 deletions

View File

@ -6,6 +6,7 @@ import (
"log"
"strings"
"sync"
"sync/atomic"
"time"
)
@ -24,6 +25,7 @@ func Start(cfg Config) error {
cfg.runners[modName] = val.(Runner)
}
// start the new runners
for name, r := range cfg.runners {
err := r.Run()
if err != nil {
@ -32,6 +34,7 @@ func Start(cfg Config) error {
}
}
// shut down down the old ones
currentCfgMu.Lock()
if currentCfg != nil {
for _, r := range currentCfg.runners {
@ -44,6 +47,20 @@ func Start(cfg Config) error {
currentCfg = &cfg
currentCfgMu.Unlock()
// shut down listeners that are no longer being used
listenersMu.Lock()
for key, info := range listeners {
if atomic.LoadInt32(&info.usage) == 0 {
err := info.ln.Close()
if err != nil {
log.Printf("[ERROR] closing listener %s: %v", info.ln.Addr(), err)
continue
}
delete(listeners, key)
}
}
listenersMu.Unlock()
return nil
}