Skip to content

Commit

Permalink
Fixing issues with throughput calculations such that the times are on…
Browse files Browse the repository at this point in the history
…ly updated when bytes are non-zero.
  • Loading branch information
voidmain committed Sep 11, 2023
1 parent b7725e3 commit ce84a6c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 10 deletions.
7 changes: 3 additions & 4 deletions src/main/java/io/fusionauth/http/ClientAbortException.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@
*/
package io.fusionauth.http;

import java.io.IOException;

/**
* An IOException that is most likely caused by the client closing a socket.
* <p>
* For example:
* <p>
* <code>java.io.IOException: Connection reset by peer</code>
*/
public class ClientAbortException extends IOException {
public ClientAbortException(IOException e) {
public class ClientAbortException extends RuntimeException {
public ClientAbortException(Exception e) {
super(e);
}
}
12 changes: 7 additions & 5 deletions src/main/java/io/fusionauth/http/server/HTTP11Processor.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,12 @@ public ProcessorState read(ByteBuffer buffer) throws IOException {
markUsed();

bytesRead += buffer.remaining();
if (firstByteReadInstant == -1) {
lastByteReadInstant = firstByteReadInstant = System.currentTimeMillis();
} else {
lastByteReadInstant = System.currentTimeMillis();
if (bytesRead > 0) {
if (firstByteReadInstant == -1) {
lastByteReadInstant = firstByteReadInstant = System.currentTimeMillis();
} else {
lastByteReadInstant = System.currentTimeMillis();
}
}

logger.trace("(R)");
Expand Down Expand Up @@ -257,7 +259,7 @@ public ProcessorState wrote(long num) {
markUsed();

bytesWritten += num;
if (firstByteWroteInstant == -1) {
if (bytesWritten > 0 && firstByteWroteInstant == -1) {
firstByteWroteInstant = System.currentTimeMillis();
}

Expand Down
4 changes: 3 additions & 1 deletion src/test/java/io/fusionauth/http/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,10 @@ public HTTPServer makeServer(String scheme, HTTPHandler handler, Instrumenter in
.withLoggerFactory(AccumulatingLoggerFactory.FACTORY)
.withMinimumReadThroughput(200 * 1024)
.withMinimumWriteThroughput(200 * 1024)
.withListener(listenerConfiguration)
.withNumberOfWorkerThreads(1)
.withListener(listenerConfiguration);
.withReadThroughputCalculationDelayDuration(Duration.ofSeconds(1))
.withWriteThroughputCalculationDelayDuration(Duration.ofSeconds(1));
}

public URI makeURI(String scheme, String params) {
Expand Down
44 changes: 44 additions & 0 deletions src/test/java/io/fusionauth/http/CoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.zip.InflaterInputStream;

import com.inversoft.net.ssl.SSLTools;
import com.inversoft.rest.ClientResponse;
import com.inversoft.rest.RESTClient;
import com.inversoft.rest.TextResponseHandler;
import io.fusionauth.http.HTTPValues.Connections;
Expand All @@ -57,6 +58,7 @@
*
* @author Brian Pontarelli
*/
@SuppressWarnings("UastIncorrectHttpHeaderInspection")
public class CoreTest extends BaseTest {
public static final String ExpectedResponse = "{\"version\":\"42\"}";

Expand Down Expand Up @@ -290,6 +292,48 @@ public void hugeHeaders(String scheme) throws Exception {
}
}

@Test
public void keepAliveTimeout() {
AtomicBoolean called = new AtomicBoolean(false);
HTTPHandler handler = (req, res) -> {
assertNull(req.getContentType());
if (!called.getAndSet(true)) {
System.out.println("Pausing");
sleep(5 * 60_000);
}

res.setStatus(200);
};

try (HTTPServer ignore = makeServer("http", handler).start()) {
URI uri = makeURI("http", "");
ClientResponse<Void, Void> response = new RESTClient<>(Void.TYPE, Void.TYPE)
.url(uri.toString())
.connectTimeout(0)
.readTimeout(0)
.post()
.go();

if (response.status != 200) {
System.out.println(response.exception);
}

assertEquals(response.status, 200);
response = new RESTClient<>(Void.TYPE, Void.TYPE)
.url(uri.toString())
.connectTimeout(0)
.readTimeout(0)
.post()
.go();

if (response.status != 200) {
System.out.println(response.exception);
}

assertEquals(response.status, 200);
}
}

@Test
public void logger() {
// Test replacement values and ensure we are handling special regex characters.
Expand Down

0 comments on commit ce84a6c

Please sign in to comment.