1 /* 2 * Copyright 2016 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 com.google.common.base.Preconditions; 20 21 /** 22 * Allows autoFlowControl to be turned on and off from interop testing and flow control windows to 23 * be accessed. 24 */ 25 final class NettyHandlerSettings { 26 27 private static volatile boolean enabled; 28 29 private static boolean autoFlowControlOn; 30 // These will be the most recently created handlers created using NettyClientTransport and 31 // NettyServerTransport 32 private static AbstractNettyHandler clientHandler; 33 private static AbstractNettyHandler serverHandler; 34 setAutoWindow(AbstractNettyHandler handler)35 static void setAutoWindow(AbstractNettyHandler handler) { 36 if (!enabled) { 37 return; 38 } 39 synchronized (NettyHandlerSettings.class) { 40 handler.setAutoTuneFlowControl(autoFlowControlOn); 41 if (handler instanceof NettyClientHandler) { 42 clientHandler = handler; 43 } else if (handler instanceof NettyServerHandler) { 44 serverHandler = handler; 45 } else { 46 throw new RuntimeException("Expecting NettyClientHandler or NettyServerHandler"); 47 } 48 } 49 } 50 enable(boolean enable)51 public static void enable(boolean enable) { 52 enabled = enable; 53 } 54 autoWindowOn(boolean autoFlowControl)55 public static synchronized void autoWindowOn(boolean autoFlowControl) { 56 autoFlowControlOn = autoFlowControl; 57 } 58 getLatestClientWindow()59 public static synchronized int getLatestClientWindow() { 60 return getLatestWindow(clientHandler); 61 } 62 getLatestServerWindow()63 public static synchronized int getLatestServerWindow() { 64 return getLatestWindow(serverHandler); 65 } 66 getLatestWindow(AbstractNettyHandler handler)67 private static synchronized int getLatestWindow(AbstractNettyHandler handler) { 68 Preconditions.checkNotNull(handler); 69 return handler.decoder().flowController() 70 .initialWindowSize(handler.connection().connectionStream()); 71 } 72 } 73