Skip to content

Commit

Permalink
fix: memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
BaiMeow committed Aug 3, 2024
1 parent 2a6b755 commit 384725b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
19 changes: 15 additions & 4 deletions pool/pool.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package pool

import "log"
import (
"context"
"log"
)

type Worker struct {
Func chan func()
Stop chan struct{}
Func chan func()
Cancel context.CancelFunc
}

type Pool struct {
Expand Down Expand Up @@ -41,12 +44,20 @@ func (w *Worker) Run() {
log.Println("Recovered ", r)
}
}()
ctx, cancel := context.WithCancel(context.Background())
w.Cancel = cancel
for {
select {
case f := <-w.Func:
f()
case <-w.Stop:
case <-ctx.Done():
return
}
}
}

func (p *Pool) Close() {
for _, w := range p.workers {
w.Cancel()
}
}
2 changes: 2 additions & 0 deletions scan/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (s *Scanner) ScanAll(prefixs []netip.Prefix, port []int) []netip.AddrPort {
} else {
p := pool.Pool{Size: s.PortScanRate, Buffer: s.PortScanRate}
p.Init()
defer p.Close()
var wg sync.WaitGroup
wg.Add(addrCount * len(port))
ipGenerator(prefixs)(func(addr netip.Addr) {
Expand All @@ -111,6 +112,7 @@ func (s *Scanner) ScanAll(prefixs []netip.Prefix, port []int) []netip.AddrPort {
log.Println("start socks5 scan with 128 threads.")
p := pool.Pool{Size: 128, Buffer: 128}
p.Init()
defer p.Close()
c = utils.NewCollector[netip.AddrPort]()
var wg sync.WaitGroup
wg.Add(len(aliveTCPAddrs))
Expand Down

0 comments on commit 384725b

Please sign in to comment.