This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NIO channels don't discover a disconnect without activity. logstash-gelf now performs a read operation before writing data. This way the socket can discover the connection state. Reading is non-blocking so the performance impact is minor.
- Loading branch information
Showing
5 changed files
with
305 additions
and
83 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
src/main/java/biz/paluch/logging/gelf/intern/sender/AbstractNioSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package biz.paluch.logging.gelf.intern.sender; | ||
|
||
import java.io.IOException; | ||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
import java.nio.ByteBuffer; | ||
import java.nio.channels.ByteChannel; | ||
import java.nio.channels.spi.AbstractSelectableChannel; | ||
|
||
import biz.paluch.logging.gelf.intern.Closer; | ||
import biz.paluch.logging.gelf.intern.ErrorReporter; | ||
|
||
/** | ||
* @author Mark Paluch | ||
*/ | ||
public abstract class AbstractNioSender<T extends AbstractSelectableChannel & ByteChannel> implements ErrorReporter { | ||
|
||
private T channel; | ||
private volatile boolean shutdown = false; | ||
private final ErrorReporter errorReporter; | ||
private final String host; | ||
private final int port; | ||
|
||
private final ThreadLocal<ByteBuffer> readBuffers = new ThreadLocal<ByteBuffer>() { | ||
@Override | ||
protected ByteBuffer initialValue() { | ||
return ByteBuffer.allocate(1); | ||
} | ||
}; | ||
|
||
protected AbstractNioSender(ErrorReporter errorReporter, String host, int port) throws UnknownHostException { | ||
|
||
// validate first address succeeds. | ||
InetAddress.getByName(host); | ||
this.errorReporter = errorReporter; | ||
this.host = host; | ||
this.port = port; | ||
|
||
} | ||
|
||
protected boolean isConnected() throws IOException { | ||
|
||
ByteBuffer byteBuffer = readBuffers.get(); | ||
byteBuffer.clear(); | ||
|
||
if (channel() != null && channel().isOpen() && isConnected(channel()) && channel.read(byteBuffer) >= 0) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
protected abstract boolean isConnected(T channel); | ||
|
||
protected T channel() { | ||
return channel; | ||
} | ||
|
||
public String getHost() { | ||
return host; | ||
} | ||
|
||
public int getPort() { | ||
return port; | ||
} | ||
|
||
public void close() { | ||
shutdown = true; | ||
Closer.close(channel()); | ||
} | ||
|
||
public boolean isShutdown() { | ||
return shutdown; | ||
} | ||
|
||
@Override | ||
public void reportError(String message, Exception e) { | ||
errorReporter.reportError(message, e); | ||
} | ||
|
||
public void setChannel(T channel) { | ||
this.channel = channel; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.