Skip to content

Commit aa5f183

Browse files
committed
THRIFT-5863: Add method for customizing the TConfiguration in TServerTransport
1 parent 5e6d1b7 commit aa5f183

File tree

6 files changed

+36
-12
lines changed

6 files changed

+36
-12
lines changed

lib/java/src/main/java/org/apache/thrift/TConfiguration.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ public void setRecursionLimit(int recursionLimit) {
6262
this.recursionLimit = recursionLimit;
6363
}
6464

65+
public void deepCopy(TConfiguration configuration) {
66+
if (configuration != null) {
67+
setMaxMessageSize(configuration.getMaxMessageSize());
68+
setRecursionLimit(configuration.getRecursionLimit());
69+
setMaxFrameSize(configuration.getMaxFrameSize());
70+
}
71+
}
72+
6573
public static final TConfiguration DEFAULT = new Builder().build();
6674

6775
public static TConfiguration.Builder custom() {

lib/java/src/main/java/org/apache/thrift/transport/TNonblockingServerSocket.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ public class TNonblockingServerSocket extends TNonblockingServerTransport {
4646
/** Timeout for client sockets from accept */
4747
private int clientTimeout_ = 0;
4848

49-
/** Limit for client sockets request size */
50-
private int maxFrameSize_ = 0;
49+
private final TConfiguration configuration_;
5150

5251
public static class NonblockingAbstractServerSocketArgs
5352
extends AbstractServerTransportArgs<NonblockingAbstractServerSocketArgs> {}
@@ -92,7 +91,7 @@ public TNonblockingServerSocket(InetSocketAddress bindAddr, int clientTimeout, i
9291
public TNonblockingServerSocket(NonblockingAbstractServerSocketArgs args)
9392
throws TTransportException {
9493
clientTimeout_ = args.clientTimeout;
95-
maxFrameSize_ = args.maxFrameSize;
94+
configuration_ = args.configuration;
9695
try {
9796
serverSocketChannel = ServerSocketChannel.open();
9897
serverSocketChannel.configureBlocking(false);
@@ -132,10 +131,7 @@ public TNonblockingSocket accept() throws TTransportException {
132131
return null;
133132
}
134133

135-
TNonblockingSocket tsocket = new TNonblockingSocket(socketChannel);
136-
tsocket.setTimeout(clientTimeout_);
137-
tsocket.setMaxFrameSize(maxFrameSize_);
138-
return tsocket;
134+
return new TNonblockingSocket(configuration_, socketChannel, clientTimeout_, null);
139135
} catch (IOException iox) {
140136
throw new TTransportException(iox);
141137
}

lib/java/src/main/java/org/apache/thrift/transport/TNonblockingSocket.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private TNonblockingSocket(SocketChannel socketChannel, int timeout, SocketAddre
7474
this(new TConfiguration(), socketChannel, timeout, socketAddress);
7575
}
7676

77-
private TNonblockingSocket(
77+
public TNonblockingSocket(
7878
TConfiguration config, SocketChannel socketChannel, int timeout, SocketAddress socketAddress)
7979
throws IOException, TTransportException {
8080
super(config);

lib/java/src/main/java/org/apache/thrift/transport/TServerSocket.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.net.ServerSocket;
2525
import java.net.Socket;
2626
import java.net.SocketException;
27+
import org.apache.thrift.TConfiguration;
2728
import org.slf4j.Logger;
2829
import org.slf4j.LoggerFactory;
2930

@@ -38,6 +39,8 @@ public class TServerSocket extends TServerTransport {
3839
/** Timeout for client sockets from accept */
3940
private int clientTimeout_ = 0;
4041

42+
private final TConfiguration configuration_;
43+
4144
public static class ServerSocketTransportArgs
4245
extends AbstractServerTransportArgs<ServerSocketTransportArgs> {
4346
ServerSocket serverSocket;
@@ -78,6 +81,7 @@ public TServerSocket(InetSocketAddress bindAddr, int clientTimeout) throws TTran
7881

7982
public TServerSocket(ServerSocketTransportArgs args) throws TTransportException {
8083
clientTimeout_ = args.clientTimeout;
84+
configuration_ = args.configuration;
8185
if (args.serverSocket != null) {
8286
this.serverSocket_ = args.serverSocket;
8387
return;
@@ -121,7 +125,7 @@ public TSocket accept() throws TTransportException {
121125
if (result == null) {
122126
throw new TTransportException("Blocking server's accept() may not return NULL");
123127
}
124-
TSocket socket = new TSocket(result);
128+
TSocket socket = new TSocket(result, configuration_);
125129
socket.setTimeout(clientTimeout_);
126130
return socket;
127131
}

lib/java/src/main/java/org/apache/thrift/transport/TServerTransport.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public abstract static class AbstractServerTransportArgs<
3131
int backlog = 0; // A value of 0 means the default value will be used (currently set at 50)
3232
int clientTimeout = 0;
3333
InetSocketAddress bindAddr;
34-
int maxFrameSize = TConfiguration.DEFAULT_MAX_FRAME_SIZE;
34+
TConfiguration configuration = new TConfiguration();
3535

3636
public T backlog(int backlog) {
3737
this.backlog = backlog;
@@ -54,7 +54,12 @@ public T bindAddr(InetSocketAddress bindAddr) {
5454
}
5555

5656
public T maxFrameSize(int maxFrameSize) {
57-
this.maxFrameSize = maxFrameSize;
57+
configuration.setMaxFrameSize(maxFrameSize);
58+
return (T) this;
59+
}
60+
61+
public T setTConfiguration(TConfiguration configuration_) {
62+
configuration.deepCopy(configuration_);
5863
return (T) this;
5964
}
6065
}

lib/java/src/main/java/org/apache/thrift/transport/TSocket.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,18 @@ public class TSocket extends TIOStreamTransport {
5656
* @throws TTransportException if there is an error setting up the streams
5757
*/
5858
public TSocket(Socket socket) throws TTransportException {
59-
super(new TConfiguration());
59+
this(socket, new TConfiguration());
60+
}
61+
62+
/**
63+
* Constructor that takes an already created socket.
64+
*
65+
* @param socket Already created socket object
66+
* @param configuration Customized configuration for this transport
67+
* @throws TTransportException if there is an error setting up the streams
68+
*/
69+
public TSocket(Socket socket, TConfiguration configuration) throws TTransportException {
70+
super(configuration);
6071
socket_ = socket;
6172
try {
6273
socket_.setSoLinger(false, 0);

0 commit comments

Comments
 (0)