github.com/chenbh/concourse/v6@v6.4.2/atc/wrappa/concurrent_request_policy.go (about)

     1  package wrappa
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/chenbh/concourse/v6/atc"
     7  )
     8  
     9  type LimitedRoute string
    10  
    11  var supportedActions = []LimitedRoute{LimitedRoute(atc.ListAllJobs)}
    12  
    13  func (lr *LimitedRoute) UnmarshalFlag(value string) error {
    14  	if !isValidAction(value) {
    15  		return fmt.Errorf("'%s' is not a valid action", value)
    16  	}
    17  	for _, supportedAction := range supportedActions {
    18  		if value == string(supportedAction) {
    19  			*lr = supportedAction
    20  			return nil
    21  		}
    22  	}
    23  	return fmt.Errorf(
    24  		"action '%s' is not supported. Supported actions are: %v",
    25  		value,
    26  		supportedActions,
    27  	)
    28  }
    29  
    30  func isValidAction(action string) bool {
    31  	for _, route := range atc.Routes {
    32  		if route.Name == action {
    33  			return true
    34  		}
    35  	}
    36  	return false
    37  }
    38  
    39  //go:generate counterfeiter . ConcurrentRequestPolicy
    40  
    41  type ConcurrentRequestPolicy interface {
    42  	HandlerPool(action string) (Pool, bool)
    43  }
    44  
    45  type concurrentRequestPolicy struct {
    46  	handlerPools map[LimitedRoute]Pool
    47  }
    48  
    49  func NewConcurrentRequestPolicy(
    50  	limits map[LimitedRoute]int,
    51  ) ConcurrentRequestPolicy {
    52  	pools := map[LimitedRoute]Pool{}
    53  	for action, limit := range limits {
    54  		pools[action] = NewPool(limit)
    55  	}
    56  	return &concurrentRequestPolicy{
    57  		handlerPools: pools,
    58  	}
    59  }
    60  
    61  func (crp *concurrentRequestPolicy) HandlerPool(action string) (Pool, bool) {
    62  	pool, found := crp.handlerPools[LimitedRoute(action)]
    63  	return pool, found
    64  }