Java Code Examples for io.netty.handler.codec.http2.Http2Settings#initialWindowSize()

The following examples show how to use io.netty.handler.codec.http2.Http2Settings#initialWindowSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractNettyHandler.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
public void updateWindow() throws Http2Exception {
  if (!autoTuneFlowControlOn) {
    return;
  }
  pingReturn++;
  long elapsedTime = (System.nanoTime() - lastPingTime);
  if (elapsedTime == 0) {
    elapsedTime = 1;
  }
  long bandwidth = (getDataSincePing() * TimeUnit.SECONDS.toNanos(1)) / elapsedTime;
  Http2LocalFlowController fc = decoder().flowController();
  // Calculate new window size by doubling the observed BDP, but cap at max window
  int targetWindow = Math.min(getDataSincePing() * 2, MAX_WINDOW_SIZE);
  setPinging(false);
  int currentWindow = fc.initialWindowSize(connection().connectionStream());
  if (targetWindow > currentWindow && bandwidth > lastBandwidth) {
    lastBandwidth = bandwidth;
    int increase = targetWindow - currentWindow;
    fc.incrementWindowSize(connection().connectionStream(), increase);
    fc.initialWindowSize(targetWindow);
    Http2Settings settings = new Http2Settings();
    settings.initialWindowSize(targetWindow);
    frameWriter().writeSettings(ctx(), settings, ctx().newPromise());
  }

}
 
Example 2
Source File: HttpServerPipelineConfigurator.java    From armeria with Apache License 2.0 6 votes vote down vote up
private Http2Settings http2Settings() {
    final Http2Settings settings = new Http2Settings();
    final int initialWindowSize = config.http2InitialStreamWindowSize();
    if (initialWindowSize != Http2CodecUtil.DEFAULT_WINDOW_SIZE) {
        settings.initialWindowSize(initialWindowSize);
    }

    final int maxFrameSize = config.http2MaxFrameSize();
    if (maxFrameSize != Http2CodecUtil.DEFAULT_MAX_FRAME_SIZE) {
        settings.maxFrameSize(maxFrameSize);
    }

    // Not using the value greater than 2^31-1 because some HTTP/2 client implementations use a signed
    // 32-bit integer to represent an HTTP/2 SETTINGS parameter value.
    settings.maxConcurrentStreams(Math.min(config.http2MaxStreamsPerConnection(), Integer.MAX_VALUE));
    settings.maxHeaderListSize(config.http2MaxHeaderListSize());
    return settings;
}
 
Example 3
Source File: AbstractNettyHandler.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
AbstractNettyHandler(
    ChannelPromise channelUnused,
    Http2ConnectionDecoder decoder,
    Http2ConnectionEncoder encoder,
    Http2Settings initialSettings,
    boolean autoFlowControl) {
  super(channelUnused, decoder, encoder, initialSettings);

  // During a graceful shutdown, wait until all streams are closed.
  gracefulShutdownTimeoutMillis(GRACEFUL_SHUTDOWN_NO_TIMEOUT);

  // Extract the connection window from the settings if it was set.
  this.initialConnectionWindow = initialSettings.initialWindowSize() == null ? -1 :
          initialSettings.initialWindowSize();
  this.autoTuneFlowControlOn = autoFlowControl;
  if (encoder instanceof ListeningEncoder) {
    ((ListeningEncoder) encoder).setListener(pingCountingListener);
  }
}
 
Example 4
Source File: AbstractNettyHandler.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
public void updateWindow() throws Http2Exception {
  if (!autoTuneFlowControlOn) {
    return;
  }
  pingReturn++;
  long elapsedTime = (System.nanoTime() - lastPingTime);
  if (elapsedTime == 0) {
    elapsedTime = 1;
  }
  long bandwidth = (getDataSincePing() * TimeUnit.SECONDS.toNanos(1)) / elapsedTime;
  Http2LocalFlowController fc = decoder().flowController();
  // Calculate new window size by doubling the observed BDP, but cap at max window
  int targetWindow = Math.min(getDataSincePing() * 2, MAX_WINDOW_SIZE);
  setPinging(false);
  int currentWindow = fc.initialWindowSize(connection().connectionStream());
  if (targetWindow > currentWindow && bandwidth > lastBandwidth) {
    lastBandwidth = bandwidth;
    int increase = targetWindow - currentWindow;
    fc.incrementWindowSize(connection().connectionStream(), increase);
    fc.initialWindowSize(targetWindow);
    Http2Settings settings = new Http2Settings();
    settings.initialWindowSize(targetWindow);
    frameWriter().writeSettings(ctx(), settings, ctx().newPromise());
  }
}
 
Example 5
Source File: AbstractNettyHandler.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
AbstractNettyHandler(
    ChannelPromise channelUnused,
    Http2ConnectionDecoder decoder,
    Http2ConnectionEncoder encoder,
    Http2Settings initialSettings) {
  super(channelUnused, decoder, encoder, initialSettings);

  // During a graceful shutdown, wait until all streams are closed.
  gracefulShutdownTimeoutMillis(GRACEFUL_SHUTDOWN_NO_TIMEOUT);

  // Extract the connection window from the settings if it was set.
  this.initialConnectionWindow = initialSettings.initialWindowSize() == null ? -1 :
          initialSettings.initialWindowSize();
}
 
