Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.apache.hc.core5.reactor.IOSession;
import org.apache.hc.core5.reactor.ssl.TransportSecurityLayer;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.Deadline;
import org.apache.hc.core5.util.TimeValue;
import org.apache.hc.core5.util.Timeout;

Expand Down Expand Up @@ -144,11 +145,10 @@ protected void validateSession(
final TimeValue timeValue = validateAfterInactivity;
if (TimeValue.isNonNegative(timeValue)) {
final long lastAccessTime = Math.min(ioSession.getLastReadTime(), ioSession.getLastWriteTime());
final long deadline = lastAccessTime + timeValue.toMilliseconds();
if (deadline <= System.currentTimeMillis()) {
final Timeout socketTimeoutMillis = ioSession.getSocketTimeout();
if (Deadline.calculate(lastAccessTime, timeValue).isNotExpired()) {
final Timeout socketTimeout = ioSession.getSocketTimeout();
ioSession.enqueue(new PingCommand(new BasicPingHandler(result -> {
ioSession.setSocketTimeout(socketTimeoutMillis);
ioSession.setSocketTimeout(socketTimeout);
callback.execute(result);
})), Command.Priority.NORMAL);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/
package org.apache.hc.core5.pool;

import java.time.Instant;
import java.util.Deque;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -276,7 +277,7 @@ public void closeIdle(final TimeValue idleTime) {

@Override
public void closeExpired() {
final long now = System.currentTimeMillis();
final Instant now = Instant.now();
enumAvailable(entry -> {
if (entry.getExpiryDeadline().isBefore(now)) {
entry.discardConnection(CloseMode.GRACEFUL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*/
package org.apache.hc.core5.pool;

import java.time.Instant;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -411,7 +412,7 @@ private void fireCallbacks() {
public void validatePendingRequests() {
this.lock.lock();
try {
final long now = System.currentTimeMillis();
final Instant now = Instant.now();
final ListIterator<LeaseRequest<T, C>> it = this.pendingRequests.listIterator();
while (it.hasNext()) {
final LeaseRequest<T, C> request = it.next();
Expand Down Expand Up @@ -629,7 +630,7 @@ public void closeIdle(final TimeValue idleTime) {

@Override
public void closeExpired() {
final long now = System.currentTimeMillis();
final Instant now = Instant.now();
enumAvailable(entry -> {
if (entry.getExpiryDeadline().isBefore(now)) {
entry.discardConnection(CloseMode.GRACEFUL);
Expand Down
36 changes: 34 additions & 2 deletions httpcore5/src/main/java/org/apache/hc/core5/reactor/IOSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.net.SocketAddress;
import java.nio.channels.ByteChannel;
import java.time.Instant;
import java.util.concurrent.locks.Lock;

import org.apache.hc.core5.annotation.Internal;
Expand Down Expand Up @@ -198,7 +199,7 @@ enum Status {
* <p>
* Please note this operation may affect the last event time.
*
* @see #getLastEventTime()
* @see #getLastEventInstant()
*
* @param timeout socket timeout.
*/
Expand All @@ -212,22 +213,53 @@ enum Status {
*/
long getLastReadTime();

/**
* Returns timestamp of the last read event.
*
* @return timestamp.
* @since 5.2
*/
default Instant getLastReadInstant() {
return Instant.ofEpochMilli(getLastReadTime());
}

/**
* Returns timestamp of the last write event.
*
* @return timestamp.
*/
long getLastWriteTime();

/**
* Returns timestamp of the last write event.
*
* @return timestamp.
* @since 5.2
*/
default Instant getLastWriteInstant() {
return Instant.ofEpochMilli(getLastWriteTime());
}

/**
* Returns timestamp of the last I/O event including socket timeout reset.
*
* @see #getSocketTimeout()
*
* @return timestamp.
*/
long getLastEventTime();

/**
* Returns timestamp of the last I/O event including socket timeout reset.
*
* @see #getSocketTimeout()
* @return timestamp.
* @since 5.2
*/
default Instant getLastEventInstant() {
return Instant.ofEpochMilli(getLastEventTime());
}


/**
* Updates the timestamp of the last read event
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.nio.channels.ByteChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.time.Instant;
import java.util.Deque;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.atomic.AtomicLong;
Expand All @@ -60,9 +61,9 @@ class IOSessionImpl implements IOSession {
private final AtomicReference<IOSession.Status> status;

private volatile Timeout socketTimeout;
private volatile long lastReadTime;
private volatile long lastWriteTime;
private volatile long lastEventTime;
private volatile Instant lastReadTime = Instant.EPOCH;
private volatile Instant lastWriteTime = Instant.EPOCH;
private volatile Instant lastEventTime = Instant.EPOCH;

public IOSessionImpl(final String type, final SelectionKey key, final SocketChannel socketChannel) {
super();
Expand All @@ -74,10 +75,10 @@ public IOSessionImpl(final String type, final SelectionKey key, final SocketChan
this.id = String.format(type + "-%010d", COUNT.getAndIncrement());
this.handlerRef = new AtomicReference<>();
this.status = new AtomicReference<>(Status.ACTIVE);
final long currentTimeMillis = System.currentTimeMillis();
this.lastReadTime = currentTimeMillis;
this.lastWriteTime = currentTimeMillis;
this.lastEventTime = currentTimeMillis;
final Instant now = Instant.now();
this.lastReadTime = now;
this.lastWriteTime = now;
this.lastEventTime = now;
}

@Override
Expand Down Expand Up @@ -194,7 +195,7 @@ public Timeout getSocketTimeout() {
@Override
public void setSocketTimeout(final Timeout timeout) {
this.socketTimeout = Timeout.defaultsToDisabled(timeout);
this.lastEventTime = System.currentTimeMillis();
this.lastEventTime = Instant.now();
}

@Override
Expand All @@ -209,28 +210,43 @@ public int write(final ByteBuffer src) throws IOException {

@Override
public void updateReadTime() {
lastReadTime = System.currentTimeMillis();
lastReadTime = Instant.now();
lastEventTime = lastReadTime;
}

@Override
public void updateWriteTime() {
lastWriteTime = System.currentTimeMillis();
lastWriteTime = Instant.now();
lastEventTime = lastWriteTime;
}

@Override
public long getLastReadTime() {
return lastReadTime.toEpochMilli();
}

@Override
public Instant getLastReadInstant() {
return lastReadTime;
}

@Override
public long getLastWriteTime() {
return lastWriteTime.toEpochMilli();
}

@Override
public Instant getLastWriteInstant() {
return lastWriteTime;
}

@Override
public long getLastEventTime() {
return lastEventTime.toEpochMilli();
}

@Override
public Instant getLastEventInstant() {
return lastEventTime;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
import java.nio.channels.SelectionKey;
import java.time.Instant;
import java.util.Locale;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -400,16 +401,31 @@ public long getLastReadTime() {
return ioSession.getLastReadTime();
}

@Override
public Instant getLastReadInstant() {
return ioSession.getLastReadInstant();
}

@Override
public long getLastWriteTime() {
return ioSession.getLastWriteTime();
}

@Override
public Instant getLastWriteInstant() {
return ioSession.getLastWriteInstant();
}

@Override
public long getLastEventTime() {
return ioSession.getLastEventTime();
}

@Override
public Instant getLastEventInstant() {
return ioSession.getLastEventInstant();
}

@Override
public void switchProtocol(final String protocolId, final FutureCallback<ProtocolIOSession> callback) {
Args.notEmpty(protocolId, "Application protocol ID");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.nio.channels.CancelledKeyException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectionKey;
import java.time.Instant;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Lock;
Expand Down Expand Up @@ -835,17 +836,32 @@ public void updateWriteTime() {

@Override
public long getLastReadTime() {
return this.session.getLastReadTime();
return session.getLastReadTime();
}

@Override
public Instant getLastReadInstant() {
return session.getLastReadInstant();
}

@Override
public long getLastWriteTime() {
return this.session.getLastWriteTime();
return session.getLastWriteTime();
}

@Override
public Instant getLastWriteInstant() {
return session.getLastWriteInstant();
}

@Override
public long getLastEventTime() {
return this.session.getLastEventTime();
return session.getLastEventTime();
}

@Override
public Instant getLastEventInstant() {
return session.getLastEventInstant();
}

private static void formatOps(final StringBuilder buffer, final int ops) {
Expand Down
Loading