Skip to content

Commit 9b11699

Browse files
author
fedejinich
committed
configurable server write timeout
1 parent 01c33e7 commit 9b11699

File tree

6 files changed

+32
-8
lines changed

6 files changed

+32
-8
lines changed

rskj-core/src/main/java/co/rsk/RskContext.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1600,7 +1600,8 @@ private Web3WebSocketServer getWeb3WebSocketServer() {
16001600
rskSystemProperties.rpcWebSocketBindAddress(),
16011601
rskSystemProperties.rpcWebSocketPort(),
16021602
jsonRpcHandler,
1603-
getJsonRpcWeb3ServerHandler()
1603+
getJsonRpcWeb3ServerHandler(),
1604+
rskSystemProperties.rpcWebSocketServerWriteTimeout()
16041605
);
16051606
}
16061607

rskj-core/src/main/java/co/rsk/rpc/netty/Web3WebSocketServer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838

3939
public class Web3WebSocketServer implements InternalService {
4040
private static final Logger logger = LoggerFactory.getLogger(Web3WebSocketServer.class);
41-
public static final int WRITE_TIMEOUT_SECONDS = 30;
4241

4342
private final InetAddress host;
4443
private final int port;
@@ -47,18 +46,21 @@ public class Web3WebSocketServer implements InternalService {
4746
private final EventLoopGroup bossGroup;
4847
private final EventLoopGroup workerGroup;
4948
private @Nullable ChannelFuture webSocketChannel;
49+
private final int serverWriteTimeout;
5050

5151
public Web3WebSocketServer(
5252
InetAddress host,
5353
int port,
5454
RskWebSocketJsonRpcHandler webSocketJsonRpcHandler,
55-
JsonRpcWeb3ServerHandler web3ServerHandler) {
55+
JsonRpcWeb3ServerHandler web3ServerHandler,
56+
int serverWriteTimeout) {
5657
this.host = host;
5758
this.port = port;
5859
this.webSocketJsonRpcHandler = webSocketJsonRpcHandler;
5960
this.web3ServerHandler = web3ServerHandler;
6061
this.bossGroup = new NioEventLoopGroup();
6162
this.workerGroup = new NioEventLoopGroup();
63+
this.serverWriteTimeout = serverWriteTimeout;
6264
}
6365

6466
@Override
@@ -73,7 +75,7 @@ protected void initChannel(SocketChannel ch) throws Exception {
7375
ChannelPipeline p = ch.pipeline();
7476
p.addLast(new HttpServerCodec());
7577
p.addLast(new HttpObjectAggregator(1024 * 1024 * 5));
76-
p.addLast(new WriteTimeoutHandler(WRITE_TIMEOUT_SECONDS, TimeUnit.SECONDS));
78+
p.addLast(new WriteTimeoutHandler(serverWriteTimeout, TimeUnit.SECONDS));
7779
p.addLast(new RskWebSocketServerProtocolHandler("/websocket"));
7880
p.addLast(webSocketJsonRpcHandler);
7981
p.addLast(web3ServerHandler);

rskj-core/src/main/java/org/ethereum/config/SystemProperties.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public abstract class SystemProperties {
8282
private static final String PROPERTY_RPC_WEBSOCKET_ENABLED = "rpc.providers.web.ws.enabled";
8383
private static final String PROPERTY_RPC_WEBSOCKET_ADDRESS = "rpc.providers.web.ws.bind_address";
8484
private static final String PROPERTY_RPC_WEBSOCKET_PORT = "rpc.providers.web.ws.port";
85+
private static final String PROPERTY_RPC_WEBSOCKET_SERVER_WRITE_TIMEOUT = "rpc.providers.web.ws.server_write_timeout";
8586

8687
public static final String PROPERTY_PUBLIC_IP = "public.ip";
8788
public static final String PROPERTY_BIND_ADDRESS = "bind_address";
@@ -612,6 +613,10 @@ public int rpcWebSocketPort() {
612613
return configFromFiles.getInt(PROPERTY_RPC_WEBSOCKET_PORT);
613614
}
614615

616+
public int rpcWebSocketServerWriteTimeout() {
617+
return configFromFiles.getInt(PROPERTY_RPC_WEBSOCKET_SERVER_WRITE_TIMEOUT);
618+
}
619+
615620
public InetAddress rpcHttpBindAddress() {
616621
return getWebBindAddress(PROPERTY_RPC_HTTP_ADDRESS);
617622
}

rskj-core/src/main/resources/expected.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ rpc = {
219219
enabled = <enabled>
220220
bind_address = <bind_address>
221221
port = <port>
222+
server_write_timeout = <timeout>
222223
}
223224
}
224225
}

rskj-core/src/main/resources/reference.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ rpc {
281281
enabled = false
282282
bind_address = localhost
283283
port = 4445
284+
# Shuts down the server when it's not able to write a response after a certain period (expressed in seconds)
285+
server_write_timeout = 30
284286
}
285287
}
286288
}

