pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Scott Harrington <scotth01@sns-usa.com>
To: pgsql-jdbc@postgresql.org
Subject: Re: send/receive buffer size
Date: Tue, 21 Aug 2012 19:08:05 -0400 (Eastern Daylight Time)
Message-ID: <alpine.WNT.2.00.1208211856150.1212@turks> (raw)
In-Reply-To: <CC596D71.107BB%skong@investoranalytics.com>
References: <CC596D71.107BB%skong@investoranalytics.com>

On Tue, 21 Aug 2012, Shijun Kong wrote:

> I read a thread somewhere that somebody contributed a patch for adding support of configurable send/receive buffer size. Did it ever commit into
> master branch? I did not see any commit message about this change for last 12 months.

I wondered the same last weekend and found that the patch had NOT yet 
found its way into pgjdbc on github. In case it helps you, I've attached 
that old patch in a form that should apply cleanly against the latest 
sources (unless something's changed in the last 10 days).

Before merging, maintainers would probably want to rename the property 
names (which have underscores in this patch) to better conform to the 
other driver properties which use camelcaps for the most part.

I have also attached a "localAddress" patch that I find useful (I think I 
posted it myself a while ago, but in any case here's an updated patch that 
should apply more cleanly against recent sources).

-Scott

Attachments: download all (2 files, .tar.gz)

pgjdbc-bufferSizes.patch (application/octet-stream, 5.4K) download diff '--exclude=.svn' -ur org/postgresql/core/v3/ConnectionFactoryImpl.java ../x/org/postgresql/core/v3/ConnectionFactoryImpl.java --- org/postgresql/core/v3/ConnectionFactoryImpl.java 2012-08-21 18:47:09.930742600 -0400 +++ ../x/org/postgresql/core/v3/ConnectionFactoryImpl.java 2012-08-13 11:34:25.267000000 -0400 @@ -118,6 +118,45 @@ // Enable TCP keep-alive probe if required. newStream.getSocket().setKeepAlive(requireTCPKeepAlive); + // Try to set SO_SNDBUF and SO_RECVBUF socket options, if requested. + // If receive_buffer_size and send_buffer_size are set to a value greater + // than 0, adjust. -1 means use the system default, 0 is ignored since not + // supported. + + // Set SO_RECVBUF read buffer size + String receiveBufferSizeProperty = info.getProperty("receive_buffer_size", "-1"); + try { + int receiveBufferSize = Integer.parseInt(receiveBufferSizeProperty); + if (receiveBufferSize > -1) { + // value of 0 not a valid buffer size value + if (receiveBufferSize > 0) { + newStream.getSocket().setReceiveBufferSize(receiveBufferSize); + } else { + logger.info("Ignore invalid value for receive_buffer_size: " + receiveBufferSize); + } + } + } catch (NumberFormatException nfe) { + logger.info("Couldn't parse receive_buffer_size value: " + receiveBufferSizeProperty); + } + + // Set SO_SNDBUF write buffer size + String sendBufferSizeProperty = info.getProperty("send_buffer_size", "-1"); + try { + int sendBufferSize = Integer.parseInt(sendBufferSizeProperty); + if (sendBufferSize > -1) { + if (sendBufferSize > 0) { + newStream.getSocket().setSendBufferSize(sendBufferSize); + } else { + logger.info("Ignore invalid value for send_buffer_size: " + sendBufferSize); + } + } + } catch (NumberFormatException nfe) { + logger.info("Couldn't parse send_buffer_size value: " + sendBufferSizeProperty); + } + + logger.info("Receive Buffer Size is " + newStream.getSocket().getReceiveBufferSize()); + logger.info("Send Buffer Size is " + newStream.getSocket().getSendBufferSize()); + // Construct and send a startup packet. String[][] params = { { "user", user }, diff '--exclude=.svn' -ur org/postgresql/ds/common/BaseDataSource.java ../x/org/postgresql/ds/common/BaseDataSource.java --- org/postgresql/ds/common/BaseDataSource.java 2012-08-21 18:47:12.982742600 -0400 +++ ../x/org/postgresql/ds/common/BaseDataSource.java 2012-08-13 11:34:28.971000000 -0400 @@ -54,6 +54,8 @@ private String binaryTransferDisable = ""; private int loginTimeout = 0; // in seconds private int socketTimeout = 0; // in seconds + private int receive_buffer_size = -1; // off (-1), not in use + private int send_buffer_size = -1; // off (-1), not in use private boolean ssl = false; private String sslfactory; private boolean tcpKeepAlive = false; @@ -293,6 +295,22 @@ } /** + * Sets the write buffer size of TCP/IP socket. + */ + public void setReceiveBufferSize(int nbytes) + { + this.receive_buffer_size = nbytes; + } + + /** + * Sets the send buffer size of TCP/IP socket. + */ + public void setSendBufferSize(int nbytes) + { + this.send_buffer_size = nbytes; + } + + /** * Gets the default threshold for enabling server-side prepare. * * @see #setPrepareThreshold(int) @@ -478,6 +496,12 @@ sb.append("&sslfactory=").append(sslfactory); } } + if (receive_buffer_size != -1) { + sb.append("&receive_buffer_size=").append(receive_buffer_size); + } + if (send_buffer_size != -1) { + sb.append("&send_buffer_size=").append(send_buffer_size); + } sb.append("&tcpkeepalive=").append(tcpKeepAlive); if (compatible != null) { sb.append("&compatible="+compatible); @@ -530,6 +554,8 @@ ref.add(new StringRefAddr("ssl", Boolean.toString(ssl))); ref.add(new StringRefAddr("sslfactory", sslfactory)); + ref.add(new StringRefAddr("receive_buffer_size", Integer.toString(receive_buffer_size))); + ref.add(new StringRefAddr("send_buffer_size", Integer.toString(send_buffer_size))); ref.add(new StringRefAddr("tcpKeepAlive", Boolean.toString(tcpKeepAlive))); if (compatible != null) { @@ -556,6 +582,8 @@ out.writeInt(socketTimeout); out.writeBoolean(ssl); out.writeObject(sslfactory); + out.writeInt(receive_buffer_size); + out.writeInt(send_buffer_size); out.writeBoolean(tcpKeepAlive); out.writeObject(compatible); out.writeInt(logLevel); @@ -579,6 +607,8 @@ socketTimeout = in.readInt(); ssl = in.readBoolean(); sslfactory = (String)in.readObject(); + receive_buffer_size = in.readInt(); + send_buffer_size = in.readInt(); tcpKeepAlive = in.readBoolean(); compatible = (String)in.readObject(); logLevel = in.readInt();
pgjdbc-localAddress.patch (application/octet-stream, 5.6K) download diff '--exclude=.svn' -ur org/postgresql/core/PGStream.java ../x/org/postgresql/core/PGStream.java --- org/postgresql/core/PGStream.java 2012-08-21 18:47:10.619742600 -0400 +++ ../x/org/postgresql/core/PGStream.java 2012-08-13 11:34:25.805000000 -0400 @@ -32,6 +32,7 @@ public class PGStream { private final InetSocketAddress address; + private final String localAddress; private final byte[] _int4buf; private final byte[] _int2buf; @@ -48,15 +49,19 @@ * Constructor: Connect to the PostgreSQL back end and return * a stream connection. * - * @param host the hostname to connect to - * @param port the port number that the postmaster is sitting on + * @param address the hostname and port of a postmaster to connect to + * @param localAddress the local address to bind to (or null for "any local address") * @exception IOException if an IOException occurs below it. */ - public PGStream(InetSocketAddress address) throws IOException + public PGStream(InetSocketAddress address, String localAddress) throws IOException { this.address = address; + this.localAddress = localAddress; Socket socket = new Socket(); + if (localAddress != null) { + socket.bind(new InetSocketAddress(localAddress, 0)); + } socket.connect(address); changeSocket(socket); setEncoding(Encoding.getJVMEncoding("US-ASCII")); @@ -69,6 +74,10 @@ return address; } + public String getLocalAddress() { + return localAddress; + } + public Socket getSocket() { return connection; } diff '--exclude=.svn' -ur org/postgresql/core/v2/ConnectionFactoryImpl.java ../x/org/postgresql/core/v2/ConnectionFactoryImpl.java --- org/postgresql/core/v2/ConnectionFactoryImpl.java 2012-08-21 18:47:09.611742600 -0400 +++ ../x/org/postgresql/core/v2/ConnectionFactoryImpl.java 2012-08-13 11:34:24.708000000 -0400 @@ -83,7 +83,7 @@ PGStream newStream = null; try { - newStream = new PGStream(address); + newStream = new PGStream(address, info.getProperty("localAddress")); // Construct and send an ssl startup packet if requested. if (trySSL) @@ -199,7 +199,7 @@ // We have to reconnect to continue. pgStream.close(); - return new PGStream(pgStream.getAddress()); + return new PGStream(pgStream.getAddress(), pgStream.getLocalAddress()); case 'N': if (logger.logDebug()) diff '--exclude=.svn' -ur org/postgresql/core/v2/ProtocolConnectionImpl.java ../x/org/postgresql/core/v2/ProtocolConnectionImpl.java --- org/postgresql/core/v2/ProtocolConnectionImpl.java 2012-08-21 18:47:09.580742600 -0400 +++ ../x/org/postgresql/core/v2/ProtocolConnectionImpl.java 2012-08-13 11:34:24.530000000 -0400 @@ -87,7 +87,7 @@ if (logger.logDebug()) logger.debug(" FE=> CancelRequest(pid=" + cancelPid + ",ckey=" + cancelKey + ")"); - cancelStream = new PGStream(pgStream.getAddress()); + cancelStream = new PGStream(pgStream.getAddress(), pgStream.getLocalAddress()); cancelStream.SendInteger4(16); cancelStream.SendInteger2(1234); cancelStream.SendInteger2(5678); diff '--exclude=.svn' -ur org/postgresql/core/v3/ConnectionFactoryImpl.java ../x/org/postgresql/core/v3/ConnectionFactoryImpl.java --- org/postgresql/core/v3/ConnectionFactoryImpl.java 2012-08-21 18:47:09.930742600 -0400 +++ ../x/org/postgresql/core/v3/ConnectionFactoryImpl.java 2012-08-13 11:34:25.267000000 -0400 @@ -98,7 +98,7 @@ PGStream newStream = null; try { - newStream = new PGStream(address); + newStream = new PGStream(address, info.getProperty("localAddress")); // Construct and send an ssl startup packet if requested. if (trySSL) @@ -257,7 +296,7 @@ // We have to reconnect to continue. pgStream.close(); - return new PGStream(pgStream.getAddress()); + return new PGStream(pgStream.getAddress(), pgStream.getLocalAddress()); case 'N': if (logger.logDebug()) diff '--exclude=.svn' -ur org/postgresql/core/v3/ProtocolConnectionImpl.java ../x/org/postgresql/core/v3/ProtocolConnectionImpl.java --- org/postgresql/core/v3/ProtocolConnectionImpl.java 2012-08-21 18:47:09.872742600 -0400 +++ ../x/org/postgresql/core/v3/ProtocolConnectionImpl.java 2012-08-13 11:34:25.095000000 -0400 @@ -89,7 +89,7 @@ if (logger.logDebug()) logger.debug(" FE=> CancelRequest(pid=" + cancelPid + ",ckey=" + cancelKey + ")"); - cancelStream = new PGStream(pgStream.getAddress()); + cancelStream = new PGStream(pgStream.getAddress(), pgStream.getLocalAddress()); cancelStream.SendInteger4(16); cancelStream.SendInteger2(1234); cancelStream.SendInteger2(5678); diff '--exclude=.svn' -ur org/postgresql/ssl/jdbc3/AbstractJdbc3MakeSSL.java ../x/org/postgresql/ssl/jdbc3/AbstractJdbc3MakeSSL.java --- org/postgresql/ssl/jdbc3/AbstractJdbc3MakeSSL.java 2012-08-21 18:47:12.150742600 -0400 +++ ../x/org/postgresql/ssl/jdbc3/AbstractJdbc3MakeSSL.java 2012-08-13 11:34:27.859000000 -0400 @@ -59,7 +59,7 @@ } } - Socket newConnection = factory.createSocket(stream.getSocket(), stream.getAddress().getHostString(), stream.getAddress().getPort(), true); + Socket newConnection = factory.createSocket(stream.getSocket(), stream.getAddress().getHostName(), stream.getAddress().getPort(), true); stream.changeSocket(newConnection); }

view thread (14+ messages)  latest in thread

Message-ID: <alpine.WNT.2.00.1208211856150.1212@turks>
Permalink:  ../alpine.WNT.2.00.1208211856150.1212@turks/
Also on:    postgresql.org/message-id/alpine.WNT.2.00.1208211856150.1212@turks

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: scotth01@sns-usa.com
  Subject: Re: send/receive buffer size
  In-Reply-To: <alpine.WNT.2.00.1208211856150.1212@turks>
  References: <CC596D71.107BB%skong@investoranalytics.com> <alpine.WNT.2.00.1208211856150.1212@turks>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox