github.com/grafana/pyroscope@v1.18.0/pkg/util/validation/limits.go (about)

     1  package validation
     2  
     3  import "time"
     4  
     5  // SmallestPositiveNonZeroIntPerTenant is returning the minimal positive and
     6  // non-zero value of the supplied limit function for all given tenants. In many
     7  // limits a value of 0 means unlimited so the method will return 0 only if all
     8  // inputs have a limit of 0 or an empty tenant list is given.
     9  func SmallestPositiveNonZeroIntPerTenant(tenantIDs []string, f func(string) int) int {
    10  	var result *int
    11  	for _, tenantID := range tenantIDs {
    12  		v := f(tenantID)
    13  		if v > 0 && (result == nil || v < *result) {
    14  			result = &v
    15  		}
    16  	}
    17  	if result == nil {
    18  		return 0
    19  	}
    20  	return *result
    21  }
    22  
    23  // MaxDurationOrZeroPerTenant is returning the maximum duration per tenant or zero if one tenant has time.Duration(0).
    24  func MaxDurationOrZeroPerTenant(tenantIDs []string, f func(string) time.Duration) time.Duration {
    25  	var result *time.Duration
    26  	for _, tenantID := range tenantIDs {
    27  		v := f(tenantID)
    28  		if v == 0 {
    29  			return v
    30  		}
    31  
    32  		if v > 0 && (result == nil || v > *result) {
    33  			result = &v
    34  		}
    35  	}
    36  	if result == nil {
    37  		return 0
    38  	}
    39  	return *result
    40  }
    41  
    42  // SmallestPositiveNonZeroDurationPerTenant is returning the minimal positive
    43  // and non-zero value of the supplied limit function for all given tenants. In
    44  // many limits a value of 0 means unlimited so the method will return 0 only if
    45  // all inputs have a limit of 0 or an empty tenant list is given.
    46  func SmallestPositiveNonZeroDurationPerTenant(tenantIDs []string, f func(string) time.Duration) time.Duration {
    47  	var result *time.Duration
    48  	for _, tenantID := range tenantIDs {
    49  		v := f(tenantID)
    50  		if v > 0 && (result == nil || v < *result) {
    51  			result = &v
    52  		}
    53  	}
    54  	if result == nil {
    55  		return 0
    56  	}
    57  	return *result
    58  }