probe/internal/pcap/pcap.go
crusader 7535e7ffc1 ing
2018-09-05 03:58:33 +09:00

67 lines
1.1 KiB
Go

package pcap
import (
"log"
"sync"
"time"
omd "git.loafle.net/overflow/model/discovery"
)
var instances sync.Map
// var instances map[string]PCapScanner
func init() {
// instances = make(map[string]PCapScanner, 0)
}
func RetainScanner(zone *omd.Zone) (PCapScanner, error) {
var ps PCapScanner
// if ps, ok = instances[zone.Network]; !ok {
_ps, ok := instances.Load(zone.Network)
if !ok {
ps = NewPCapScanner(zone)
if err := ps.Start(); nil != err {
return nil, err
}
instances.Store(zone.Network, ps)
} else {
ps = _ps.(PCapScanner)
}
ps.retain()
log.Print("Pcap retain")
return ps, nil
}
func ReleaseScanner(zone *omd.Zone) {
go func() {
time.Sleep(1 * time.Second)
if _ps, ok := instances.Load(zone.Network); ok {
ps := _ps.(PCapScanner)
log.Print("Pcap release")
if ps.release() {
ps.Stop()
instances.Delete(zone.Network)
}
}
}()
}
func ReleaseScannerAll() {
instances.Range(func(k, v interface{}) bool {
ps := v.(PCapScanner)
if ps.release() {
ps.Stop()
instances.Delete(k)
return false
}
return true
})
}