Example 6
Source File: Http2SettingsSpec.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
Http2SettingsSpec(Build build) {
	Http2Settings settings = build.http2Settings;
	headerTableSize = settings.headerTableSize();
	initialWindowSize = settings.initialWindowSize();
	maxConcurrentStreams = settings.maxConcurrentStreams();
	maxFrameSize = settings.maxFrameSize();
	maxHeaderListSize = settings.maxHeaderListSize();
	pushEnabled = settings.pushEnabled();
}
 
Example 7
Source File: HttpClientConfig.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
Http2Settings http2Settings() {
	Http2Settings settings = Http2Settings.defaultSettings();

	if (http2Settings != null) {
		Long headerTableSize = http2Settings.headerTableSize();
		if (headerTableSize != null) {
			settings.headerTableSize(headerTableSize);
		}

		Integer initialWindowSize = http2Settings.initialWindowSize();
		if (initialWindowSize != null) {
			settings.initialWindowSize(initialWindowSize);
		}

		Long maxConcurrentStreams = http2Settings.maxConcurrentStreams();
		if (maxConcurrentStreams != null) {
			settings.maxConcurrentStreams(maxConcurrentStreams);
		}

		Integer maxFrameSize = http2Settings.maxFrameSize();
		if (maxFrameSize != null) {
			settings.maxFrameSize(maxFrameSize);
		}

		settings.maxHeaderListSize(http2Settings.maxHeaderListSize());

		Boolean pushEnabled = http2Settings.pushEnabled();
		if (pushEnabled != null) {
			settings.pushEnabled(pushEnabled);
		}
	}

	return settings;
}
 
Example 8
Source File: HttpServerConfig.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
Http2Settings http2Settings() {
	Http2Settings settings = Http2Settings.defaultSettings();

	if (http2Settings != null) {
		Long headerTableSize = http2Settings.headerTableSize();
		if (headerTableSize != null) {
			settings.headerTableSize(headerTableSize);
		}

		Integer initialWindowSize = http2Settings.initialWindowSize();
		if (initialWindowSize != null) {
			settings.initialWindowSize(initialWindowSize);
		}

		Long maxConcurrentStreams = http2Settings.maxConcurrentStreams();
		if (maxConcurrentStreams != null) {
			settings.maxConcurrentStreams(maxConcurrentStreams);
		}

		Integer maxFrameSize = http2Settings.maxFrameSize();
		if (maxFrameSize != null) {
			settings.maxFrameSize(maxFrameSize);
		}

		settings.maxHeaderListSize(http2Settings.maxHeaderListSize());

		Boolean pushEnabled = http2Settings.pushEnabled();
		if (pushEnabled != null) {
			settings.pushEnabled(pushEnabled);
		}
	}

	return settings;
}
 
Example 9
Source File: HttpClientPipelineConfigurator.java    From armeria with Apache License 2.0 5 votes vote down vote up
private Http2Settings http2Settings() {
    final Http2Settings settings = new Http2Settings();
    final int initialWindowSize = clientFactory.http2InitialStreamWindowSize();
    if (initialWindowSize != DEFAULT_WINDOW_SIZE) {
        settings.initialWindowSize(initialWindowSize);
    }
    final int maxFrameSize = clientFactory.http2MaxFrameSize();
    if (maxFrameSize != DEFAULT_MAX_FRAME_SIZE) {
        settings.maxFrameSize(maxFrameSize);
    }
    settings.maxHeaderListSize(clientFactory.http2MaxHeaderListSize());
    return settings;
}
 
