Reputation: 29
I have a memory leak in my application which use gopacket/pcap : memory consumption increases continuously when the application receives network traffic.
My code :
src := gopacket.NewPacketSource(p.parent.pcapHandle, layers.LayerTypeEthernet)
in := src.Packets()
for {
select {
case err = <-tg.IsClosingEvent():
return err
case packet := <-in:
fmt.Print(packet)
}
}
Prototype of IsClosingEvent : func (h *ThreadGroup) IsClosingEvent() <-chan error {
During my application works, tg.IsClosingEvent() is never used : it is to manage a application close event. If I comment case of tg.IsClosingEvent(), the problem doesn't appear.
When I use this code, I don't have problem :
src := gopacket.NewPacketSource(p.parent.pcapHandle, layers.LayerTypeEthernet)
in := src.Packets()
stopCh := make(chan error)
for {
select {
case err = <-stopCh:
return err
case packet := <-in:
fmt.Print(packet)
if packet.String() == "a" {
stopCh <- errors.New("a")
}
}
}
I don't understand, it seems to be the same thing.
Where is the problem ?
For information pcapHandle is :
handle, err := pcap.OpenLive(interfaceName, 65536, true, pcap.BlockForever)
IsClosingEvent function :
func (h *ThreadGroup) IsClosingEvent() <-chan error {
ch := make(chan error, 1)
go func() {
<-h.ctx.Done()
ch <- h.msgClosed()
}()
return ch
}
func (h *ThreadGroup) msgClosed() error {
return errors.New("Thread Group \"" + h.name + "\" is Closed")
}
Upvotes: 0
Views: 76