All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.smallrye.stork.loadbalancer.poweroftwochoices.PowerOfTwoChoicesLoadBalancerProviderLoader Maven / Gradle / Ivy

Go to download

SmallRye Stork Load Balancer provider based selecting two random destinations and then selecting the one with the least assigned requests.

There is a newer version: 2.7.1
Show newest version
package io.smallrye.stork.loadbalancer.poweroftwochoices;

import io.smallrye.stork.loadbalancer.poweroftwochoices.PowerOfTwoChoicesConfiguration;
import io.smallrye.stork.loadbalancer.poweroftwochoices.PowerOfTwoChoicesLoadBalancerProvider;
import io.smallrye.stork.api.LoadBalancer;
import io.smallrye.stork.api.config.ConfigWithType;
import io.smallrye.stork.api.ServiceDiscovery;
import jakarta.enterprise.inject.spi.CDI;
import jakarta.enterprise.context.ApplicationScoped;

/**
 * LoadBalancerLoader for io.smallrye.stork.loadbalancer.poweroftwochoices.PowerOfTwoChoicesLoadBalancerProvider
 */
 @ApplicationScoped
 public class PowerOfTwoChoicesLoadBalancerProviderLoader implements io.smallrye.stork.spi.internal.LoadBalancerLoader {
   private final io.smallrye.stork.loadbalancer.poweroftwochoices.PowerOfTwoChoicesLoadBalancerProvider provider;
   public PowerOfTwoChoicesLoadBalancerProviderLoader() {
       io.smallrye.stork.loadbalancer.poweroftwochoices.PowerOfTwoChoicesLoadBalancerProvider actual = null;
       try {
          actual = CDI.current().select(io.smallrye.stork.loadbalancer.poweroftwochoices.PowerOfTwoChoicesLoadBalancerProvider.class).get();
       } catch(Exception e) { 
          // Use direct instantiation
          actual = new io.smallrye.stork.loadbalancer.poweroftwochoices.PowerOfTwoChoicesLoadBalancerProvider();
       } 
       this.provider = actual;
   }

   @Override
   public LoadBalancer createLoadBalancer(ConfigWithType config, ServiceDiscovery serviceDiscovery) {
      io.smallrye.stork.loadbalancer.poweroftwochoices.PowerOfTwoChoicesConfiguration typedConfig = new io.smallrye.stork.loadbalancer.poweroftwochoices.PowerOfTwoChoicesConfiguration(config.parameters());
      return provider.createLoadBalancer(typedConfig, serviceDiscovery);
   }

  /**
   * @return the type
   */
   @Override
   public String type() {
      return "power-of-two-choices";
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy