container_discovery/internal/pcap/pcap.go

61 lines
929 B
Go
Raw Normal View History

2018-04-19 11:36:56 +00:00
package pcap
import (
"sync"
"time"
2018-04-26 09:00:24 +00:00
ocmd "git.loafle.net/overflow/commons-go/model/discovery"
2018-04-19 11:36:56 +00:00
)
var mtx sync.Mutex
var instances map[string]PCapScanner
func init() {
instances = make(map[string]PCapScanner, 0)
}
2018-04-26 09:00:24 +00:00
func RetainScanner(zone *ocmd.Zone) (PCapScanner, error) {
2018-04-19 11:36:56 +00:00
mtx.Lock()
defer mtx.Unlock()
var ps PCapScanner
var ok bool
if ps, ok = instances[zone.Network]; !ok {
ps = newPCapScanner(zone)
if err := ps.start(); nil != err {
return nil, err
}
instances[zone.Network] = ps
}
ps.retain()
return ps, nil
}
2018-04-26 09:00:24 +00:00
func ReleaseScanner(zone *ocmd.Zone) {
2018-04-19 11:36:56 +00:00
go func() {
time.Sleep(2 * time.Second)
mtx.Lock()
defer mtx.Unlock()
if ps, ok := instances[zone.Network]; ok {
if ps.release() {
ps.stop()
delete(instances, zone.Network)
}
}
}()
}
func ReleaseScannerAll() {
mtx.Lock()
defer mtx.Unlock()
for k, ps := range instances {
ps.stop()
delete(instances, k)
}
}