From 79c4c355ba47c3bb5e87e64c3eb32b7ee70e105c Mon Sep 17 00:00:00 2001 From: Terry Wilson Date: Thu, 15 Sep 2022 09:35:23 -0700 Subject: [PATCH] okhttp: Fair treatment when writing out streams (#9545) When allocating bytes to streams within a flow control window we always go through the streams in the same order. This can lead to large streams hogging all the bytes and a smaller one down the list getting starved out. This change shuffles the stream array to lower the chance of this happening. Fixes #9089 --- .../src/main/java/io/grpc/okhttp/OutboundFlowController.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/okhttp/src/main/java/io/grpc/okhttp/OutboundFlowController.java b/okhttp/src/main/java/io/grpc/okhttp/OutboundFlowController.java index 35117c1b22..2c959ee076 100644 --- a/okhttp/src/main/java/io/grpc/okhttp/OutboundFlowController.java +++ b/okhttp/src/main/java/io/grpc/okhttp/OutboundFlowController.java @@ -25,6 +25,8 @@ import static java.lang.Math.min; import com.google.common.base.Preconditions; import io.grpc.okhttp.internal.framed.FrameWriter; import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; import javax.annotation.Nullable; import okio.Buffer; @@ -152,6 +154,7 @@ class OutboundFlowController { */ public void writeStreams() { StreamState[] states = transport.getActiveStreams(); + Collections.shuffle(Arrays.asList(states)); int connectionWindow = connectionState.window(); for (int numStreams = states.length; numStreams > 0 && connectionWindow > 0;) { int nextNumStreams = 0;