ConcReqs using alocate instead of dealocate to control maximum resources

This commit is contained in:
DanB
2020-11-01 19:55:56 +01:00
parent de7c0ffbb6
commit 481a6e3adc
2 changed files with 11 additions and 11 deletions

View File

@@ -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
}

View File

@@ -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