From 5f332115ff5c71af93df3ec94023d44fcd14b01c Mon Sep 17 00:00:00 2001 From: Tillie Kottmann Date: Fri, 30 Oct 2020 19:26:22 +0100 Subject: [PATCH] Fix concurrency limiting? --- internal/stopandgo/stopandgo.go | 26 +++++++------------------- pkg/goop/clone.go | 4 ++-- 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/internal/stopandgo/stopandgo.go b/internal/stopandgo/stopandgo.go index cfa855e..8e66f1d 100644 --- a/internal/stopandgo/stopandgo.go +++ b/internal/stopandgo/stopandgo.go @@ -1,43 +1,31 @@ -// a thing that at least in theory limits concurrency, but i honestly have no clue if it works package stopandgo import ( "sync" - "sync/atomic" ) type StopAndGo struct { - wg sync.WaitGroup + wg sync.WaitGroup maxConcurrency int - n int32 + guard chan struct{} } func NewStopAndGo(max int) *StopAndGo { return &StopAndGo{ maxConcurrency: max, + guard: make(chan struct{}, max), } } -func (s *StopAndGo) check() { - if int(atomic.LoadInt32(&s.n)) >= s.maxConcurrency { - s.Wait() - } -} - -func (s *StopAndGo) AddN(n int32) { - atomic.AddInt32(&s.n, n) - s.wg.Add(int(n)) - // check on a new, separate goroutine so we don't lock everything up - go s.check() -} func (s *StopAndGo) Add() { - s.AddN(1) + s.wg.Add(1) + s.guard <- struct{}{} } -func (s *StopAndGo) Done () { - atomic.AddInt32(&s.n, -1) +func (s *StopAndGo) Done() { s.wg.Done() + <-s.guard } func (s *StopAndGo) Wait() { diff --git a/pkg/goop/clone.go b/pkg/goop/clone.go index 57c8a16..d9e3990 100644 --- a/pkg/goop/clone.go +++ b/pkg/goop/clone.go @@ -24,10 +24,10 @@ import ( "time" ) -const maxConcurrency = 40 +const maxConcurrency = 250 var c = &fasthttp.Client{ - MaxConnsPerHost: 5000,//utils.MaxInt(maxConcurrency + 250, fasthttp.DefaultMaxConnsPerHost), + MaxConnsPerHost: utils.MaxInt(maxConcurrency + 250, fasthttp.DefaultMaxConnsPerHost), TLSConfig: &tls.Config{ InsecureSkipVerify: true, },