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

grpc / grpc-java / #19572

27 Nov 2024 10:56PM CUT coverage: 84.622%. Remained the same
#19572

push

github

web-flow
[CSM] Use xds-enabled server and xds credentials in examples (#11706)

Backport #11706 to v1.68.x

33853 of 40005 relevant lines covered (84.62%)

0.85 hits per line

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

85.31
/../netty/src/main/java/io/grpc/netty/Utils.java
1
/*
2
 * Copyright 2014 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
import static io.grpc.internal.GrpcUtil.CONTENT_TYPE_KEY;
21
import static io.grpc.internal.TransportFrameUtil.toHttp2Headers;
22
import static io.grpc.internal.TransportFrameUtil.toRawSerializedHeaders;
23
import static io.netty.channel.ChannelOption.SO_LINGER;
24
import static io.netty.channel.ChannelOption.SO_TIMEOUT;
25
import static io.netty.util.CharsetUtil.UTF_8;
26

27
import com.google.common.annotations.VisibleForTesting;
28
import com.google.common.base.Preconditions;
29
import io.grpc.InternalChannelz;
30
import io.grpc.InternalMetadata;
31
import io.grpc.Metadata;
32
import io.grpc.Status;
33
import io.grpc.internal.GrpcUtil;
34
import io.grpc.internal.SharedResourceHolder.Resource;
35
import io.grpc.internal.TransportTracer;
36
import io.grpc.netty.GrpcHttp2HeadersUtils.GrpcHttp2InboundHeaders;
37
import io.grpc.netty.NettySocketSupport.NativeSocketOptions;
38
import io.netty.buffer.ByteBufAllocator;
39
import io.netty.buffer.PooledByteBufAllocator;
40
import io.netty.buffer.UnpooledByteBufAllocator;
41
import io.netty.channel.Channel;
42
import io.netty.channel.ChannelConfig;
43
import io.netty.channel.ChannelFactory;
44
import io.netty.channel.ChannelOption;
45
import io.netty.channel.EventLoopGroup;
46
import io.netty.channel.ReflectiveChannelFactory;
47
import io.netty.channel.ServerChannel;
48
import io.netty.channel.nio.NioEventLoopGroup;
49
import io.netty.channel.socket.nio.NioServerSocketChannel;
50
import io.netty.channel.socket.nio.NioSocketChannel;
51
import io.netty.handler.codec.DecoderException;
52
import io.netty.handler.codec.http2.Http2Connection;
53
import io.netty.handler.codec.http2.Http2Exception;
54
import io.netty.handler.codec.http2.Http2FlowController;
55
import io.netty.handler.codec.http2.Http2Headers;
56
import io.netty.handler.codec.http2.Http2Stream;
57
import io.netty.util.AsciiString;
58
import io.netty.util.NettyRuntime;
59
import io.netty.util.concurrent.DefaultThreadFactory;
60
import java.io.IOException;
61
import java.lang.reflect.Constructor;
62
import java.nio.channels.ClosedChannelException;
63
import java.nio.channels.UnresolvedAddressException;
64
import java.util.Locale;
65
import java.util.Map;
66
import java.util.concurrent.ThreadFactory;
67
import java.util.concurrent.TimeUnit;
68
import java.util.logging.Level;
69
import java.util.logging.Logger;
70
import javax.annotation.CheckReturnValue;
71
import javax.annotation.Nullable;
72
import javax.net.ssl.SSLException;
73

74
/**
75
 * Common utility methods.
76
 */
77
class Utils {
78
  private static final Logger logger = Logger.getLogger(Utils.class.getName());
1✔
79

80
  public static final AsciiString STATUS_OK = AsciiString.of("200");
1✔
81
  public static final AsciiString HTTP_METHOD = AsciiString.of(GrpcUtil.HTTP_METHOD);
1✔
82
  public static final AsciiString HTTP_GET_METHOD = AsciiString.of("GET");
1✔
83
  public static final AsciiString HTTPS = AsciiString.of("https");
1✔
84
  public static final AsciiString HTTP = AsciiString.of("http");
1✔
85
  public static final AsciiString CONTENT_TYPE_HEADER = AsciiString.of(CONTENT_TYPE_KEY.name());
1✔
86
  public static final AsciiString CONTENT_TYPE_GRPC = AsciiString.of(GrpcUtil.CONTENT_TYPE_GRPC);
1✔
87
  public static final AsciiString TE_HEADER = AsciiString.of(GrpcUtil.TE_HEADER.name());
1✔
88
  public static final AsciiString TE_TRAILERS = AsciiString.of(GrpcUtil.TE_TRAILERS);
1✔
89
  public static final AsciiString USER_AGENT = AsciiString.of(GrpcUtil.USER_AGENT_KEY.name());
1✔
90
  public static final Resource<EventLoopGroup> NIO_BOSS_EVENT_LOOP_GROUP
1✔
91
      = new DefaultEventLoopGroupResource(1, "grpc-nio-boss-ELG", EventLoopGroupType.NIO);
92
  public static final Resource<EventLoopGroup> NIO_WORKER_EVENT_LOOP_GROUP
1✔
93
      = new DefaultEventLoopGroupResource(0, "grpc-nio-worker-ELG", EventLoopGroupType.NIO);
94

95
  public static final Resource<EventLoopGroup> DEFAULT_BOSS_EVENT_LOOP_GROUP;
96
  public static final Resource<EventLoopGroup> DEFAULT_WORKER_EVENT_LOOP_GROUP;
97

98
  // This class is initialized on first use, thus provides delayed allocator creation.
99
  private static final class ByteBufAllocatorPreferDirectHolder {
100
    private static final ByteBufAllocator allocator = createByteBufAllocator(true);
1✔
101
  }
102

103
  // This class is initialized on first use, thus provides delayed allocator creation.
104
  private static final class ByteBufAllocatorPreferHeapHolder {
105
    private static final ByteBufAllocator allocator = createByteBufAllocator(false);
1✔
106
  }
107

108
  public static final ChannelFactory<? extends ServerChannel> DEFAULT_SERVER_CHANNEL_FACTORY;
109
  public static final Class<? extends Channel> DEFAULT_CLIENT_CHANNEL_TYPE;
110
  public static final Class<? extends Channel> EPOLL_DOMAIN_CLIENT_CHANNEL_TYPE;
111

112
  @Nullable
113
  private static final Constructor<? extends EventLoopGroup> EPOLL_EVENT_LOOP_GROUP_CONSTRUCTOR;
114

115
  static {
116
    // Decide default channel types and EventLoopGroup based on Epoll availability
117
    if (isEpollAvailable()) {
1✔
118
      DEFAULT_CLIENT_CHANNEL_TYPE = epollChannelType();
1✔
119
      EPOLL_DOMAIN_CLIENT_CHANNEL_TYPE = epollDomainSocketChannelType();
1✔
120
      DEFAULT_SERVER_CHANNEL_FACTORY = new ReflectiveChannelFactory<>(epollServerChannelType());
1✔
121
      EPOLL_EVENT_LOOP_GROUP_CONSTRUCTOR = epollEventLoopGroupConstructor();
1✔
122
      DEFAULT_BOSS_EVENT_LOOP_GROUP
1✔
123
        = new DefaultEventLoopGroupResource(1, "grpc-default-boss-ELG", EventLoopGroupType.EPOLL);
124
      DEFAULT_WORKER_EVENT_LOOP_GROUP
1✔
125
        = new DefaultEventLoopGroupResource(0,"grpc-default-worker-ELG", EventLoopGroupType.EPOLL);
126
    } else {
127
      logger.log(Level.FINE, "Epoll is not available, using Nio.", getEpollUnavailabilityCause());
1✔
128
      DEFAULT_SERVER_CHANNEL_FACTORY = nioServerChannelFactory();
1✔
129
      DEFAULT_CLIENT_CHANNEL_TYPE = NioSocketChannel.class;
1✔
130
      EPOLL_DOMAIN_CLIENT_CHANNEL_TYPE = null;
1✔
131
      DEFAULT_BOSS_EVENT_LOOP_GROUP = NIO_BOSS_EVENT_LOOP_GROUP;
1✔
132
      DEFAULT_WORKER_EVENT_LOOP_GROUP = NIO_WORKER_EVENT_LOOP_GROUP;
1✔
133
      EPOLL_EVENT_LOOP_GROUP_CONSTRUCTOR = null;
1✔
134
    }
135
  }
1✔
136

137
  public static ByteBufAllocator getByteBufAllocator(boolean forceHeapBuffer) {
138
    if (Boolean.parseBoolean(
1✔
139
            System.getProperty("io.grpc.netty.useCustomAllocator", "true"))) {
1✔
140

141
      String allocType = System.getProperty("io.netty.allocator.type", "pooled");
1✔
142
      if (allocType.toLowerCase(Locale.ROOT).equals("unpooled")) {
1✔
143
        logger.log(Level.FINE, "Using unpooled allocator");
1✔
144
        return UnpooledByteBufAllocator.DEFAULT;
1✔
145
      }
146

147
      boolean defaultPreferDirect = PooledByteBufAllocator.defaultPreferDirect();
1✔
148
      logger.log(
1✔
149
          Level.FINE,
150
          "Using custom allocator: forceHeapBuffer={0}, defaultPreferDirect={1}",
151
          new Object[] { forceHeapBuffer, defaultPreferDirect });
1✔
152
      if (forceHeapBuffer || !defaultPreferDirect) {
1✔
153
        return ByteBufAllocatorPreferHeapHolder.allocator;
1✔
154
      } else {
155
        return ByteBufAllocatorPreferDirectHolder.allocator;
1✔
156
      }
157
    } else {
158
      logger.log(Level.FINE, "Using default allocator");
×
159
      return ByteBufAllocator.DEFAULT;
×
160
    }
161
  }
162

163
  private static ByteBufAllocator createByteBufAllocator(boolean preferDirect) {
164
    int maxOrder;
165
    logger.log(Level.FINE, "Creating allocator, preferDirect=" + preferDirect);
1✔
166
    if (System.getProperty("io.netty.allocator.maxOrder") == null) {
1✔
167
      // See the implementation of PooledByteBufAllocator.  DEFAULT_MAX_ORDER in there is
168
      // 11, which makes chunk size to be 8192 << 11 = 16 MiB.  We want the chunk size to be
169
      // 2MiB, thus reducing the maxOrder to 8.
170
      maxOrder = 8;
1✔
171
      logger.log(Level.FINE, "Forcing maxOrder=" + maxOrder);
1✔
172
    } else {
173
      maxOrder = PooledByteBufAllocator.defaultMaxOrder();
×
174
      logger.log(Level.FINE, "Using default maxOrder=" + maxOrder);
×
175
    }
176
    return new PooledByteBufAllocator(
1✔
177
        preferDirect,
178
        PooledByteBufAllocator.defaultNumHeapArena(),
1✔
179
        // Assuming neither gRPC nor netty are using allocator.directBuffer() to request
180
        // specifically for direct buffers, which is true as I just checked, setting arenas to 0
181
        // will make sure no direct buffer is ever created.
182
        preferDirect ? PooledByteBufAllocator.defaultNumDirectArena() : 0,
1✔
183
        PooledByteBufAllocator.defaultPageSize(),
1✔
184
        maxOrder,
185
        PooledByteBufAllocator.defaultSmallCacheSize(),
1✔
186
        PooledByteBufAllocator.defaultNormalCacheSize(),
1✔
187
        PooledByteBufAllocator.defaultUseCacheForAllThreads());
1✔
188
  }
189

190
  public static Metadata convertHeaders(Http2Headers http2Headers) {
191
    if (http2Headers instanceof GrpcHttp2InboundHeaders) {
1✔
192
      GrpcHttp2InboundHeaders h = (GrpcHttp2InboundHeaders) http2Headers;
1✔
193
      return InternalMetadata.newMetadata(h.numHeaders(), h.namesAndValues());
1✔
194
    }
195
    return InternalMetadata.newMetadata(convertHeadersToArray(http2Headers));
1✔
196
  }
197

198
  @CheckReturnValue
199
  private static byte[][] convertHeadersToArray(Http2Headers http2Headers) {
200
    // The Netty AsciiString class is really just a wrapper around a byte[] and supports
201
    // arbitrary binary data, not just ASCII.
202
    byte[][] headerValues = new byte[http2Headers.size() * 2][];
1✔
203
    int i = 0;
1✔
204
    for (Map.Entry<CharSequence, CharSequence> entry : http2Headers) {
1✔
205
      headerValues[i++] = bytes(entry.getKey());
1✔
206
      headerValues[i++] = bytes(entry.getValue());
1✔
207
    }
1✔
208
    return toRawSerializedHeaders(headerValues);
1✔
209
  }
210

211
  private static byte[] bytes(CharSequence seq) {
212
    if (seq instanceof AsciiString) {
1✔
213
      // Fast path - sometimes copy.
214
      AsciiString str = (AsciiString) seq;
1✔
215
      return str.isEntireArrayUsed() ? str.array() : str.toByteArray();
1✔
216
    }
217
    // Slow path - copy.
218
    return seq.toString().getBytes(UTF_8);
1✔
219
  }
220

221
  public static Http2Headers convertClientHeaders(Metadata headers,
222
      AsciiString scheme,
223
      AsciiString defaultPath,
224
      AsciiString authority,
225
      AsciiString method,
226
      AsciiString userAgent) {
227
    Preconditions.checkNotNull(defaultPath, "defaultPath");
1✔
228
    Preconditions.checkNotNull(authority, "authority");
1✔
229
    Preconditions.checkNotNull(method, "method");
1✔
230

231
    // Discard any application supplied duplicates of the reserved headers
232
    headers.discardAll(CONTENT_TYPE_KEY);
1✔
233
    headers.discardAll(GrpcUtil.TE_HEADER);
1✔
234
    headers.discardAll(GrpcUtil.USER_AGENT_KEY);
1✔
235

236
    return GrpcHttp2OutboundHeaders.clientRequestHeaders(
1✔
237
        toHttp2Headers(headers),
1✔
238
        authority,
239
        defaultPath,
240
        method,
241
        scheme,
242
        userAgent);
243
  }
244

245
  public static Http2Headers convertServerHeaders(Metadata headers) {
246
    // Discard any application supplied duplicates of the reserved headers
247
    headers.discardAll(CONTENT_TYPE_KEY);
1✔
248
    headers.discardAll(GrpcUtil.TE_HEADER);
1✔
249
    headers.discardAll(GrpcUtil.USER_AGENT_KEY);
1✔
250

251
    return GrpcHttp2OutboundHeaders.serverResponseHeaders(toHttp2Headers(headers));
1✔
252
  }
253

254
  public static Metadata convertTrailers(Http2Headers http2Headers) {
255
    if (http2Headers instanceof GrpcHttp2InboundHeaders) {
1✔
256
      GrpcHttp2InboundHeaders h = (GrpcHttp2InboundHeaders) http2Headers;
1✔
257
      return InternalMetadata.newMetadata(h.numHeaders(), h.namesAndValues());
1✔
258
    }
259
    return InternalMetadata.newMetadata(convertHeadersToArray(http2Headers));
1✔
260
  }
261

262
  public static Http2Headers convertTrailers(Metadata trailers, boolean headersSent) {
263
    if (!headersSent) {
1✔
264
      return convertServerHeaders(trailers);
1✔
265
    }
266
    return GrpcHttp2OutboundHeaders.serverResponseTrailers(toHttp2Headers(trailers));
1✔
267
  }
268

269
  public static Status statusFromThrowable(Throwable t) {
270
    Status s = Status.fromThrowable(t);
1✔
271
    if (s.getCode() != Status.Code.UNKNOWN) {
1✔
272
      return s;
1✔
273
    }
274
    if (t instanceof ClosedChannelException) {
1✔
275
      if (t.getCause() != null) {
1✔
276
        // If the remote closes the connection while the event loop is processing, then a write or
277
        // flush can be the first operation to notice the closure. Those exceptions are a
278
        // ClosedChannelException, with a cause that provides more information, which is exactly
279
        // what we'd hope for.
280
        return Status.UNAVAILABLE.withDescription("channel closed").withCause(t);
×
281
      }
282
      // ClosedChannelException is used for all operations after the Netty channel is closed. But it
283
      // doesn't have the original closure information. Proper error processing requires remembering
284
      // the error that occurred before this one and using it instead.
285
      //
286
      // Netty uses an exception that has no stack trace, while we would never hope to show this to
287
      // users, if it happens having the extra information may provide a small hint of where to
288
      // look.
289
      ClosedChannelException extraT = new ClosedChannelException();
1✔
290
      extraT.initCause(t);
1✔
291
      return Status.UNKNOWN.withDescription("channel closed").withCause(extraT);
1✔
292
    }
293
    if (t instanceof DecoderException && t.getCause() instanceof SSLException) {
1✔
294
      return Status.UNAVAILABLE.withDescription("ssl exception").withCause(t);
1✔
295
    }
296
    if (t instanceof IOException) {
1✔
297
      return Status.UNAVAILABLE.withDescription("io exception").withCause(t);
1✔
298
    }
299
    if (t instanceof UnresolvedAddressException) {
1✔
300
      return Status.UNAVAILABLE.withDescription("unresolved address").withCause(t);
1✔
301
    }
302
    if (t instanceof Http2Exception) {
1✔
303
      return Status.INTERNAL.withDescription("http2 exception").withCause(t);
1✔
304
    }
305
    return s;
1✔
306
  }
307

308
  @VisibleForTesting
309
  static boolean isEpollAvailable() {
310
    try {
311
      return (boolean) (Boolean)
1✔
312
          Class
313
              .forName("io.netty.channel.epoll.Epoll")
1✔
314
              .getDeclaredMethod("isAvailable")
1✔
315
              .invoke(null);
1✔
316
    } catch (ClassNotFoundException e) {
1✔
317
      // this is normal if netty-epoll runtime dependency doesn't exist.
318
      return false;
1✔
319
    } catch (Exception e) {
×
320
      throw new RuntimeException("Exception while checking Epoll availability", e);
×
321
    }
322
  }
323

324
  private static Throwable getEpollUnavailabilityCause() {
325
    try {
326
      return (Throwable)
1✔
327
          Class
328
              .forName("io.netty.channel.epoll.Epoll")
×
329
              .getDeclaredMethod("unavailabilityCause")
×
330
              .invoke(null);
×
331
    } catch (Exception e) {
1✔
332
      return e;
1✔
333
    }
334
  }
335

336
  // Must call when epoll is available
337
  private static Class<? extends Channel> epollChannelType() {
338
    try {
339
      Class<? extends Channel> channelType = Class
1✔
340
          .forName("io.netty.channel.epoll.EpollSocketChannel").asSubclass(Channel.class);
1✔
341
      return channelType;
1✔
342
    } catch (ClassNotFoundException e) {
×
343
      throw new RuntimeException("Cannot load EpollSocketChannel", e);
×
344
    }
345
  }
346

347
  // Must call when epoll is available
348
  private static Class<? extends Channel> epollDomainSocketChannelType() {
349
    try {
350
      Class<? extends Channel> channelType = Class
1✔
351
          .forName("io.netty.channel.epoll.EpollDomainSocketChannel").asSubclass(Channel.class);
1✔
352
      return channelType;
1✔
353
    } catch (ClassNotFoundException e) {
×
354
      throw new RuntimeException("Cannot load EpollDomainSocketChannel", e);
×
355
    }
356
  }
357

358
  // Must call when epoll is available
359
  private static Constructor<? extends EventLoopGroup> epollEventLoopGroupConstructor() {
360
    try {
361
      return Class
1✔
362
          .forName("io.netty.channel.epoll.EpollEventLoopGroup").asSubclass(EventLoopGroup.class)
1✔
363
          .getConstructor(Integer.TYPE, ThreadFactory.class);
1✔
364
    } catch (ClassNotFoundException e) {
×
365
      throw new RuntimeException("Cannot load EpollEventLoopGroup", e);
×
366
    } catch (NoSuchMethodException e) {
×
367
      throw new RuntimeException("EpollEventLoopGroup constructor not found", e);
×
368
    }
369
  }
370

371
  // Must call when epoll is available
372
  private static Class<? extends ServerChannel> epollServerChannelType() {
373
    try {
374
      Class<? extends ServerChannel> serverSocketChannel =
1✔
375
          Class
376
              .forName("io.netty.channel.epoll.EpollServerSocketChannel")
1✔
377
              .asSubclass(ServerChannel.class);
1✔
378
      return serverSocketChannel;
1✔
379
    } catch (ClassNotFoundException e) {
×
380
      throw new RuntimeException("Cannot load EpollServerSocketChannel", e);
×
381
    }
382
  }
383

384
  private static EventLoopGroup createEpollEventLoopGroup(
385
      int parallelism,
386
      ThreadFactory threadFactory) {
387
    checkState(EPOLL_EVENT_LOOP_GROUP_CONSTRUCTOR != null, "Epoll is not available");
1✔
388

389
    try {
390
      return EPOLL_EVENT_LOOP_GROUP_CONSTRUCTOR
1✔
391
          .newInstance(parallelism, threadFactory);
1✔
392
    } catch (Exception e) {
×
393
      throw new RuntimeException("Cannot create Epoll EventLoopGroup", e);
×
394
    }
395
  }
396

397
  private static ChannelFactory<ServerChannel> nioServerChannelFactory() {
398
    return new ChannelFactory<ServerChannel>() {
1✔
399
      @Override
400
      public ServerChannel newChannel() {
401
        return new NioServerSocketChannel();
1✔
402
      }
403
    };
404
  }
405

406
  /**
407
   * Returns TCP_USER_TIMEOUT channel option for Epoll channel if Epoll is available, otherwise
408
   * null.
409
   */
410
  @Nullable
411
  static ChannelOption<Integer> maybeGetTcpUserTimeoutOption() {
412
    return getEpollChannelOption("TCP_USER_TIMEOUT");
1✔
413
  }
414

415
  @Nullable
416
  @SuppressWarnings("unchecked")
417
  private static <T> ChannelOption<T> getEpollChannelOption(String optionName) {
418
    if (isEpollAvailable()) {
1✔
419
      try {
420
        return
1✔
421
            (ChannelOption<T>) Class.forName("io.netty.channel.epoll.EpollChannelOption")
1✔
422
                .getField(optionName)
1✔
423
                .get(null);
1✔
424
      } catch (Exception e) {
×
425
        throw new RuntimeException("ChannelOption(" + optionName + ") is not available", e);
×
426
      }
427
    }
428
    return null;
×
429
  }
430

431
  private static final class DefaultEventLoopGroupResource implements Resource<EventLoopGroup> {
432
    private final String name;
433
    private final int numEventLoops;
434
    private final EventLoopGroupType eventLoopGroupType;
435

436
    DefaultEventLoopGroupResource(
437
        int numEventLoops, String name, EventLoopGroupType eventLoopGroupType) {
1✔
438
      this.name = name;
1✔
439
      // See the implementation of MultithreadEventLoopGroup.  DEFAULT_EVENT_LOOP_THREADS there
440
      // defaults to NettyRuntime.availableProcessors() * 2.  We don't think we need that many
441
      // threads.  The overhead of a thread includes file descriptors and at least one chunk
442
      // allocation from PooledByteBufAllocator.  Here we reduce the default number of threads by
443
      // half.
444
      if (numEventLoops == 0 && System.getProperty("io.netty.eventLoopThreads") == null) {
1✔
445
        this.numEventLoops = NettyRuntime.availableProcessors();
1✔
446
      } else {
447
        this.numEventLoops = numEventLoops;
1✔
448
      }
449
      this.eventLoopGroupType = eventLoopGroupType;
1✔
450
    }
1✔
451

452
    @Override
453
    public EventLoopGroup create() {
454
      // Use Netty's DefaultThreadFactory in order to get the benefit of FastThreadLocal.
455
      ThreadFactory threadFactory = new DefaultThreadFactory(name, /* daemon= */ true);
1✔
456
      switch (eventLoopGroupType) {
1✔
457
        case NIO:
458
          return new NioEventLoopGroup(numEventLoops, threadFactory);
1✔
459
        case EPOLL:
460
          return createEpollEventLoopGroup(numEventLoops, threadFactory);
1✔
461
        default:
462
          throw new AssertionError("Unknown/Unsupported EventLoopGroupType: " + eventLoopGroupType);
×
463
      }
464
    }
465

466
    @Override
467
    public void close(EventLoopGroup instance) {
468
      instance.shutdownGracefully(0, 0, TimeUnit.SECONDS);
1✔
469
    }
1✔
470

471
    @Override
472
    public String toString() {
473
      return name;
×
474
    }
475
  }
476

477
  static final class FlowControlReader implements TransportTracer.FlowControlReader {
478
    private final Http2Stream connectionStream;
479
    private final Http2FlowController local;
480
    private final Http2FlowController remote;
481

482
    FlowControlReader(Http2Connection connection) {
1✔
483
      // 'local' in Netty is the _controller_ that controls inbound data. 'local' in Channelz is
484
      // the _present window_ provided by the remote that allows data to be sent. They are
485
      // opposites.
486
      local = connection.remote().flowController();
1✔
487
      remote = connection.local().flowController();
1✔
488
      connectionStream = connection.connectionStream();
1✔
489
    }
1✔
490

491
    @Override
492
    public TransportTracer.FlowControlWindows read() {
493
      return new TransportTracer.FlowControlWindows(
1✔
494
          local.windowSize(connectionStream),
1✔
495
          remote.windowSize(connectionStream));
1✔
496
    }
497
  }
498

499
  static InternalChannelz.SocketOptions getSocketOptions(Channel channel) {
500
    ChannelConfig config = channel.config();
1✔
501
    InternalChannelz.SocketOptions.Builder b = new InternalChannelz.SocketOptions.Builder();
1✔
502

503
    // The API allows returning null but not sure if it can happen in practice.
504
    // Let's be paranoid and do null checking just in case.
505
    Integer lingerSeconds = config.getOption(SO_LINGER);
1✔
506
    if (lingerSeconds != null) {
1✔
507
      b.setSocketOptionLingerSeconds(lingerSeconds);
1✔
508
    }
509

510
    Integer timeoutMillis = config.getOption(SO_TIMEOUT);
1✔
511
    if (timeoutMillis != null) {
1✔
512
      // in java, SO_TIMEOUT only applies to receiving
513
      b.setSocketOptionTimeoutMillis(timeoutMillis);
1✔
514
    }
515

516
    for (Map.Entry<ChannelOption<?>, Object> opt : config.getOptions().entrySet()) {
1✔
517
      ChannelOption<?> key = opt.getKey();
1✔
518
      // Constants are pooled, so there should only be one instance of each constant
519
      if (key.equals(SO_LINGER) || key.equals(SO_TIMEOUT)) {
1✔
520
        continue;
1✔
521
      }
522
      Object value = opt.getValue();
1✔
523
      // zpencer: Can a netty option be null?
524
      b.addOption(key.name(), String.valueOf(value));
1✔
525
    }
1✔
526

527
    NativeSocketOptions nativeOptions
1✔
528
        = NettySocketSupport.getNativeSocketOptions(channel);
1✔
529
    if (nativeOptions != null) {
1✔
530
      b.setTcpInfo(nativeOptions.tcpInfo); // may be null
×
531
      for (Map.Entry<String, String> entry : nativeOptions.otherInfo.entrySet()) {
×
532
        b.addOption(entry.getKey(), entry.getValue());
×
533
      }
×
534
    }
535
    return b.build();
1✔
536
  }
537

538
  private enum EventLoopGroupType {
1✔
539
    NIO,
1✔
540
    EPOLL
1✔
541
  }
542

543
  private Utils() {
544
    // Prevents instantiation
545
  }
546
}
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