Skip to content

Commit a9eb22f

Browse files
committed
DBZ-7570/osheroff#133: add workaround using SO_LINGER with 0 timeout
1 parent 0f38e43 commit a9eb22f

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.zendesk</groupId>
66
<artifactId>mysql-binlog-connector-java</artifactId>
7-
<version>0.29.1</version>
7+
<version>0.29.2</version>
88

99
<name>mysql-binlog-connector-java</name>
1010
<description>MySQL Binary Log connector</description>

src/main/java/com/github/shyiko/mysql/binlog/BinaryLogClient.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ public X509Certificate[] getAcceptedIssuers() {
137137
private volatile long binlogPosition = 4;
138138
private volatile long connectionId;
139139
private SSLMode sslMode = SSLMode.DISABLED;
140+
private boolean useNonGracefulDisconnect = false;
140141

141142
protected GtidSet gtidSet;
142143
protected final Object gtidSetAccessLock = new Object();
@@ -249,6 +250,10 @@ public void setSSLMode(SSLMode sslMode) {
249250
this.sslMode = sslMode;
250251
}
251252

253+
public void setUseNonGracefulDisconnect(boolean useNonGracefulDisconnect) {
254+
this.useNonGracefulDisconnect = useNonGracefulDisconnect;
255+
}
256+
252257
public long getMasterServerId() {
253258
return this.masterServerId;
254259
}
@@ -891,7 +896,7 @@ public void run() {
891896
if (connectionLost) {
892897
logger.info("Keepalive: Trying to restore lost connection to " + hostname + ":" + port);
893898
try {
894-
terminateConnect();
899+
terminateConnect(useNonGracefulDisconnect);
895900
connect(connectTimeout);
896901
} catch (Exception ce) {
897902
logger.warning("keepalive: Failed to restore connection to " + hostname + ":" + port +
@@ -1341,8 +1346,11 @@ private static boolean awaitTerminationInterruptibly(ExecutorService executorSer
13411346
}
13421347

13431348
private void terminateConnect() throws IOException {
1349+
terminateConnect(false);
1350+
}
1351+
private void terminateConnect(boolean force) throws IOException {
13441352
do {
1345-
disconnectChannel();
1353+
disconnectChannel(force);
13461354
} while (!tryLockInterruptibly(connectLock, 1000, TimeUnit.MILLISECONDS));
13471355
connectLock.unlock();
13481356
}
@@ -1356,8 +1364,14 @@ private static boolean tryLockInterruptibly(Lock lock, long time, TimeUnit unit)
13561364
}
13571365

13581366
private void disconnectChannel() throws IOException {
1367+
disconnectChannel(false);
1368+
}
1369+
private void disconnectChannel(boolean force) throws IOException {
13591370
connected = false;
13601371
if (channel != null && channel.isOpen()) {
1372+
if (force) {
1373+
channel.setShouldUseSoLinger0();
1374+
}
13611375
channel.close();
13621376
}
13631377
}

src/main/java/com/github/shyiko/mysql/binlog/network/protocol/PacketChannel.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import javax.net.ssl.SSLSocket;
2727
import java.io.IOException;
2828
import java.net.Socket;
29+
import java.net.SocketException;
2930
import java.nio.channels.Channel;
3031

3132
/**
@@ -38,6 +39,7 @@ public class PacketChannel implements Channel {
3839
private Socket socket;
3940
private ByteArrayInputStream inputStream;
4041
private ByteArrayOutputStream outputStream;
42+
private boolean shouldUseSoLinger0 = false;
4143

4244
public PacketChannel(String hostname, int port) throws IOException {
4345
this(new Socket(hostname, port));
@@ -109,6 +111,10 @@ public boolean isSSL() {
109111
return isSSL;
110112
}
111113

114+
public void setShouldUseSoLinger0() {
115+
shouldUseSoLinger0 = true;
116+
}
117+
112118
@Override
113119
public boolean isOpen() {
114120
return !socket.isClosed();
@@ -126,6 +132,14 @@ public void close() throws IOException {
126132
} catch (Exception e) {
127133
// ignore
128134
}
135+
if (shouldUseSoLinger0) {
136+
try {
137+
socket.setSoLinger(true, 0);
138+
} catch (SocketException e) {
139+
// ignore
140+
}
141+
}
129142
socket.close();
143+
shouldUseSoLinger0 = false;
130144
}
131145
}

0 commit comments

Comments
 (0)