public static CompletionStage connect()

in folsom/src/main/java/com/spotify/folsom/client/DefaultRawMemcacheClient.java [108:195]


  public static CompletionStage<RawMemcacheClient> connect(
      final HostAndPort address,
      final int outstandingRequestLimit,
      final int eventLoopThreadFlushMaxBatchSize,
      final boolean binary,
      final Executor executor,
      final long connectionTimeoutMillis,
      final Charset charset,
      final Metrics metrics,
      final int maxSetLength,
      final EventLoopGroup eventLoopGroup,
      final Class<? extends Channel> channelClass,
      final SSLEngineFactory sslEngineFactory) {

    final ChannelInboundHandler decoder;
    if (binary) {
      decoder = new BinaryMemcacheDecoder();
    } else {
      decoder = new AsciiMemcacheDecoder(charset);
    }

    final ChannelHandler initializer =
        new ChannelInitializer<Channel>() {
          @Override
          protected void initChannel(final Channel ch) {
            final ChannelPipeline channelPipeline = ch.pipeline();
            channelPipeline.addLast(new TcpTuningHandler());

            if (sslEngineFactory != null) {
              final SSLEngine sslEngine =
                  sslEngineFactory.createSSLEngine(address.getHostText(), address.getPort());
              SslHandler sslHandler = new SslHandler(sslEngine);
              // Disable SSL data aggregation
              // it doesn't play well with memcached protocol and causes connection hangs
              sslHandler.setWrapDataSize(0);
              channelPipeline.addLast(sslHandler);
            }

            channelPipeline.addLast(decoder, new MemcacheEncoder());
          }
        };

    final CompletableFuture<RawMemcacheClient> clientFuture = new CompletableFuture<>();

    final EventLoopGroup effectiveELG =
        eventLoopGroup != null ? eventLoopGroup : DEFAULT_EVENT_LOOP_GROUP.get();
    Class<? extends Channel> effectiveChannelClass =
        channelClass != null ? channelClass : defaultChannelClass(effectiveELG);

    final Bootstrap bootstrap =
        new Bootstrap()
            .group(effectiveELG)
            .handler(initializer)
            .channel(effectiveChannelClass)
            .option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, SimpleSizeEstimator.INSTANCE);

    final ChannelFuture connectFuture =
        bootstrap.connect(new InetSocketAddress(address.getHostText(), address.getPort()));

    connectFuture.addListener(
        (ChannelFutureListener)
            future -> {
              final Channel channel = future.channel();
              if (channel == null) {
                clientFuture.completeExceptionally(new IOException("Channel is closed"));
                return;
              }
              if (future.isSuccess()) {
                // Create client
                final RawMemcacheClient client =
                    new DefaultRawMemcacheClient(
                        address,
                        channel,
                        outstandingRequestLimit,
                        eventLoopThreadFlushMaxBatchSize,
                        executor,
                        connectionTimeoutMillis,
                        metrics,
                        maxSetLength);
                clientFuture.complete(client);
              } else {
                channel.close();
                clientFuture.completeExceptionally(future.cause());
              }
            });

    return clientFuture;
  }