Skip to content

fix not use connectTimeout #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 36 additions & 21 deletions src/main/java/com/notnoop/apns/internal/ApnsConnectionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
Expand All @@ -45,13 +46,15 @@

import javax.net.SocketFactory;
import javax.net.ssl.SSLSocketFactory;

import com.notnoop.apns.ApnsDelegate;
import com.notnoop.apns.ApnsNotification;
import com.notnoop.apns.DeliveryError;
import com.notnoop.apns.EnhancedApnsNotification;
import com.notnoop.apns.ReconnectPolicy;
import com.notnoop.exceptions.ApnsDeliveryErrorException;
import com.notnoop.exceptions.NetworkIOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -136,7 +139,6 @@ private void monitorSocket(final Socket socket) {
Thread t = threadFactory.newThread(new Runnable() {
final static int EXPECTED_SIZE = 6;

@SuppressWarnings("InfiniteLoopStatement")
@Override
public void run() {
logger.debug("Started monitoring thread");
Expand Down Expand Up @@ -244,9 +246,15 @@ private boolean readPacket(final InputStream in, final byte[] bytes) throws IOEx
}
n += count;
} catch (IOException ioe) {
if (n == 0)
return false;
throw new IOException("Error after reading "+n+" bytes of packet", ioe);
if (!(ioe instanceof SocketTimeoutException)) {
if (n == 0)
return false;
throw new IOException("Error after reading "+n+" bytes of packet", ioe);
} else {
if (logger.isDebugEnabled()) {
logger.debug("just SocketTimeoutException ignore");
}
}
}
}
return true;
Expand All @@ -265,7 +273,8 @@ private synchronized Socket getOrCreateSocket() throws NetworkIOException {
if (socket == null || socket.isClosed()) {
try {
if (proxy == null) {
socket = factory.createSocket(host, port);
socket = factory.createSocket();
socket.connect(new InetSocketAddress(host, port),connectTimeout);
logger.debug("Connected new socket {}", socket);
} else if (proxy.type() == Proxy.Type.HTTP) {
TlsTunnelBuilder tunnelBuilder = new TlsTunnelBuilder();
Expand Down Expand Up @@ -330,26 +339,32 @@ private synchronized void sendMessage(ApnsNotification m, boolean fromBuffer) th
attempts = 0;
break;
} catch (IOException e) {
Utilities.close(socket);
if (attempts >= RETRIES) {
logger.error("Couldn't send message after " + RETRIES + " retries." + m, e);
delegate.messageSendFailed(m, e);
Utilities.wrapAndThrowAsRuntimeException(e);
}
// The first failure might be due to closed connection (which in turn might be caused by
// a message containing a bad token), so don't delay for the first retry.
//
// Additionally we don't want to spam the log file in this case, only after the second retry
// which uses the delay.

if (attempts != 1) {
logger.info("Failed to send message " + m + "... trying again after delay", e);
Utilities.sleep(DELAY_IN_MS);
}
dealException(attempts, m, e);
} catch (NetworkIOException e){
dealException(attempts, m, e);
}
}
}

private void dealException(int attempts,ApnsNotification m,Exception e){
Utilities.close(socket);
if (attempts >= RETRIES) {
logger.error("Couldn't send message after " + RETRIES + " retries." + m, e);
delegate.messageSendFailed(m, e);
Utilities.wrapAndThrowAsRuntimeException(e);
}
// The first failure might be due to closed connection (which in turn might be caused by
// a message containing a bad token), so don't delay for the first retry.
//
// Additionally we don't want to spam the log file in this case, only after the second retry
// which uses the delay.

if (attempts != 1) {
logger.info("Failed to send message " + m + "... trying again after delay", e);
Utilities.sleep(DELAY_IN_MS);
}
}

private synchronized void drainBuffer() {
logger.debug("draining buffer");
while (!notificationsBuffer.isEmpty()) {
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/com/notnoop/apns/internal/MockingUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ static SocketFactory mockClosedThenOpenSocket(OutputStream out, InputStream in,
for (Socket t : socketMocks)
stubbing = stubbing.thenReturn(t);

stubbing = when(factory.createSocket());
for (Socket t : socketMocks)
stubbing = stubbing.thenReturn(t);

return factory;
} catch (Exception e) {
e.printStackTrace();
Expand Down