• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

grpc / grpc-java / #19971

01 Sep 2025 07:40AM UTC coverage: 88.568% (+0.02%) from 88.547%
#19971

push

github

web-flow
allow java21 in jre matrix (#12281)

34692 of 39170 relevant lines covered (88.57%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.33
/../netty/src/main/java/io/grpc/netty/GrpcHttp2ConnectionHandler.java
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 com.google.common.annotations.VisibleForTesting;
22
import io.grpc.Attributes;
23
import io.grpc.ChannelLogger;
24
import io.grpc.Internal;
25
import io.grpc.InternalChannelz;
26
import io.netty.channel.ChannelPromise;
27
import io.netty.handler.codec.http2.Http2ConnectionDecoder;
28
import io.netty.handler.codec.http2.Http2ConnectionEncoder;
29
import io.netty.handler.codec.http2.Http2ConnectionHandler;
30
import io.netty.handler.codec.http2.Http2Settings;
31
import io.netty.util.Version;
32
import javax.annotation.Nullable;
33

34
/**
35
 * gRPC wrapper for {@link Http2ConnectionHandler}.
36
 */
37
@Internal
38
public abstract class GrpcHttp2ConnectionHandler extends Http2ConnectionHandler {
39
  static final int ADAPTIVE_CUMULATOR_COMPOSE_MIN_SIZE_DEFAULT = 1024;
40
  static final Cumulator ADAPTIVE_CUMULATOR =
1✔
41
      new NettyAdaptiveCumulator(ADAPTIVE_CUMULATOR_COMPOSE_MIN_SIZE_DEFAULT);
42

43
  @Nullable
44
  protected final ChannelPromise channelUnused;
45
  private final ChannelLogger negotiationLogger;
46
  private static final boolean usingPre4_1_111_Netty;
47

48
  static {
49
    // Netty 4.1.111 introduced a change in the behavior of duplicate() method
50
    // that breaks the assumption of the cumulator. We need to detect this version
51
    // and adjust the behavior accordingly.
52

53
    boolean identifiedOldVersion = false;
1✔
54
    try {
55
      Version version = Version.identify().get("netty-buffer");
1✔
56
      if (version != null) {
1✔
57
        String[] split = version.artifactVersion().split("\\.");
1✔
58
        if (split.length >= 3
1✔
59
            && Integer.parseInt(split[0]) == 4
1✔
60
            && Integer.parseInt(split[1]) <= 1
1✔
61
            && Integer.parseInt(split[2]) < 111) {
1✔
62
          identifiedOldVersion = true;
×
63
        }
64
      }
65
    } catch (Exception e) {
×
66
      // Ignore, we'll assume it's a new version.
67
    }
1✔
68
    usingPre4_1_111_Netty = identifiedOldVersion;
1✔
69
  }
1✔
70

71
  @SuppressWarnings("this-escape")
72
  protected GrpcHttp2ConnectionHandler(
73
      ChannelPromise channelUnused,
74
      Http2ConnectionDecoder decoder,
75
      Http2ConnectionEncoder encoder,
76
      Http2Settings initialSettings,
77
      ChannelLogger negotiationLogger) {
78
    super(decoder, encoder, initialSettings);
1✔
79
    this.channelUnused = channelUnused;
1✔
80
    this.negotiationLogger = negotiationLogger;
1✔
81
    if (usingPre4_1_111_Netty()) {
1✔
82
      // We need to use the adaptive cumulator only if we're using a version of Netty that
83
      // doesn't have the behavior that breaks it.
84
      setCumulator(ADAPTIVE_CUMULATOR);
×
85
    }
86
  }
1✔
87

88
  @VisibleForTesting
89
  static boolean usingPre4_1_111_Netty() {
90
    return usingPre4_1_111_Netty;
1✔
91
  }
92

93
  /**
94
   * Same as {@link #handleProtocolNegotiationCompleted(
95
   *   Attributes, io.grpc.InternalChannelz.Security)}
96
   * but with no {@link io.grpc.InternalChannelz.Security}.
97
   *
98
   * @deprecated Use the two argument method instead.
99
   */
100
  @Deprecated
101
  @SuppressWarnings("InlineMeSuggester") // the caller should consider providing securityInfo
102
  public void handleProtocolNegotiationCompleted(Attributes attrs) {
103
    handleProtocolNegotiationCompleted(attrs, /*securityInfo=*/ null);
×
104
  }
×
105

106
  /**
107
   * Triggered on protocol negotiation completion.
108
   *
109
   * <p>It must me called after negotiation is completed but before given handler is added to the
110
   * channel.
111
   *
112
   * @param attrs arbitrary attributes passed after protocol negotiation (eg. SSLSession).
113
   * @param securityInfo informs channelz about the security protocol.
114
   */
115
  public void handleProtocolNegotiationCompleted(
116
      Attributes attrs, InternalChannelz.Security securityInfo) {
117
  }
1✔
118

119
  /**
120
   * Returns the channel logger for the given channel context.
121
   */
122
  public ChannelLogger getNegotiationLogger() {
123
    checkState(negotiationLogger != null, "NegotiationLogger must not be null");
1✔
124
    return negotiationLogger;
1✔
125
  }
126

127
  /**
128
   * Calling this method indicates that the channel will no longer be used.  This method is roughly
129
   * the same as calling {@link #close} on the channel, but leaving the channel alive.  This is
130
   * useful if the channel will soon be deregistered from the executor and used in a non-Netty
131
   * context.
132
   */
133
  @SuppressWarnings("FutureReturnValueIgnored")
134
  public void notifyUnused() {
135
    channelUnused.setSuccess(null);
×
136
  }
×
137

138
  /** Get the attributes of the EquivalentAddressGroup used to create this transport. */
139
  public Attributes getEagAttributes() {
140
    return Attributes.EMPTY;
1✔
141
  }
142

143
  /**
144
   * Returns the authority of the server. Only available on the client-side.
145
   *
146
   * @throws UnsupportedOperationException if on server-side
147
   */
148
  public String getAuthority() {
149
    throw new UnsupportedOperationException();
×
150
  }
151
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc