• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 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.netty;
18 
19 import static com.google.common.base.Preconditions.checkState;
20 
21 import io.grpc.Attributes;
22 import io.grpc.ChannelLogger;
23 import io.grpc.Internal;
24 import io.grpc.InternalChannelz;
25 import io.netty.channel.ChannelPromise;
26 import io.netty.handler.codec.http2.Http2ConnectionDecoder;
27 import io.netty.handler.codec.http2.Http2ConnectionEncoder;
28 import io.netty.handler.codec.http2.Http2ConnectionHandler;
29 import io.netty.handler.codec.http2.Http2Settings;
30 import javax.annotation.Nullable;
31 
32 /**
33  * gRPC wrapper for {@link Http2ConnectionHandler}.
34  */
35 @Internal
36 public abstract class GrpcHttp2ConnectionHandler extends Http2ConnectionHandler {
37   static final int ADAPTIVE_CUMULATOR_COMPOSE_MIN_SIZE_DEFAULT = 1024;
38   static final Cumulator ADAPTIVE_CUMULATOR =
39       new NettyAdaptiveCumulator(ADAPTIVE_CUMULATOR_COMPOSE_MIN_SIZE_DEFAULT);
40 
41   @Nullable
42   protected final ChannelPromise channelUnused;
43   private final ChannelLogger negotiationLogger;
44 
GrpcHttp2ConnectionHandler( ChannelPromise channelUnused, Http2ConnectionDecoder decoder, Http2ConnectionEncoder encoder, Http2Settings initialSettings, ChannelLogger negotiationLogger)45   protected GrpcHttp2ConnectionHandler(
46       ChannelPromise channelUnused,
47       Http2ConnectionDecoder decoder,
48       Http2ConnectionEncoder encoder,
49       Http2Settings initialSettings,
50       ChannelLogger negotiationLogger) {
51     super(decoder, encoder, initialSettings);
52     this.channelUnused = channelUnused;
53     this.negotiationLogger = negotiationLogger;
54     setCumulator(ADAPTIVE_CUMULATOR);
55   }
56 
57   /**
58    * Same as {@link #handleProtocolNegotiationCompleted(
59    *   Attributes, io.grpc.InternalChannelz.Security)}
60    * but with no {@link io.grpc.InternalChannelz.Security}.
61    *
62    * @deprecated Use the two argument method instead.
63    */
64   @Deprecated
65   @SuppressWarnings("InlineMeSuggester") // the caller should consider providing securityInfo
handleProtocolNegotiationCompleted(Attributes attrs)66   public void handleProtocolNegotiationCompleted(Attributes attrs) {
67     handleProtocolNegotiationCompleted(attrs, /*securityInfo=*/ null);
68   }
69 
70   /**
71    * Triggered on protocol negotiation completion.
72    *
73    * <p>It must me called after negotiation is completed but before given handler is added to the
74    * channel.
75    *
76    * @param attrs arbitrary attributes passed after protocol negotiation (eg. SSLSession).
77    * @param securityInfo informs channelz about the security protocol.
78    */
handleProtocolNegotiationCompleted( Attributes attrs, InternalChannelz.Security securityInfo)79   public void handleProtocolNegotiationCompleted(
80       Attributes attrs, InternalChannelz.Security securityInfo) {
81   }
82 
83   /**
84    * Returns the channel logger for the given channel context.
85    */
getNegotiationLogger()86   public ChannelLogger getNegotiationLogger() {
87     checkState(negotiationLogger != null, "NegotiationLogger must not be null");
88     return negotiationLogger;
89   }
90 
91   /**
92    * Calling this method indicates that the channel will no longer be used.  This method is roughly
93    * the same as calling {@link #close} on the channel, but leaving the channel alive.  This is
94    * useful if the channel will soon be deregistered from the executor and used in a non-Netty
95    * context.
96    */
97   @SuppressWarnings("FutureReturnValueIgnored")
notifyUnused()98   public void notifyUnused() {
99     channelUnused.setSuccess(null);
100   }
101 
102   /** Get the attributes of the EquivalentAddressGroup used to create this transport. */
getEagAttributes()103   public Attributes getEagAttributes() {
104     return Attributes.EMPTY;
105   }
106 
107   /**
108    * Returns the authority of the server. Only available on the client-side.
109    *
110    * @throws UnsupportedOperationException if on server-side
111    */
getAuthority()112   public String getAuthority() {
113     throw new UnsupportedOperationException();
114   }
115 }
116