@Test public void setSoLingerChannelOption() throws IOException { startServer(); Map<ChannelOption<?>, Object> channelOptions = new HashMap<ChannelOption<?>, Object>(); // set SO_LINGER option int soLinger = 123; channelOptions.put(ChannelOption.SO_LINGER, soLinger); NettyClientTransport transport = new NettyClientTransport( address, NioSocketChannel.class, channelOptions, group, newNegotiator(), DEFAULT_WINDOW_SIZE, DEFAULT_MAX_MESSAGE_SIZE, GrpcUtil.DEFAULT_MAX_HEADER_LIST_SIZE, KEEPALIVE_TIME_NANOS_DISABLED, 1L, false, authority, null /* user agent */, tooManyPingsRunnable, new TransportTracer()); transports.add(transport); callMeMaybe(transport.start(clientTransportListener)); // verify SO_LINGER has been set ChannelConfig config = transport.channel().config(); assertTrue(config instanceof SocketChannelConfig); assertEquals(soLinger, ((SocketChannelConfig) config).getSoLinger()); }
private void setBufferSizeIfConfigIsSocketChannelConfig( ChannelConfig config, long contentLength) { if (config instanceof SocketChannelConfig) { int sendBufferSize = contentLength < m_maxSendBufferSize ? (int) contentLength : m_maxSendBufferSize; ((SocketChannelConfig) config).setSendBufferSize(sendBufferSize); } }
@Override public void channelActive(final ChannelHandlerContext ctx) throws Exception { final SocketChannelConfig config = (SocketChannelConfig) ctx.channel().config(); // Disable Nagle's algorithm config.setTcpNoDelay(true); // Setup TCP keepalive config.setKeepAlive(true); super.channelActive(ctx); // Our work is done ctx.channel().pipeline().remove(this); }
@Test public void testFilterRequest() throws IOException { AppConfiguration appConfig = new AppConfiguration(new ConfigLoader(), null); appConfig.init(); PolicyManager policyManager = mock(PolicyManager.class); NettyRequestProxyFilter filter = new NettyRequestProxyFilter( policyManager, appConfig); ChannelHandlerContext ctx = mock(ChannelHandlerContext.class); when(ctx.attr(any(AttributeKey.class))).thenReturn( mock(Attribute.class)); assertNull(filter.filterRequest(mock(HttpRequest.class), ctx)); DefaultFullHttpRequest req = new DefaultFullHttpRequest( HttpVersion.HTTP_1_1, HttpMethod.GET, "http://test.ebay.com/s/"); when(policyManager.cacheIsNeededFor(any(CacheDecisionObject.class))) .thenReturn(false); assertNull(filter.filterRequest(req, ctx)); when(policyManager.cacheIsNeededFor(any(CacheDecisionObject.class))) .thenReturn(true); CacheManager cm = mock(CacheManager.class); when(policyManager.getCacheManager()).thenReturn(cm); assertNull(filter.filterRequest(req, ctx)); FullHttpResponse resp = mock(FullHttpResponse.class); HttpHeaders respHeaders = mock(HttpHeaders.class); when(resp.headers()).thenReturn(respHeaders); when(respHeaders.get(any(CharSequence.class))).thenReturn("100"); when(cm.get(anyString())).thenReturn(resp); Channel channel = mock(Channel.class); SocketChannelConfig config = mock(SocketChannelConfig.class); when(channel.config()).thenReturn(config); when(ctx.channel()).thenReturn(channel); req.headers().add("h1", "v1"); when(resp.content()).thenReturn( new EmptyByteBuf(new PooledByteBufAllocator())).thenReturn( Unpooled.copiedBuffer("Hello".getBytes())); assertEquals(resp, filter.filterRequest(req, ctx)); assertEquals(resp, filter.filterRequest(req, ctx)); }
/** * Template method for changing properties on the given {@link SocketChannelConfig}. * <p>The default implementation sets the connect timeout based on the set property. * @param config the channel configuration */ protected void configureChannel(SocketChannelConfig config) { if (this.connectTimeout >= 0) { config.setConnectTimeoutMillis(this.connectTimeout); } }
private void configure(SocketChannelConfig channelConfiguration) { channelConfiguration.setConnectTimeoutMillis(nettyHttpClientRequestConfiguration.connectionTimeout()); }
@Override public SocketChannelConfig config() { return config; }
@Override public SocketChannelConfig setTcpNoDelay(boolean tcpNoDelay) { channel.setOption(Options.TCP_NODELAY, tcpNoDelay); return this; }
@Override public SocketChannelConfig setSoLinger(int soLinger) { throw new UnsupportedOperationException(); }
@Override public SocketChannelConfig setSendBufferSize(int sendBufferSize) { channel.setOption(Options.SEND_BUFFER, sendBufferSize); return this; }
@Override public SocketChannelConfig setReceiveBufferSize(int receiveBufferSize) { channel.setOption(Options.RECEIVE_BUFFER, receiveBufferSize); return this; }
@Override public SocketChannelConfig setKeepAlive(boolean keepAlive) { channel.setOption(Options.KEEP_ALIVE, keepAlive); return this; }
@Override public SocketChannelConfig setTrafficClass(int trafficClass) { channel.setOption(Options.IP_TRAFFIC_CLASS, trafficClass); return this; }
@Override public SocketChannelConfig setReuseAddress(boolean reuseAddress) { channel.setOption(Options.REUSE_ADDRESSES, reuseAddress); return this; }
@Override public SocketChannelConfig setPerformancePreferences(int connectionTime, int latency, int bandwidth) { throw new UnsupportedOperationException(); }
@Override public SocketChannelConfig setAllowHalfClosure(boolean allowHalfClosure) { throw new UnsupportedOperationException(); }
@Override public SocketChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) { super.setConnectTimeoutMillis(connectTimeoutMillis); return this; }
@Override public SocketChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) { super.setMaxMessagesPerRead(maxMessagesPerRead); return this; }
@Override public SocketChannelConfig setWriteSpinCount(int writeSpinCount) { super.setWriteSpinCount(writeSpinCount); return this; }
@Override public SocketChannelConfig setAllocator(ByteBufAllocator allocator) { super.setAllocator(allocator); return this; }
@Override public SocketChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) { super.setRecvByteBufAllocator(allocator); return this; }
@Override public SocketChannelConfig setAutoRead(boolean autoRead) { super.setAutoRead(autoRead); return this; }
@Override public SocketChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) { super.setWriteBufferHighWaterMark(writeBufferHighWaterMark); return this; }
@Override public SocketChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) { super.setMessageSizeEstimator(estimator); return this; }
@Override public SocketChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) { super.setWriteBufferLowWaterMark(writeBufferLowWaterMark); return this; }
@Override public SocketChannelConfig setAutoClose(boolean autoClose) { super.setAutoClose(autoClose); return this; }