mirror of
https://github.com/nxtrace/NTrace-core.git
synced 2025-08-12 06:26:39 +00:00
Add Traceroute Module
This commit is contained in:
52
parallel_limiter/parallel_limiter.go
Normal file
52
parallel_limiter/parallel_limiter.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package parallel_limiter
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type ParallelLimiter struct {
|
||||
maxCount int
|
||||
|
||||
mu sync.Mutex
|
||||
currentRunning int
|
||||
|
||||
waiting []chan struct{}
|
||||
}
|
||||
|
||||
func New(count int) *ParallelLimiter {
|
||||
return &ParallelLimiter{
|
||||
maxCount: count,
|
||||
|
||||
currentRunning: 0,
|
||||
waiting: []chan struct{}{},
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ParallelLimiter) Start() chan struct{} {
|
||||
p.mu.Lock()
|
||||
if p.currentRunning+1 > p.maxCount {
|
||||
waitChan := make(chan struct{})
|
||||
p.waiting = append(p.waiting, waitChan)
|
||||
p.mu.Unlock()
|
||||
return waitChan
|
||||
}
|
||||
p.currentRunning++
|
||||
p.mu.Unlock()
|
||||
instantResolveChan := make(chan struct{})
|
||||
go func() {
|
||||
instantResolveChan <- struct{}{}
|
||||
}()
|
||||
return instantResolveChan
|
||||
}
|
||||
|
||||
func (p *ParallelLimiter) Finished() {
|
||||
p.mu.Lock()
|
||||
if len(p.waiting) > 0 {
|
||||
first := p.waiting[0]
|
||||
p.waiting = p.waiting[1:]
|
||||
first <- struct{}{}
|
||||
p.currentRunning++
|
||||
}
|
||||
p.currentRunning--
|
||||
p.mu.Unlock()
|
||||
}
|
||||
Reference in New Issue
Block a user