mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-16 05:39:54 +05:00
ConcReqs using alocate instead of dealocate to control maximum resources
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user