rskj-core/src/test/java/co/rsk/rpc/netty/Web3WebSocketServerTest.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package co.rsk.rpc.netty;
1919

20+
import co.rsk.config.TestSystemProperties;
2021
import co.rsk.rpc.JacksonBasedRpcSerializer;
2122
import co.rsk.rpc.ModuleDescription;
2223
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -30,6 +31,7 @@
3031
import okio.Buffer;
3132
import org.ethereum.rpc.Web3;
3233
import org.junit.After;
34+
import org.junit.Assert;
3335
import org.junit.Before;
3436
import org.junit.Test;
3537

@@ -46,14 +48,14 @@
4648
import java.util.concurrent.atomic.AtomicReference;
4749

4850
import static org.hamcrest.Matchers.is;
49-
import static org.junit.Assert.assertThat;
50-
import static org.junit.Assert.fail;
51+
import static org.junit.Assert.*;
5152
import static org.mockito.Mockito.*;
5253

5354
public class Web3WebSocketServerTest {
5455

5556
private static JsonNodeFactory JSON_NODE_FACTORY = JsonNodeFactory.instance;
5657
private static ObjectMapper OBJECT_MAPPER = new ObjectMapper();
58+
private static final int DEFAULT_WRITE_TIMEOUT = 30;
5759

5860
private ExecutorService wsExecutor;
5961

@@ -68,13 +70,24 @@ public void smokeTest() throws Exception {
6870
String mockResult = "output";
6971
when(web3Mock.web3_sha3(anyString())).thenReturn(mockResult);
7072

71-
int randomPort = 9998;//new ServerSocket(0).getLocalPort();
73+
int randomPort = 9998;
74+
75+
TestSystemProperties testSystemProperties = new TestSystemProperties();
7276

7377
List<ModuleDescription> filteredModules = Collections.singletonList(new ModuleDescription("web3", "1.0", true, Collections.emptyList(), Collections.emptyList()));
7478
RskWebSocketJsonRpcHandler handler = new RskWebSocketJsonRpcHandler(null, new JacksonBasedRpcSerializer());
7579
JsonRpcWeb3ServerHandler serverHandler = new JsonRpcWeb3ServerHandler(web3Mock, filteredModules);
80+
int serverWriteTimout = testSystemProperties.rpcWebSocketServerWriteTimeout();
81+
82+
assertEquals(DEFAULT_WRITE_TIMEOUT, serverWriteTimout);
7683

77-
Web3WebSocketServer websocketServer = new Web3WebSocketServer(InetAddress.getLoopbackAddress(), randomPort, handler, serverHandler);
84+
Web3WebSocketServer websocketServer = new Web3WebSocketServer(
85+
InetAddress.getLoopbackAddress(),
86+
randomPort,
87+
handler,
88+
serverHandler,
89+
serverWriteTimout
90+
);
7891
websocketServer.start();
7992

8093
OkHttpClient wsClient = new OkHttpClient();

0 commit comments

Comments
 (0)