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
This commit is contained in:
Terry Wilson 2022-09-15 09:35:23 -07:00 committed by GitHub
parent 341fea8996
commit 79c4c355ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 0 deletions

View File

@ -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;