Skip to content

Commit

Permalink
Merge pull request #102 from mysteriumnetwork/configurable-limits
Browse files Browse the repository at this point in the history
Add hard and soft limits configuration per country
  • Loading branch information
soffokl authored Dec 1, 2023
2 parents 7bf7ddf + 3fd4f5f commit e044470
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 33 deletions.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func main() {
}

tagEnhancer := tags.NewEnhancer(tags.NewApi(cfg.BadgerAddress.String()))
proposalRepo := proposal.NewRepository([]proposal.Enhancer{tagEnhancer})
proposalRepo := proposal.NewRepository([]proposal.Enhancer{tagEnhancer}, cfg.ProposalsHardLimitPerCountry, cfg.ProposalsSoftLimitPerCountry)
qualityOracleAPI := oracleapi.New(cfg.QualityOracleURL.String())
qualityService := quality.NewService(qualityOracleAPI, cfg.QualityCacheTTL)
proposalService := proposal.NewService(proposalRepo, qualityService)
Expand Down
42 changes: 28 additions & 14 deletions config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ type Options struct {
DevPass string
InternalPass string

ProposalsHardLimitPerCountry int
ProposalsSoftLimitPerCountry int

MaxRequestsLimit int

ProposalsCacheTTL time.Duration
Expand Down Expand Up @@ -85,27 +88,38 @@ func ReadDiscovery() (*Options, error) {
internalPass := OptionalEnv("INTERNAL_PASS", "")
logLevel := OptionalEnv("LOG_LEVEL", "debug")

proposalsHardLimitPerCountry, err := OptionalEnvInt("PROPOSALS_HARD_LIMIT_PER_COUNTRY", "1000")
if err != nil {
return nil, err
}
proposalsSoftLimitPerCountry, err := OptionalEnvInt("PROPOSALS_SOFT_LIMIT_PER_COUNTRY", "1000")
if err != nil {
return nil, err
}

maxRequestsLimit := OptionalEnv("MAX_REQUESTS_LIMIT", "1000")
limit, err := strconv.Atoi(maxRequestsLimit)
if err != nil {
return nil, fmt.Errorf("failed to parse max requests limit: %w", err)
}

return &Options{
QualityOracleURL: *qualityOracleURL,
QualityCacheTTL: *qualityCacheTTL,
BrokerURL: *brokerURL,
BadgerAddress: *badgerAddress,
LocationAddress: *locationAddress,
LocationUser: locationUser,
LocationPass: locationPass,
MaxRequestsLimit: limit,
DevPass: devPass,
InternalPass: internalPass,
ProposalsCacheTTL: *proposalsCacheTTL,
ProposalsCacheLimit: proposalsCacheLimit,
CountriesCacheLimit: countriesCacheLimit,
LogLevel: logLevel,
QualityOracleURL: *qualityOracleURL,
QualityCacheTTL: *qualityCacheTTL,
BrokerURL: *brokerURL,
BadgerAddress: *badgerAddress,
LocationAddress: *locationAddress,
LocationUser: locationUser,
LocationPass: locationPass,
MaxRequestsLimit: limit,
DevPass: devPass,
InternalPass: internalPass,
ProposalsCacheTTL: *proposalsCacheTTL,
ProposalsCacheLimit: proposalsCacheLimit,
CountriesCacheLimit: countriesCacheLimit,
LogLevel: logLevel,
ProposalsHardLimitPerCountry: proposalsHardLimitPerCountry,
ProposalsSoftLimitPerCountry: proposalsSoftLimitPerCountry,
}, nil
}

Expand Down
34 changes: 16 additions & 18 deletions proposal/inmemory.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,18 @@ import (
"github.com/mysteriumnetwork/discovery/quality/oracleapi"
)

const (
// TODO: lower this values once dvpn apps start using proposal numbers endpoint.
countryHardLimit = 100000
countrySoftLimit = 100000
)

type Enhancer interface {
Enhance(proposal *v3.Proposal)
}

type Repository struct {
expirationJobDelay time.Duration
expirationDuration time.Duration
mu sync.RWMutex
proposals map[string]record
enhancers []Enhancer
expirationJobDelay time.Duration
expirationDuration time.Duration
mu sync.RWMutex
proposals map[string]record
enhancers []Enhancer
proposalsHardLimitPerCountry int
proposalsSoftLimitPerCountry int
}

type repoListOpts struct {
Expand All @@ -52,12 +48,14 @@ type record struct {
expiresAt time.Time
}

func NewRepository(enhancers []Enhancer) *Repository {
func NewRepository(enhancers []Enhancer, proposalsHardLimitPerCountry, proposalsSoftLimitPerCountry int) *Repository {
return &Repository{
expirationDuration: 3*time.Minute + 10*time.Second,
expirationJobDelay: 20 * time.Second,
proposals: make(map[string]record),
enhancers: enhancers,
expirationDuration: 3*time.Minute + 10*time.Second,
expirationJobDelay: 20 * time.Second,
proposals: make(map[string]record),
enhancers: enhancers,
proposalsHardLimitPerCountry: proposalsHardLimitPerCountry,
proposalsSoftLimitPerCountry: proposalsSoftLimitPerCountry,
}
}

Expand Down Expand Up @@ -91,8 +89,8 @@ func (r *Repository) List(opts repoListOpts, limited bool) (res []v3.Proposal) {

countryLimit[p.proposal.Location.Country]++

if !limited || countryLimit[p.proposal.Location.Country] <= countryHardLimit {
if !limited || countryLimit[p.proposal.Location.Country] <= countrySoftLimit || countryLimit[p.proposal.Location.Country]%10 == 0 {
if !limited || countryLimit[p.proposal.Location.Country] <= r.proposalsHardLimitPerCountry {
if !limited || countryLimit[p.proposal.Location.Country] <= r.proposalsSoftLimitPerCountry || countryLimit[p.proposal.Location.Country]%10 == 0 {
res = append(res, p.proposal)
}
}
Expand Down

0 comments on commit e044470

Please sign in to comment.