package io.grpc; import com.google.common.base.Preconditions; import io.grpc.ServiceProviders; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; /* loaded from: classes6.dex */ public final class LoadBalancerRegistry { private static LoadBalancerRegistry instance; private final LinkedHashSet allProviders = new LinkedHashSet<>(); private final LinkedHashMap effectiveProviders = new LinkedHashMap<>(); private static final Logger logger = Logger.getLogger(LoadBalancerRegistry.class.getName()); private static final Iterable> HARDCODED_CLASSES = getHardCodedClasses(); public final void register(LoadBalancerProvider loadBalancerProvider) { synchronized (this) { addProvider(loadBalancerProvider); refreshProviderMap(); } } private void addProvider(LoadBalancerProvider loadBalancerProvider) { synchronized (this) { Preconditions.checkArgument(loadBalancerProvider.isAvailable(), "isAvailable() returned false"); this.allProviders.add(loadBalancerProvider); } } public final void deregister(LoadBalancerProvider loadBalancerProvider) { synchronized (this) { this.allProviders.remove(loadBalancerProvider); refreshProviderMap(); } } private void refreshProviderMap() { synchronized (this) { this.effectiveProviders.clear(); Iterator it = this.allProviders.iterator(); while (it.hasNext()) { LoadBalancerProvider next = it.next(); String policyName = next.getPolicyName(); LoadBalancerProvider loadBalancerProvider = this.effectiveProviders.get(policyName); if (loadBalancerProvider == null || loadBalancerProvider.getPriority() < next.getPriority()) { this.effectiveProviders.put(policyName, next); } } } } public static LoadBalancerRegistry getDefaultRegistry() { LoadBalancerRegistry loadBalancerRegistry; synchronized (LoadBalancerRegistry.class) { if (instance == null) { List loadAll = ServiceProviders.loadAll(LoadBalancerProvider.class, HARDCODED_CLASSES, LoadBalancerProvider.class.getClassLoader(), new LoadBalancerPriorityAccessor()); instance = new LoadBalancerRegistry(); for (LoadBalancerProvider loadBalancerProvider : loadAll) { Logger logger2 = logger; StringBuilder sb = new StringBuilder(); sb.append("Service loader found "); sb.append(loadBalancerProvider); logger2.fine(sb.toString()); if (loadBalancerProvider.isAvailable()) { instance.addProvider(loadBalancerProvider); } } instance.refreshProviderMap(); } loadBalancerRegistry = instance; } return loadBalancerRegistry; } public final LoadBalancerProvider getProvider(String str) { LoadBalancerProvider loadBalancerProvider; synchronized (this) { loadBalancerProvider = this.effectiveProviders.get(Preconditions.checkNotNull(str, "policy")); } return loadBalancerProvider; } final Map providers() { LinkedHashMap linkedHashMap; synchronized (this) { linkedHashMap = new LinkedHashMap(this.effectiveProviders); } return linkedHashMap; } static List> getHardCodedClasses() { ArrayList arrayList = new ArrayList(); try { arrayList.add(Class.forName("io.grpc.internal.PickFirstLoadBalancerProvider")); } catch (ClassNotFoundException e) { logger.log(Level.WARNING, "Unable to find pick-first LoadBalancer", (Throwable) e); } try { arrayList.add(Class.forName("io.grpc.util.SecretRoundRobinLoadBalancerProvider$Provider")); } catch (ClassNotFoundException e2) { logger.log(Level.FINE, "Unable to find round-robin LoadBalancer", (Throwable) e2); } return Collections.unmodifiableList(arrayList); } /* loaded from: classes6.dex */ static final class LoadBalancerPriorityAccessor implements ServiceProviders.PriorityAccessor { LoadBalancerPriorityAccessor() { } @Override // io.grpc.ServiceProviders.PriorityAccessor public final boolean isAvailable(LoadBalancerProvider loadBalancerProvider) { return loadBalancerProvider.isAvailable(); } @Override // io.grpc.ServiceProviders.PriorityAccessor public final int getPriority(LoadBalancerProvider loadBalancerProvider) { return loadBalancerProvider.getPriority(); } } }