Skip to content

Commit

Permalink
Removed caching of the DNS resolution - the code will now rely on the… (
Browse files Browse the repository at this point in the history
#124)

* Removed caching of the DNS resolution - the code will now rely on the JVM's cache instead and rely on the networkaddress.cache.ttl security property;
For the UDP sender, also added a refresh of the channel every 5 minutes - otherwise the same channel would keep sending to the same IP forever.

Signed-off-by: Gaspard Petit <[email protected]>

* Fixed the refresh from 30 seconds to 5 mins :)

Signed-off-by: Gaspard Petit <[email protected]>
  • Loading branch information
gaspardpetit authored Sep 2, 2021
1 parent 30bf9b2 commit cb458f6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/graylog2/GelfTCPSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import java.io.IOException;
import java.io.OutputStream;
import java.net.*;
import java.net.Socket;

public class GelfTCPSender implements GelfSender {
private boolean shutdown = false;
private InetAddress host;
private String host;
private int port;
private Socket socket;
private OutputStream os;
Expand All @@ -15,7 +15,7 @@ public GelfTCPSender() {
}

public GelfTCPSender(String host, int port) throws IOException {
this.host = InetAddress.getByName(host);
this.host = host;
this.port = port;
this.socket = new Socket(host, port);
this.os = socket.getOutputStream();
Expand Down
26 changes: 22 additions & 4 deletions src/main/java/org/graylog2/GelfUDPSender.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package org.graylog2;

import java.io.IOException;
import java.net.*;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.DatagramChannel;
import java.util.Date;

public class GelfUDPSender implements GelfSender {

private InetAddress host;
private String host;
private int port;
private DatagramChannel channel;
private Date lastChannelRefresh = null;

private static final int MAX_RETRIES = 5;
private static final int REFRESH_CHANNEL_SECONDS = 300;

public GelfUDPSender() {
}
Expand All @@ -22,7 +24,7 @@ public GelfUDPSender(String host) throws IOException {
}

public GelfUDPSender(String host, int port) throws IOException {
this.host = InetAddress.getByName(host);
this.host = host;
this.port = port;
setChannel(initiateChannel());
}
Expand All @@ -49,6 +51,10 @@ private GelfSenderResult sendDatagrams(ByteBuffer[] bytesList) {

try {

if (isChannelOld() ) {
getChannel().close();
}

if (!getChannel().isOpen()) {
setChannel(initiateChannel());
}
Expand Down Expand Up @@ -81,5 +87,17 @@ public DatagramChannel getChannel() {

public void setChannel(DatagramChannel channel) {
this.channel = channel;
this.lastChannelRefresh = new Date();
}

private boolean isChannelOld() {
if (this.lastChannelRefresh == null)
return true;

Date now = new Date();
if ((now.getTime() - REFRESH_CHANNEL_SECONDS * 1000) > this.lastChannelRefresh.getTime())
return true;

return false;
}
}

0 comments on commit cb458f6

Please sign in to comment.