diff options
author | zpencer <spencerfang@google.com> | 2018-03-30 08:48:37 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-30 08:48:37 -0700 |
commit | 7c37f1d32b9dc11027ff1ba946303f5a11dbfe00 (patch) | |
tree | 8323666a75669ee53e6740a78069539e309beda1 /okhttp | |
parent | 2a95e3825753a455810a93890c36348e7815b4f9 (diff) | |
download | grpc-grpc-java-7c37f1d32b9dc11027ff1ba946303f5a11dbfe00.tar.gz |
core,netty,okhttp,services: expose socket options to channelz (#4228)
For okhttp, expose the standard options from the Socket object.
For netty, expose all the `io.netty.channel.ChannelOption`s of the
`channel.config()`.
Diffstat (limited to 'okhttp')
-rw-r--r-- | okhttp/src/main/java/io/grpc/okhttp/OkHttpClientTransport.java | 1 | ||||
-rw-r--r-- | okhttp/src/main/java/io/grpc/okhttp/Utils.java | 79 | ||||
-rw-r--r-- | okhttp/src/test/java/io/grpc/okhttp/UtilsTest.java | 27 |
3 files changed, 107 insertions, 0 deletions
diff --git a/okhttp/src/main/java/io/grpc/okhttp/OkHttpClientTransport.java b/okhttp/src/main/java/io/grpc/okhttp/OkHttpClientTransport.java index f710399c8..feab8311f 100644 --- a/okhttp/src/main/java/io/grpc/okhttp/OkHttpClientTransport.java +++ b/okhttp/src/main/java/io/grpc/okhttp/OkHttpClientTransport.java @@ -911,6 +911,7 @@ class OkHttpClientTransport implements ConnectionClientTransport { transportTracer.getStats(), socket.getLocalSocketAddress(), socket.getRemoteSocketAddress(), + Utils.getSocketOptions(socket), new Security())); return ret; } diff --git a/okhttp/src/main/java/io/grpc/okhttp/Utils.java b/okhttp/src/main/java/io/grpc/okhttp/Utils.java index 6ab5d9a01..89c326f9f 100644 --- a/okhttp/src/main/java/io/grpc/okhttp/Utils.java +++ b/okhttp/src/main/java/io/grpc/okhttp/Utils.java @@ -19,16 +19,23 @@ package io.grpc.okhttp; import com.google.common.base.Preconditions; import io.grpc.InternalMetadata; import io.grpc.Metadata; +import io.grpc.internal.Channelz; import io.grpc.internal.TransportFrameUtil; import io.grpc.okhttp.internal.CipherSuite; import io.grpc.okhttp.internal.ConnectionSpec; import io.grpc.okhttp.internal.framed.Header; +import java.net.Socket; +import java.net.SocketException; import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; /** * Common utility methods for OkHttp transport. */ class Utils { + private static final Logger log = Logger.getLogger(Utils.class.getName()); + static final int DEFAULT_WINDOW_SIZE = 65535; static final int CONNECTION_STREAM_ID = 0; @@ -79,6 +86,78 @@ class Utils { .build(); } + /** + * Attempts to capture all known socket options and return the results as a + * {@link Channelz.SocketOptions}. If getting a socket option threw an exception, + * log the error to the logger and report the value as an error in the response. + */ + static Channelz.SocketOptions getSocketOptions(Socket socket) { + Channelz.SocketOptions.Builder builder = new Channelz.SocketOptions.Builder(); + try { + builder.setSocketOptionLingerSeconds(socket.getSoLinger()); + } catch (SocketException e) { + log.log(Level.SEVERE, "Exception caught while reading socket option", e); + builder.addOption("SO_LINGER", "channelz_internal_error"); + } + + try { + builder.setSocketOptionTimeoutMillis(socket.getSoTimeout()); + } catch (Exception e) { + log.log(Level.SEVERE, "Exception caught while reading socket option", e); + builder.addOption("SO_TIMEOUT", "channelz_internal_error"); + } + + try { + builder.addOption("TCP_NODELAY", socket.getTcpNoDelay()); + } catch (SocketException e) { + log.log(Level.SEVERE, "Exception caught while reading socket option", e); + builder.addOption("TCP_NODELAY", "channelz_internal_error"); + } + + try { + builder.addOption("SO_REUSEADDR", socket.getReuseAddress()); + } catch (SocketException e) { + log.log(Level.SEVERE, "Exception caught while reading socket option", e); + builder.addOption("SO_REUSEADDR", "channelz_internal_error"); + } + + try { + builder.addOption("SO_SNDBUF", socket.getSendBufferSize()); + } catch (SocketException e) { + log.log(Level.SEVERE, "Exception caught while reading socket option", e); + builder.addOption("SO_SNDBUF", "channelz_internal_error"); + } + + try { + builder.addOption("SO_RECVBUF", socket.getReceiveBufferSize()); + } catch (SocketException e) { + log.log(Level.SEVERE, "Exception caught while reading socket option", e); + builder.addOption("SO_RECVBUF", "channelz_internal_error"); + } + + try { + builder.addOption("SO_KEEPALIVE", socket.getKeepAlive()); + } catch (SocketException e) { + log.log(Level.SEVERE, "Exception caught while reading socket option", e); + builder.addOption("SO_KEEPALIVE", "channelz_internal_error"); + } + + try { + builder.addOption("SO_OOBINLINE", socket.getOOBInline()); + } catch (SocketException e) { + log.log(Level.SEVERE, "Exception caught while reading socket option", e); + builder.addOption("SO_OOBINLINE", "channelz_internal_error"); + } + + try { + builder.addOption("IP_TOS", socket.getTrafficClass()); + } catch (SocketException e) { + log.log(Level.SEVERE, "Exception caught while reading socket option", e); + builder.addOption("IP_TOS", "channelz_internal_error"); + } + return builder.build(); + } + private Utils() { // Prevents instantiation } diff --git a/okhttp/src/test/java/io/grpc/okhttp/UtilsTest.java b/okhttp/src/test/java/io/grpc/okhttp/UtilsTest.java index 35ca1ae68..157911f25 100644 --- a/okhttp/src/test/java/io/grpc/okhttp/UtilsTest.java +++ b/okhttp/src/test/java/io/grpc/okhttp/UtilsTest.java @@ -19,9 +19,11 @@ package io.grpc.okhttp; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import io.grpc.internal.Channelz.SocketOptions; import io.grpc.okhttp.internal.CipherSuite; import io.grpc.okhttp.internal.ConnectionSpec; import io.grpc.okhttp.internal.TlsVersion; +import java.net.Socket; import java.util.List; import org.junit.Rule; import org.junit.Test; @@ -69,4 +71,29 @@ public class UtilsTest { assertEquals(CipherSuite.forJavaName(squareCipherSuites.get(i).name()), cipherSuites.get(i)); } } + + @Test + public void getSocketOptions() throws Exception { + Socket socket = new Socket(); + socket.setSoLinger(true, 2); + socket.setSoTimeout(3); + socket.setTcpNoDelay(true); + socket.setReuseAddress(true); + socket.setReceiveBufferSize(4000); + socket.setSendBufferSize(5000); + socket.setKeepAlive(true); + socket.setOOBInline(true); + socket.setTrafficClass(8); // note: see javadoc for valid input values + + SocketOptions socketOptions = Utils.getSocketOptions(socket); + assertEquals(2, (int) socketOptions.lingerSeconds); + assertEquals(3, (int) socketOptions.soTimeoutMillis); + assertEquals("true", socketOptions.others.get("TCP_NODELAY")); + assertEquals("true", socketOptions.others.get("SO_REUSEADDR")); + assertEquals("4000", socketOptions.others.get("SO_RECVBUF")); + assertEquals("5000", socketOptions.others.get("SO_SNDBUF")); + assertEquals("true", socketOptions.others.get("SO_KEEPALIVE")); + assertEquals("true", socketOptions.others.get("SO_OOBINLINE")); + assertEquals("8", socketOptions.others.get("IP_TOS")); + } } |