Skip to content

Commit

Permalink
Instruct (non-SSL) socket to use TCP keep-alive
Browse files Browse the repository at this point in the history
  • Loading branch information
jakelandis committed Jun 4, 2018
1 parent f08363a commit 9b1efb6
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ build
*.iml
*.iws
*.ipr
out
.rakeTasks

3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 5.0.9
- New configuration option to set TCP keep-alive [#16](https://github.com/logstash-plugins/logstash-input-tcp/pull/116)

## 5.0.8
- Reorder shut down of the two event loops to prevent RejectedExecutionException

Expand Down
6 changes: 5 additions & 1 deletion lib/logstash/inputs/tcp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ class LogStash::Inputs::Tcp < LogStash::Inputs::Base
# Useful when the CA chain is not necessary in the system store.
config :ssl_extra_chain_certs, :validate => :array, :default => []

# Instruct the socket to use TCP keep alives. Uses OS defaults for keep alive settings.
config :tcp_keep_alive, :validate => :boolean, :default => false

HOST_FIELD = "host".freeze
HOST_IP_FIELD = "[@metadata][ip_address]".freeze
PORT_FIELD = "port".freeze
Expand Down Expand Up @@ -140,7 +143,7 @@ def register
if @ssl_enable
self.server_socket = new_server_socket
else
@loop = InputLoop.new(@host, @port, DecoderImpl.new(@codec, self))
@loop = InputLoop.new(@host, @port, DecoderImpl.new(@codec, self), @tcp_keep_alive)
end
end
end
Expand Down Expand Up @@ -341,6 +344,7 @@ def new_server_socket

def new_client_socket
socket = TCPSocket.new(@host, @port)
socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true) if @tcp_keep_alive

if @ssl_enable
socket = OpenSSL::SSL::SSLSocket.new(socket, ssl_context)
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/logstash/tcp/InputLoop.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ public final class InputLoop implements Runnable, Closeable {
* @param host Host to bind the listen to
* @param port Port to listen on
* @param decoder {@link Decoder} provided by Jruby
* @param keepAlive set to true to instruct the socket to issue TCP keep alive
*/
public InputLoop(final String host, final int port, final Decoder decoder) {
public InputLoop(final String host, final int port, final Decoder decoder, final boolean keepAlive) {
worker = new NioEventLoopGroup();
boss = new NioEventLoopGroup(1);
future = new ServerBootstrap().group(boss, worker)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1024)
.childOption(ChannelOption.SO_KEEPALIVE, keepAlive)
.childHandler(new InputLoop.InputHandler(decoder)).bind(host, port);
}

Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.0.8
5.0.9

0 comments on commit 9b1efb6

Please sign in to comment.