You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2021/01/08 16:43:29 UTC
[httpcomponents-core] branch master updated: HTTPCORE-657: Add
TrafficClass to IOReactorConfig (#243)
This is an automated email from the ASF dual-hosted git repository.
olegk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git
The following commit(s) were added to refs/heads/master by this push:
new 9d7803f HTTPCORE-657: Add TrafficClass to IOReactorConfig (#243)
9d7803f is described below
commit 9d7803fe905da0490304adca98fd4af4d9247df9
Author: Desmond Yeung <dy...@datto.com>
AuthorDate: Fri Jan 8 11:43:18 2021 -0500
HTTPCORE-657: Add TrafficClass to IOReactorConfig (#243)
---
.../apache/hc/core5/reactor/IOReactorConfig.java | 32 ++++++++++
.../hc/core5/reactor/SingleCoreIOReactor.java | 3 +
.../hc/core5/reactor/IOReactorConfigTest.java | 71 ++++++++++++++++++++++
3 files changed, 106 insertions(+)
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOReactorConfig.java b/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOReactorConfig.java
index 73cfca8..be49929 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOReactorConfig.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOReactorConfig.java
@@ -53,6 +53,7 @@ public final class IOReactorConfig {
private final TimeValue soLinger;
private final boolean soKeepAlive;
private final boolean tcpNoDelay;
+ private final int trafficClass;
private final int sndBufSize;
private final int rcvBufSize;
private final int backlogSize;
@@ -68,6 +69,7 @@ public final class IOReactorConfig {
final TimeValue soLinger,
final boolean soKeepAlive,
final boolean tcpNoDelay,
+ final int trafficClass,
final int sndBufSize,
final int rcvBufSize,
final int backlogSize,
@@ -82,6 +84,7 @@ public final class IOReactorConfig {
this.soLinger = soLinger;
this.soKeepAlive = soKeepAlive;
this.tcpNoDelay = tcpNoDelay;
+ this.trafficClass = trafficClass;
this.sndBufSize = sndBufSize;
this.rcvBufSize = rcvBufSize;
this.backlogSize = backlogSize;
@@ -140,6 +143,15 @@ public final class IOReactorConfig {
}
/**
+ * @see Builder#setTrafficClass(int)
+ *
+ * @since 5.1
+ */
+ public int getTrafficClass() {
+ return trafficClass;
+ }
+
+ /**
* @see Builder#setSndBufSize(int)
*/
public int getSndBufSize() {
@@ -239,6 +251,7 @@ public final class IOReactorConfig {
private TimeValue soLinger;
private boolean soKeepAlive;
private boolean tcpNoDelay;
+ private int trafficClass;
private int sndBufSize;
private int rcvBufSize;
private int backlogSize;
@@ -254,6 +267,7 @@ public final class IOReactorConfig {
this.soLinger = TimeValue.NEG_ONE_SECOND;
this.soKeepAlive = false;
this.tcpNoDelay = true;
+ this.trafficClass = 0;
this.sndBufSize = 0;
this.rcvBufSize = 0;
this.backlogSize = 0;
@@ -382,6 +396,22 @@ public final class IOReactorConfig {
}
/**
+ * Determines the default value of the {@link java.net.SocketOptions#IP_TOS} parameter
+ * for newly created sockets.
+ * <p>
+ * Default: {@code 0}
+ * </p>
+ *
+ * @see java.net.SocketOptions#IP_TOS
+ *
+ * @since 5.1
+ */
+ public Builder setTrafficClass(final int trafficClass) {
+ this.trafficClass = trafficClass;
+ return this;
+ }
+
+ /**
* Determines the default value of the {@link java.net.SocketOptions#SO_SNDBUF} parameter
* for newly created sockets.
* <p>
@@ -455,6 +485,7 @@ public final class IOReactorConfig {
TimeValue.defaultsToNegativeOneMillisecond(soLinger),
soKeepAlive,
tcpNoDelay,
+ trafficClass,
sndBufSize, rcvBufSize, backlogSize,
socksProxyAddress, socksProxyUsername, socksProxyPassword);
}
@@ -471,6 +502,7 @@ public final class IOReactorConfig {
.append(", soLinger=").append(this.soLinger)
.append(", soKeepAlive=").append(this.soKeepAlive)
.append(", tcpNoDelay=").append(this.tcpNoDelay)
+ .append(", trafficClass=").append(this.trafficClass)
.append(", sndBufSize=").append(this.sndBufSize)
.append(", rcvBufSize=").append(this.rcvBufSize)
.append(", backlogSize=").append(this.backlogSize)
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/reactor/SingleCoreIOReactor.java b/httpcore5/src/main/java/org/apache/hc/core5/reactor/SingleCoreIOReactor.java
index c1a99b4..5d731a3 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/reactor/SingleCoreIOReactor.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/reactor/SingleCoreIOReactor.java
@@ -274,6 +274,9 @@ class SingleCoreIOReactor extends AbstractSingleCoreIOReactor implements Connect
if (this.reactorConfig.getRcvBufSize() > 0) {
socket.setReceiveBufferSize(this.reactorConfig.getRcvBufSize());
}
+ if (this.reactorConfig.getTrafficClass() > 0) {
+ socket.setTrafficClass(this.reactorConfig.getTrafficClass());
+ }
final int linger = this.reactorConfig.getSoLinger().toSecondsIntBound();
if (linger >= 0) {
socket.setSoLinger(true, linger);
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/reactor/IOReactorConfigTest.java b/httpcore5/src/test/java/org/apache/hc/core5/reactor/IOReactorConfigTest.java
new file mode 100644
index 0000000..03f398a
--- /dev/null
+++ b/httpcore5/src/test/java/org/apache/hc/core5/reactor/IOReactorConfigTest.java
@@ -0,0 +1,71 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.core5.reactor;
+
+import java.net.InetSocketAddress;
+
+import org.apache.hc.core5.util.TimeValue;
+import org.apache.hc.core5.util.Timeout;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class IOReactorConfigTest {
+ @Test
+ public void testCustomIOReactorConfig() throws Exception {
+ final IOReactorConfig reactorConfig = IOReactorConfig.custom()
+ .setSelectInterval(TimeValue.ofMilliseconds(500))
+ .setIoThreadCount(2)
+ .setSoTimeout(Timeout.ofSeconds(10))
+ .setSoReuseAddress(true)
+ .setSoLinger(TimeValue.ofSeconds(30))
+ .setSoKeepAlive(true)
+ .setTcpNoDelay(false)
+ .setTrafficClass(0x02)
+ .setSndBufSize(32767)
+ .setRcvBufSize(8192)
+ .setBacklogSize(5)
+ .setSocksProxyAddress(new InetSocketAddress(8888))
+ .setSocksProxyUsername("socksProxyUsername")
+ .setSocksProxyPassword("socksProxyPassword")
+ .build();
+
+ Assert.assertEquals(TimeValue.ofMilliseconds(500), reactorConfig.getSelectInterval());
+ Assert.assertEquals(2, reactorConfig.getIoThreadCount());
+ Assert.assertEquals(Timeout.ofSeconds(10), reactorConfig.getSoTimeout());
+ Assert.assertEquals(true, reactorConfig.isSoReuseAddress());
+ Assert.assertEquals(TimeValue.ofSeconds(30), reactorConfig.getSoLinger());
+ Assert.assertEquals(true, reactorConfig.isSoKeepalive());
+ Assert.assertEquals(false, reactorConfig.isTcpNoDelay());
+ Assert.assertEquals(0x02, reactorConfig.getTrafficClass());
+ Assert.assertEquals(32767, reactorConfig.getSndBufSize());
+ Assert.assertEquals(8192, reactorConfig.getRcvBufSize());
+ Assert.assertEquals(5, reactorConfig.getBacklogSize());
+ Assert.assertEquals(new InetSocketAddress(8888), reactorConfig.getSocksProxyAddress());
+ Assert.assertEquals("socksProxyUsername", reactorConfig.getSocksProxyUsername());
+ Assert.assertEquals("socksProxyPassword", reactorConfig.getSocksProxyPassword());
+ }
+}