下面列出了io.grpc.internal.SharedResourceHolder#get ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private CronetTransportFactory(
StreamBuilderFactory streamFactory,
Executor executor,
@Nullable ScheduledExecutorService timeoutService,
int maxMessageSize,
boolean alwaysUsePut,
TransportTracer transportTracer) {
usingSharedScheduler = timeoutService == null;
this.timeoutService = usingSharedScheduler
? SharedResourceHolder.get(GrpcUtil.TIMER_SERVICE) : timeoutService;
this.maxMessageSize = maxMessageSize;
this.alwaysUsePut = alwaysUsePut;
this.streamFactory = streamFactory;
this.executor = Preconditions.checkNotNull(executor, "executor");
this.transportTracer = Preconditions.checkNotNull(transportTracer, "transportTracer");
}
NettyTransportFactory(ProtocolNegotiator protocolNegotiator,
Class<? extends Channel> channelType, Map<ChannelOption<?>, ?> channelOptions,
EventLoopGroup group, int flowControlWindow, int maxMessageSize, int maxHeaderListSize,
long keepAliveTimeNanos, long keepAliveTimeoutNanos, boolean keepAliveWithoutCalls,
TransportTracer transportTracer, LocalSocketPicker localSocketPicker) {
this.protocolNegotiator = protocolNegotiator;
this.channelType = channelType;
this.channelOptions = new HashMap<ChannelOption<?>, Object>(channelOptions);
this.flowControlWindow = flowControlWindow;
this.maxMessageSize = maxMessageSize;
this.maxHeaderListSize = maxHeaderListSize;
this.keepAliveTimeNanos = new AtomicBackoff("keepalive time nanos", keepAliveTimeNanos);
this.keepAliveTimeoutNanos = keepAliveTimeoutNanos;
this.keepAliveWithoutCalls = keepAliveWithoutCalls;
this.transportTracer = transportTracer;
this.localSocketPicker =
localSocketPicker != null ? localSocketPicker : new LocalSocketPicker();
usingSharedGroup = group == null;
if (usingSharedGroup) {
// The group was unspecified, using the shared group.
this.group = SharedResourceHolder.get(Utils.DEFAULT_WORKER_EVENT_LOOP_GROUP);
} else {
this.group = group;
}
}
/**
* 将当前出错的服务器从备选列表中去除
*
* @author sxp
* @since 2018-6-21
* @since 2019/12/11 modify by wlh 10分钟(时间可配)后,将服务重新放回至服务提供列表
*/
private static void removeCurrentProvider(NameResolver nameResolver, String providerId, String method) {
Map<String, ServiceProvider> providersForLoadBalance = nameResolver.getProvidersForLoadBalance();
if (providersForLoadBalance == null || providersForLoadBalance.size() == 0) {
logger.info("客户端的备选列表为空", providerId);
return;
}
if (providersForLoadBalance.containsKey(providerId)) {
logger.error("FATAL ERROR : 服务器节点{}连续调用出错{}次,从客户端备选服务器列表中删除", providerId, switchoverThreshold);
providersForLoadBalance.remove(providerId);
nameResolver.reCalculateProvidersCountAfterLoadBalance(method);
if (timerService == null) {
timerService = SharedResourceHolder.get(GrpcUtil.TIMER_SERVICE);
}
timerService.schedule(new RecoveryServerRunnable(nameResolver, providerId, method), recoveryMilliseconds, TimeUnit.MILLISECONDS);
}
}
@Override
@GuardedBy("this")
public void refresh() {
if (refreshing) return;
try {
refreshing = true;
Endpoints endpoints = kubernetesClient.endpoints().inNamespace(namespace)
.withName(name)
.get();
if (endpoints == null) {
// Didn't find anything, retrying
ScheduledExecutorService timerService = SharedResourceHolder.get(timerServiceResource);
timerService.schedule(() -> {
refresh();
}, 30, TimeUnit.SECONDS);
return;
}
update(endpoints);
watch();
} finally {
refreshing = false;
}
}
private CronetTransportFactory(
StreamBuilderFactory streamFactory,
Executor executor,
@Nullable ScheduledExecutorService timeoutService,
int maxMessageSize,
boolean alwaysUsePut,
TransportTracer transportTracer,
boolean useGetForSafeMethods,
boolean usePutForIdempotentMethods) {
usingSharedScheduler = timeoutService == null;
this.timeoutService = usingSharedScheduler
? SharedResourceHolder.get(GrpcUtil.TIMER_SERVICE) : timeoutService;
this.maxMessageSize = maxMessageSize;
this.alwaysUsePut = alwaysUsePut;
this.streamFactory = streamFactory;
this.executor = Preconditions.checkNotNull(executor, "executor");
this.transportTracer = Preconditions.checkNotNull(transportTracer, "transportTracer");
this.useGetForSafeMethods = useGetForSafeMethods;
this.usePutForIdempotentMethods = usePutForIdempotentMethods;
}
private OkHttpTransportFactory(Executor executor,
@Nullable ScheduledExecutorService timeoutService,
@Nullable SSLSocketFactory socketFactory,
@Nullable HostnameVerifier hostnameVerifier,
ConnectionSpec connectionSpec,
int maxMessageSize,
boolean enableKeepAlive,
long keepAliveTimeNanos,
long keepAliveTimeoutNanos,
int flowControlWindow,
boolean keepAliveWithoutCalls,
int maxInboundMetadataSize,
TransportTracer.Factory transportTracerFactory) {
usingSharedScheduler = timeoutService == null;
this.timeoutService = usingSharedScheduler
? SharedResourceHolder.get(GrpcUtil.TIMER_SERVICE) : timeoutService;
this.socketFactory = socketFactory;
this.hostnameVerifier = hostnameVerifier;
this.connectionSpec = connectionSpec;
this.maxMessageSize = maxMessageSize;
this.enableKeepAlive = enableKeepAlive;
this.keepAliveTimeNanos = new AtomicBackoff("keepalive time nanos", keepAliveTimeNanos);
this.keepAliveTimeoutNanos = keepAliveTimeoutNanos;
this.flowControlWindow = flowControlWindow;
this.keepAliveWithoutCalls = keepAliveWithoutCalls;
this.maxInboundMetadataSize = maxInboundMetadataSize;
usingSharedExecutor = executor == null;
this.transportTracerFactory =
Preconditions.checkNotNull(transportTracerFactory, "transportTracerFactory");
if (usingSharedExecutor) {
// The executor was unspecified, using the shared executor.
this.executor = SharedResourceHolder.get(SHARED_EXECUTOR);
} else {
this.executor = executor;
}
}
@Override
public GoogleDefaultProtocolNegotiator buildProtocolNegotiator() {
TsiHandshakerFactory altsHandshakerFactory =
new TsiHandshakerFactory() {
@Override
public TsiHandshaker newHandshaker(String authority) {
// Used the shared grpc channel to connecting to the ALTS handshaker service.
// TODO: Release the channel if it is not used.
// https://github.com/grpc/grpc-java/issues/4755.
ManagedChannel channel =
SharedResourceHolder.get(HandshakerServiceChannel.SHARED_HANDSHAKER_CHANNEL);
AltsClientOptions handshakerOptions =
new AltsClientOptions.Builder()
.setRpcProtocolVersions(RpcProtocolVersionsUtil.getRpcProtocolVersions())
.setTargetName(authority)
.build();
return AltsTsiHandshaker.newClient(
HandshakerServiceGrpc.newStub(channel), handshakerOptions);
}
};
SslContext sslContext;
try {
sslContext = GrpcSslContexts.forClient().build();
} catch (SSLException ex) {
throw new RuntimeException(ex);
}
return negotiatorForTest =
new GoogleDefaultProtocolNegotiator(altsHandshakerFactory, sslContext);
}
@Override
public final synchronized void start(Listener listener) {
Preconditions.checkState(this.listener == null, "already started");
timerService = SharedResourceHolder.get(timerServiceResource);
executor = SharedResourceHolder.get(executorResource);
this.listener = Preconditions.checkNotNull(listener, "listener");
resolve();
}
private InProcessClientTransportFactory(
String name,
@Nullable ScheduledExecutorService scheduledExecutorService,
int maxInboundMetadataSize) {
this.name = name;
useSharedTimer = scheduledExecutorService == null;
timerService = useSharedTimer
? SharedResourceHolder.get(GrpcUtil.TIMER_SERVICE) : scheduledExecutorService;
this.maxInboundMetadataSize = maxInboundMetadataSize;
}
@Override
public final void start(final Listener2 listener) {
checkState(this.listener == null, "already started");
this.listener = checkNotNull(listener, "listener");
if (this.usingExecutorResource) {
this.executor = SharedResourceHolder.get(this.executorResource);
}
resolve();
}
@Override
public void start(final Listener2 listener) {
checkState(this.listener == null, "already started");
if (this.usingExecutorResource) {
this.executor = SharedResourceHolder.get(this.executorResource);
}
this.listener = checkNotNull(listener, "listener");
resolve();
}
@Override
public final void start(final Listener2 listener) {
checkState(this.listener == null, "already started");
this.listener = checkNotNull(listener, "listener");
if (this.usingExecutorResource) {
this.executor = SharedResourceHolder.get(this.executorResource);
}
resolve();
}
@Override
public void start(final Listener2 listener) {
checkState(this.listener == null, "already started");
if (this.usingExecutorResource) {
this.executor = SharedResourceHolder.get(this.executorResource);
}
this.listener = checkNotNull(listener, "listener");
resolve();
}
@Override
public void start(Listener listener) {
synchronized (lock) {
Preconditions.checkState(this.listener == null, "already started");
this.executor = SharedResourceHolder.get(GrpcUtil.SHARED_CHANNEL_EXECUTOR);
this.listener = Preconditions.checkNotNull(listener, "listener");
resolve();
}
}
/** Creates an XdsClient and starts a watch. */
public void createXdsClientAndStart() {
checkState(xdsClient == null, "start() called more than once");
Bootstrapper.BootstrapInfo bootstrapInfo;
List<Bootstrapper.ServerInfo> serverList;
try {
bootstrapInfo = Bootstrapper.getInstance().readBootstrap();
serverList = bootstrapInfo.getServers();
if (serverList.isEmpty()) {
throw new ManagementServerNotFoundException("No management server provided by bootstrap");
}
} catch (IOException | ManagementServerNotFoundException e) {
logger.log(Level.FINE, "Exception reading bootstrap", e);
logger.log(Level.INFO, "Fallback to plaintext for server at port {0}", port);
return;
}
Node node = bootstrapInfo.getNode();
timeService = SharedResourceHolder.get(timeServiceResource);
XdsClientImpl xdsClientImpl =
new XdsClientImpl(
"",
serverList,
XdsClient.XdsChannelFactory.getInstance(),
node,
createSynchronizationContext(),
timeService,
new ExponentialBackoffPolicy.Provider(),
GrpcUtil.STOPWATCH_SUPPLIER);
start(xdsClientImpl);
}
private InProcessClientTransportFactory(
String name,
@Nullable ScheduledExecutorService scheduledExecutorService,
int maxInboundMetadataSize, boolean includeCauseWithStatus) {
this.name = name;
useSharedTimer = scheduledExecutorService == null;
timerService = useSharedTimer
? SharedResourceHolder.get(GrpcUtil.TIMER_SERVICE) : scheduledExecutorService;
this.maxInboundMetadataSize = maxInboundMetadataSize;
this.includeCauseWithStatus = includeCauseWithStatus;
}
private OkHttpTransportFactory(
Executor executor,
@Nullable ScheduledExecutorService timeoutService,
@Nullable SocketFactory socketFactory,
@Nullable SSLSocketFactory sslSocketFactory,
@Nullable HostnameVerifier hostnameVerifier,
ConnectionSpec connectionSpec,
int maxMessageSize,
boolean enableKeepAlive,
long keepAliveTimeNanos,
long keepAliveTimeoutNanos,
int flowControlWindow,
boolean keepAliveWithoutCalls,
int maxInboundMetadataSize,
TransportTracer.Factory transportTracerFactory,
boolean useGetForSafeMethods) {
usingSharedScheduler = timeoutService == null;
this.timeoutService = usingSharedScheduler
? SharedResourceHolder.get(GrpcUtil.TIMER_SERVICE) : timeoutService;
this.socketFactory = socketFactory;
this.sslSocketFactory = sslSocketFactory;
this.hostnameVerifier = hostnameVerifier;
this.connectionSpec = connectionSpec;
this.maxMessageSize = maxMessageSize;
this.enableKeepAlive = enableKeepAlive;
this.keepAliveTimeNanos = new AtomicBackoff("keepalive time nanos", keepAliveTimeNanos);
this.keepAliveTimeoutNanos = keepAliveTimeoutNanos;
this.flowControlWindow = flowControlWindow;
this.keepAliveWithoutCalls = keepAliveWithoutCalls;
this.maxInboundMetadataSize = maxInboundMetadataSize;
this.useGetForSafeMethods = useGetForSafeMethods;
usingSharedExecutor = executor == null;
this.transportTracerFactory =
Preconditions.checkNotNull(transportTracerFactory, "transportTracerFactory");
if (usingSharedExecutor) {
// The executor was unspecified, using the shared executor.
this.executor = SharedResourceHolder.get(SHARED_EXECUTOR);
} else {
this.executor = executor;
}
}