Class SelectableService<T>

  • Type Parameters:
    T - the type of service to be selected

    public class SelectableService<T>
    extends java.lang.Object
    Provides support for sharding a service based on SelectableService.Parameter. Example of a service definition:
     bakery:
      baseUrl: http://bakery.com
      altBakeryApiEnabled: false
      timeoutMs: 3000
      baseUrls:
      - baseUrl: http://alt-bakery.us-west-x.com
        priority: 1
        config:
          altBakeryApiEnabled: true
          maxItemsCached: 10
        parameters:
        - name: region
          values:
          - us-west-1
          - us-west-2
      - baseUrl: http://alt-bakery.other.com
        priority: 2
        config:
          altBakeryApiEnabled: true
        parameters:
        - name: artifactType
          values:
          - RPM
        - name: baseOS
          values:
          - centOS
        - name: user
          values:
          - regx:^[a-f].+@netflix.com
     
    Usage:
    
     val config = mapOf("timeoutMs", 3000)
     val selectableBakery: SelectableService<BakeryService> = SelectableService(
       baseUrls: properties.baseUrls,
       defaultService: bakery,
       defaultConfig: config,
       getServiceByUrlFx: { url -> getService(url)}
     )
    
     // select by artifact type
     val authenticatedUser = "alice@netflix.com"
     val params = listOf(Parameter("artifactType", listOf("RPM")), Parameter("user", listOf(authenticatedUser)))
     val bakery = selectableBakery.byParameters(params)
     val result = bakery.getService().createBake(request)
    
     // configuration {@link BaseUrl#config} attributes for each service definition are accessible like this
     if (bakery.config["altBakeryApiEnabled"]) {
         // Do interesting things
     }
    
     assert bakery.baseUrl == "http://alt-bakery.other.com"
     assert bakery.config == mapOf("timeoutMs" to 3000, "altBakeryApiEnabled" to true)