From 481a6e3adc0ac14e5168cf64f4877342a832ce4e Mon Sep 17 00:00:00 2001 From: DanB Date: Sun, 1 Nov 2020 19:55:56 +0100 Subject: [PATCH] ConcReqs using alocate instead of dealocate to control maximum resources --- utils/concureqs.go | 18 +++++++++--------- utils/consts.go | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/utils/concureqs.go b/utils/concureqs.go index 78fa05e5a..ba3379d5d 100644 --- a/utils/concureqs.go +++ b/utils/concureqs.go @@ -27,45 +27,45 @@ import ( var ConReqs *ConcReqs type ConcReqs struct { - limit int strategy string aReqs chan struct{} } func NewConReqs(reqs int, strategy string) *ConcReqs { cR := &ConcReqs{ - limit: reqs, strategy: strategy, aReqs: make(chan struct{}, reqs), } - for i := 0; i < reqs; i++ { - cR.aReqs <- struct{}{} - } return cR } // IsLimited returns true if the limit is not 0 func (cR *ConcReqs) IsLimited() bool { - return cR.limit != 0 + return len(cR.aReqs) != 0 +} + +// Allocated returns the number of requests actively serviced +func (cR *ConcReqs) Allocated() int { + return len(cR.aReqs) } // Allocate will reserve a channel for the API call func (cR *ConcReqs) Allocate() (err error) { switch cR.strategy { case MetaBusy: - if len(cR.aReqs) == 0 { + if len(cR.aReqs) == cap(cR.aReqs) { return ErrMaxConcurentRPCExceededNoCaps } fallthrough case MetaQueue: - <-cR.aReqs // get from channel + cR.aReqs <- struct{}{} } return } // Deallocate will free a channel for the API call func (cR *ConcReqs) Deallocate() { - cR.aReqs <- struct{}{} + <-cR.aReqs return } diff --git a/utils/consts.go b/utils/consts.go index faa5f295d..95fa50cbd 100755 --- a/utils/consts.go +++ b/utils/consts.go @@ -233,6 +233,8 @@ var ( RateFilterIndexIDs: CacheRateFilterIndexes, FilterIndexIDs: CacheReverseFilterIndexes, } + ConcurrentReqsLimit int + ConcurrentReqsStrategy string ) const ( @@ -871,8 +873,6 @@ const ( ProcessRuns = "ProcessRuns" HashtagSep = "#" MetaRounding = "*rounding" - ConcurrentReqsLimit = 0 - ConcurrentReqsStrategy = "" ) // Migrator Action