1 /*
2  * Copyright 2017 Google LLC
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google LLC nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 package com.google.api.gax.grpc;
31 
32 import com.google.api.core.InternalExtensionOnly;
33 import com.google.api.gax.core.ResourceCloseException;
34 import com.google.api.gax.rpc.TransportChannel;
35 import com.google.auto.value.AutoValue;
36 import io.grpc.Channel;
37 import io.grpc.ManagedChannel;
38 import java.util.concurrent.TimeUnit;
39 
40 /** Implementation of TransportChannel based on gRPC. */
41 @AutoValue
42 @InternalExtensionOnly
43 public abstract class GrpcTransportChannel implements TransportChannel {
44 
45   /** The name of the Grpc transport. */
getGrpcTransportName()46   public static String getGrpcTransportName() {
47     return "grpc";
48   }
49 
50   @Override
getTransportName()51   public String getTransportName() {
52     return getGrpcTransportName();
53   }
54 
55   @Override
getEmptyCallContext()56   public GrpcCallContext getEmptyCallContext() {
57     return GrpcCallContext.createDefault();
58   }
59 
60   /** The channel in use. */
getManagedChannel()61   abstract ManagedChannel getManagedChannel();
62 
getChannel()63   public Channel getChannel() {
64     return getManagedChannel();
65   }
66 
67   @Override
shutdown()68   public void shutdown() {
69     getManagedChannel().shutdown();
70   }
71 
72   @Override
isShutdown()73   public boolean isShutdown() {
74     return getManagedChannel().isShutdown();
75   }
76 
77   @Override
isTerminated()78   public boolean isTerminated() {
79     return getManagedChannel().isTerminated();
80   }
81 
82   @Override
shutdownNow()83   public void shutdownNow() {
84     getManagedChannel().shutdownNow();
85   }
86 
87   @Override
awaitTermination(long duration, TimeUnit unit)88   public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException {
89     return getManagedChannel().awaitTermination(duration, unit);
90   }
91 
92   @Override
close()93   public void close() {
94     getManagedChannel().shutdown();
95     try {
96       awaitTermination(6, TimeUnit.MINUTES);
97     } catch (InterruptedException e) {
98       throw new ResourceCloseException(e);
99     }
100   }
101 
newBuilder()102   public static Builder newBuilder() {
103     return new AutoValue_GrpcTransportChannel.Builder();
104   }
105 
create(ManagedChannel channel)106   public static GrpcTransportChannel create(ManagedChannel channel) {
107     return newBuilder().setManagedChannel(channel).build();
108   }
109 
110   @AutoValue.Builder
111   public abstract static class Builder {
setManagedChannel(ManagedChannel value)112     public abstract Builder setManagedChannel(ManagedChannel value);
113 
build()114     public abstract GrpcTransportChannel build();
115   }
116 }
117