1 /* 2 * Copyright 2018 The gRPC Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.grpc.alts; 18 19 import io.grpc.ManagedChannel; 20 import io.grpc.internal.FixedObjectPool; 21 import io.grpc.internal.SharedResourceHolder.Resource; 22 import io.grpc.netty.NettyChannelBuilder; 23 import io.netty.channel.EventLoopGroup; 24 import io.netty.channel.nio.NioEventLoopGroup; 25 import io.netty.util.concurrent.DefaultThreadFactory; 26 import java.util.concurrent.ThreadFactory; 27 28 /** 29 * Class for creating a single shared gRPC channel to the ALTS Handshaker Service using 30 * SharedResourceHolder. The channel to the handshaker service is local and is over plaintext. Each 31 * application will have at most one connection to the handshaker service. 32 */ 33 final class HandshakerServiceChannel { 34 35 static final Resource<ManagedChannel> SHARED_HANDSHAKER_CHANNEL = 36 new Resource<ManagedChannel>() { 37 38 private EventLoopGroup eventGroup = null; 39 40 @Override 41 public ManagedChannel create() { 42 /* Use its own event loop thread pool to avoid blocking. */ 43 if (eventGroup == null) { 44 eventGroup = 45 new NioEventLoopGroup(1, new DefaultThreadFactory("handshaker pool", true)); 46 } 47 return NettyChannelBuilder.forTarget("metadata.google.internal:8080") 48 .directExecutor() 49 .eventLoopGroup(eventGroup) 50 .usePlaintext() 51 .build(); 52 } 53 54 @Override 55 @SuppressWarnings("FutureReturnValueIgnored") // netty ChannelFuture 56 public void close(ManagedChannel instance) { 57 instance.shutdownNow(); 58 if (eventGroup != null) { 59 eventGroup.shutdownGracefully(); 60 } 61 } 62 63 @Override 64 public String toString() { 65 return "grpc-alts-handshaker-service-channel"; 66 } 67 }; 68 69 /** Returns a fixed object pool of handshaker service channel for testing only. */ getHandshakerChannelPoolForTesting( String handshakerAddress)70 static FixedObjectPool<ManagedChannel> getHandshakerChannelPoolForTesting( 71 String handshakerAddress) { 72 ThreadFactory clientThreadFactory = new DefaultThreadFactory("handshaker pool", true); 73 ManagedChannel channel = 74 NettyChannelBuilder.forTarget(handshakerAddress) 75 .directExecutor() 76 .eventLoopGroup(new NioEventLoopGroup(1, clientThreadFactory)) 77 .usePlaintext() 78 .build(); 79 return new FixedObjectPool<ManagedChannel>(channel); 80 } 81 } 82