Example 10
Source File: NettyServerHandler.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
static NettyServerHandler newHandler(
    ChannelPromise channelUnused,
    Http2FrameReader frameReader,
    Http2FrameWriter frameWriter,
    ServerTransportListener transportListener,
    List<ServerStreamTracer.Factory> streamTracerFactories,
    TransportTracer transportTracer,
    int maxStreams,
    int flowControlWindow,
    int maxHeaderListSize,
    int maxMessageSize,
    long keepAliveTimeInNanos,
    long keepAliveTimeoutInNanos,
    long maxConnectionIdleInNanos,
    long maxConnectionAgeInNanos,
    long maxConnectionAgeGraceInNanos,
    boolean permitKeepAliveWithoutCalls,
    long permitKeepAliveTimeInNanos) {
  Preconditions.checkArgument(maxStreams > 0, "maxStreams must be positive");
  Preconditions.checkArgument(flowControlWindow > 0, "flowControlWindow must be positive");
  Preconditions.checkArgument(maxHeaderListSize > 0, "maxHeaderListSize must be positive");
  Preconditions.checkArgument(maxMessageSize > 0, "maxMessageSize must be positive");

  final Http2Connection connection = new DefaultHttp2Connection(true);
  WeightedFairQueueByteDistributor dist = new WeightedFairQueueByteDistributor(connection);
  dist.allocationQuantum(16 * 1024); // Make benchmarks fast again.
  DefaultHttp2RemoteFlowController controller =
      new DefaultHttp2RemoteFlowController(connection, dist);
  connection.remote().flowController(controller);
  final KeepAliveEnforcer keepAliveEnforcer = new KeepAliveEnforcer(
      permitKeepAliveWithoutCalls, permitKeepAliveTimeInNanos, TimeUnit.NANOSECONDS);

  // Create the local flow controller configured to auto-refill the connection window.
  connection.local().flowController(
      new DefaultHttp2LocalFlowController(connection, DEFAULT_WINDOW_UPDATE_RATIO, true));
  frameWriter = new WriteMonitoringFrameWriter(frameWriter, keepAliveEnforcer);
  Http2ConnectionEncoder encoder = new DefaultHttp2ConnectionEncoder(connection, frameWriter);
  Http2ConnectionDecoder decoder = new DefaultHttp2ConnectionDecoder(connection, encoder,
      frameReader);

  Http2Settings settings = new Http2Settings();
  settings.initialWindowSize(flowControlWindow);
  settings.maxConcurrentStreams(maxStreams);
  settings.maxHeaderListSize(maxHeaderListSize);

  return new NettyServerHandler(
      channelUnused,
      connection,
      transportListener,
      streamTracerFactories,
      transportTracer,
      decoder, encoder, settings,
      maxMessageSize,
      keepAliveTimeInNanos, keepAliveTimeoutInNanos,
      maxConnectionIdleInNanos,
      maxConnectionAgeInNanos, maxConnectionAgeGraceInNanos,
      keepAliveEnforcer);
}
 
Example 11
Source File: NettyServerHandler.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
static NettyServerHandler newHandler(
    ChannelPromise channelUnused,
    Http2FrameReader frameReader,
    Http2FrameWriter frameWriter,
    ServerTransportListener transportListener,
    List<? extends ServerStreamTracer.Factory> streamTracerFactories,
    TransportTracer transportTracer,
    int maxStreams,
    boolean autoFlowControl,
    int flowControlWindow,
    int maxHeaderListSize,
    int maxMessageSize,
    long keepAliveTimeInNanos,
    long keepAliveTimeoutInNanos,
    long maxConnectionIdleInNanos,
    long maxConnectionAgeInNanos,
    long maxConnectionAgeGraceInNanos,
    boolean permitKeepAliveWithoutCalls,
    long permitKeepAliveTimeInNanos) {
  Preconditions.checkArgument(maxStreams > 0, "maxStreams must be positive: %s", maxStreams);
  Preconditions.checkArgument(flowControlWindow > 0, "flowControlWindow must be positive: %s",
      flowControlWindow);
  Preconditions.checkArgument(maxHeaderListSize > 0, "maxHeaderListSize must be positive: %s",
      maxHeaderListSize);
  Preconditions.checkArgument(maxMessageSize > 0, "maxMessageSize must be positive: %s",
      maxMessageSize);

  final Http2Connection connection = new DefaultHttp2Connection(true);
  WeightedFairQueueByteDistributor dist = new WeightedFairQueueByteDistributor(connection);
  dist.allocationQuantum(16 * 1024); // Make benchmarks fast again.
  DefaultHttp2RemoteFlowController controller =
      new DefaultHttp2RemoteFlowController(connection, dist);
  connection.remote().flowController(controller);
  final KeepAliveEnforcer keepAliveEnforcer = new KeepAliveEnforcer(
      permitKeepAliveWithoutCalls, permitKeepAliveTimeInNanos, TimeUnit.NANOSECONDS);

  // Create the local flow controller configured to auto-refill the connection window.
  connection.local().flowController(
      new DefaultHttp2LocalFlowController(connection, DEFAULT_WINDOW_UPDATE_RATIO, true));
  frameWriter = new WriteMonitoringFrameWriter(frameWriter, keepAliveEnforcer);
  Http2ConnectionEncoder encoder =
      new ListeningDefaultHttp2ConnectionEncoder(connection, frameWriter);
  encoder = new Http2ControlFrameLimitEncoder(encoder, 10000);
  Http2ConnectionDecoder decoder = new DefaultHttp2ConnectionDecoder(connection, encoder,
      frameReader);

  Http2Settings settings = new Http2Settings();
  settings.initialWindowSize(flowControlWindow);
  settings.maxConcurrentStreams(maxStreams);
  settings.maxHeaderListSize(maxHeaderListSize);

  return new NettyServerHandler(
      channelUnused,
      connection,
      transportListener,
      streamTracerFactories,
      transportTracer,
      decoder, encoder, settings,
      maxMessageSize,
      keepAliveTimeInNanos, keepAliveTimeoutInNanos,
      maxConnectionIdleInNanos,
      maxConnectionAgeInNanos, maxConnectionAgeGraceInNanos,
      keepAliveEnforcer,
      autoFlowControl);
}