func (rls *ResourceLimiterService) matchingResourceLimitsForEvent(ev map[string]interface{}) (map[string]*ResourceLimit, error) { matchingResources := make(map[string]*ResourceLimit) for fldName, fieldValIf := range ev { fldVal, canCast := utils.CastFieldIfToString(fieldValIf) if !canCast { return nil, fmt.Errorf("Cannot cast field: %s into string", fldName) } rlIDs, err := rls.dataDB.MatchReqFilterIndex(utils.ResourceLimitsIndex, utils.ConcatenatedKey(fldName, fldVal)) if err != nil { if err == utils.ErrNotFound { continue } return nil, err } for resName := range rlIDs { if _, hasIt := matchingResources[resName]; hasIt { // Already checked this RL continue } rl, err := rls.dataDB.GetResourceLimit(resName, false, utils.NonTransactional) if err != nil { if err == utils.ErrNotFound { continue } return nil, err } now := time.Now() if rl.ActivationTime.After(now) || (!rl.ExpiryTime.IsZero() && rl.ExpiryTime.Before(now)) { // not active continue } passAllFilters := true for _, fltr := range rl.Filters { if pass, err := fltr.Pass(ev, "", rls.cdrStatS); err != nil { return nil, utils.NewErrServerError(err) } else if !pass { passAllFilters = false continue } } if passAllFilters { matchingResources[rl.ID] = rl // Cannot save it here since we could have errors after and resource will remain unused } } } // Check un-indexed resources uIdxRLIDs, err := rls.dataDB.MatchReqFilterIndex(utils.ResourceLimitsIndex, utils.ConcatenatedKey(utils.NOT_AVAILABLE, utils.NOT_AVAILABLE)) if err != nil && err != utils.ErrNotFound { return nil, err } for resName := range uIdxRLIDs { if _, hasIt := matchingResources[resName]; hasIt { // Already checked this RL continue } rl, err := rls.dataDB.GetResourceLimit(resName, false, utils.NonTransactional) if err != nil { if err == utils.ErrNotFound { continue } return nil, err } now := time.Now() if rl.ActivationTime.After(now) || (!rl.ExpiryTime.IsZero() && rl.ExpiryTime.Before(now)) { // not active continue } for _, fltr := range rl.Filters { if pass, err := fltr.Pass(ev, "", rls.cdrStatS); err != nil { return nil, utils.NewErrServerError(err) } else if !pass { continue } matchingResources[rl.ID] = rl // Cannot save it here since we could have errors after and resource will remain unused } } return matchingResources, nil }
func (rls *ResourceLimiterService) matchingResourceLimitsForEvent(ev map[string]interface{}) (map[string]*ResourceLimit, error) { matchingResources := make(map[string]*ResourceLimit) for fldName, fieldValIf := range ev { if _, hasIt := rls.stringIndexes[fldName]; !hasIt { continue } strVal, canCast := utils.CastFieldIfToString(fieldValIf) if !canCast { return nil, fmt.Errorf("Cannot cast field: %s into string", fldName) } if _, hasIt := rls.stringIndexes[fldName][strVal]; !hasIt { continue } for resName := range rls.stringIndexes[fldName][strVal] { if _, hasIt := matchingResources[resName]; hasIt { // Already checked this RL continue } x, ok := cache2go.Get(utils.ResourceLimitsPrefix + resName) if !ok { return nil, utils.ErrNotFound } rl := x.(*ResourceLimit) now := time.Now() if rl.ActivationTime.After(now) || (!rl.ExpiryTime.IsZero() && rl.ExpiryTime.Before(now)) { // not active continue } passAllFilters := true for _, fltr := range rl.Filters { if pass, err := fltr.Pass(ev, "", rls.cdrStatS); err != nil { return nil, utils.NewErrServerError(err) } else if !pass { passAllFilters = false continue } } if passAllFilters { matchingResources[rl.ID] = rl // Cannot save it here since we could have errors after and resource will remain unused } } } // Check un-indexed resources for resName := range rls.stringIndexes[utils.NOT_AVAILABLE][utils.NOT_AVAILABLE] { if _, hasIt := matchingResources[resName]; hasIt { // Already checked this RL continue } x, ok := cache2go.Get(utils.ResourceLimitsPrefix + resName) if !ok { return nil, utils.ErrNotFound } rl := x.(*ResourceLimit) now := time.Now() if rl.ActivationTime.After(now) || (!rl.ExpiryTime.IsZero() && rl.ExpiryTime.Before(now)) { // not active continue } for _, fltr := range rl.Filters { if pass, err := fltr.Pass(ev, "", rls.cdrStatS); err != nil { return nil, utils.NewErrServerError(err) } else if !pass { continue } matchingResources[rl.ID] = rl // Cannot save it here since we could have errors after and resource will remain unused } } return matchingResources, nil }