Skip to content

Commit

Permalink
[MRESOLVER-635] Use Instant instead of untyped millis for TransferRes…
Browse files Browse the repository at this point in the history
…ource startTime (#615)
  • Loading branch information
gnodet authored Dec 12, 2024
1 parent 9c91325 commit 15a7d65
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import java.io.File;
import java.nio.file.Path;
import java.time.Clock;
import java.time.Instant;

import org.eclipse.aether.RequestTrace;

Expand All @@ -28,6 +30,8 @@
*/
public final class TransferResource {

private static Clock clock = Clock.systemUTC();

private final String repositoryId;

private final String repositoryUrl;
Expand All @@ -38,14 +42,22 @@ public final class TransferResource {

private final Path path;

private final long startTime;
private final Instant startTime;

private final RequestTrace trace;

private long contentLength = -1L;

private long resumeOffset;

public static Clock getClock() {
return clock;
}

public static void setClock(Clock clock) {
TransferResource.clock = clock;
}

/**
* Creates a new transfer resource with the specified properties.
*
Expand Down Expand Up @@ -114,7 +126,7 @@ public TransferResource(
this.path = path;
this.resource = resource;
this.trace = trace;
this.startTime = System.currentTimeMillis();
this.startTime = getClock().instant();
}

/**
Expand Down Expand Up @@ -232,8 +244,19 @@ public TransferResource setResumeOffset(long resumeOffset) {
* Gets the timestamp when the transfer of this resource was started.
*
* @return The timestamp when the transfer of this resource was started.
* @deprecated use {@link #getStartTime()}
*/
@Deprecated
public long getTransferStartTime() {
return startTime.toEpochMilli();
}

/**
* Gets the timestamp when the transfer of this resource was started.
*
* @return The timestamp when the transfer of this resource was started.
*/
public Instant getStartTime() {
return startTime;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.io.PrintStream;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.time.Duration;
import java.time.Instant;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -116,11 +118,11 @@ public void transferSucceeded(TransferEvent event) {
String len = contentLength >= 1024 ? toKB(contentLength) + " KB" : contentLength + " B";

String throughput = "";
long duration = System.currentTimeMillis() - resource.getTransferStartTime();
if (duration > 0) {
Duration duration = Duration.between(resource.getStartTime(), Instant.now());
if (duration.toMillis() > 0) {
long bytes = contentLength - resource.getResumeOffset();
DecimalFormat format = new DecimalFormat("0.0", new DecimalFormatSymbols(Locale.ENGLISH));
double kbPerSec = (bytes / 1024.0) / (duration / 1000.0);
double kbPerSec = (bytes / 1024.0) / duration.toSeconds();
throughput = " at " + format.format(kbPerSec) + " KB/sec";
}

Expand Down

0 comments on commit 15a7d65

Please sign in to comment.